Как заполнить треугольник с помощью класса Graphics (Java) - PullRequest
0 голосов
/ 24 сентября 2019

Моя цель - нарисовать / заполнить треугольник с помощью класса Graphics.Я смог заполнить / нарисовать прямоугольник и круг, но когда я пытаюсь скомпилировать свой код, я получаю ошибку ниже.

Я понимаю, что метод заполнения полигона принимает массивы типов, но я не знаю, какой другой метод использовать, кроме .fillPologyon, который принимает параметры, которые я хочу.

Есть ли другой методчто я не пробовал?Есть ли способ конвертировать мою ширину и высоту для моего прямоугольника, не испуская код для рисования прямоугольника.

DrawArea.java:62: error: no suitable method found for fillPolygon(int,int,int,int)
                    g.fillPolygon(100, 100, width, height);
                     ^
method Graphics.fillPolygon(int[],int[],int) is not applicable
  (actual and formal argument lists differ in length)
method Graphics.fillPolygon(Polygon) is not applicable
  (actual and formal argument lists differ in length)
  1 error

import java.awt.*;
import javax.swing.*;
public class DrawArea extends JComponent {
private int radius;
private int width; // base for rt trig
private int height;
//private int [] w = new int[]
//private int [] h = new int[];
private String shape;

/**
 * constructor for circle
 * @param shape string "circle"
 * @param r the radius the user entered
 */
public DrawArea(String shape, int r){
    this.shape = shape;
    radius = r;
}
/**
 * constructor for rectangle and right triangle
 * @param shape either the string "rectanlge" or "triangle"
 * @param w - either the width or the base
 * @param h - height of rect or tri
 */
public DrawArea(String shape, int w, int h){
    this.shape = shape;
    width = w;
    height = h;


}
/**
 * paint method that draws the selected shape
 */
public void paintComponent(Graphics g){ // Graphic g is and object allows you to set color to green
    removeAll();
    if(shape.equals("circle")){
        //Set color to green
        g.setColor(Color.green);
        // 100 pixels down 100 pixels over
        //How wide and how tall = radius *2 = diamter
        //There broth width and height
        g.fillOval(100, 100, radius * 2, radius * 2);   
    }else if(shape.equals("rectangle")){
        g.setColor(Color.green);
        g.fillRect(100, 100, width, height);   //change  **************************************
    //}else{

    }else if(shape.equals("rtTriangle")){
        g.setColor(Color.green);
        g.fillPolygon(100, 100, width, height);
    }
  }
}

1 Ответ

3 голосов
/ 24 сентября 2019

Вы можете определить свой треугольник как Polygon и использовать fillPolygon (Polygon) например:

Polygon triangle = new Polygon();
triangle.addPoint(40, 55);
triangle.addPoint(60, 55);
triangle.addPoint(50, 35);

g.setColor(Color.green);
g.fillPolygon(triangle);

Теперь просто используйте width и heightпеременные, чтобы определить три точки треугольника, которые вам нужны.

Вот пример точек, которые вы можете использовать:

triangle.addPoint(0, height); // bottom-left angle
triangle.addPoint(width, height); // bottom-right angle
triangle.addPoint(width / 2, 0); // top angle

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...