【问题标题】:How to fill a triangle using Graphics class (Java)如何使用图形类(Java)填充三角形
【发布时间】:2019-09-24 07:46:33
【问题描述】:

我的目标是能够使用 Graphics 类绘制/填充三角形。我能够填写/绘制一个矩形和一个圆形,但是当我尝试编译我的代码时,我得到了下面的错误。

我知道填充多边形方法接受类型数组,但我不知道除了接受我想要的参数的 .fillPologyon 之外还可以使用什么其他方法。

还有其他我没有尝试过的方法吗?有没有办法为我的 rttriangle 转换我的宽度和高度,同时又不会弄乱绘制矩形的代码。

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);
    }
  }
}

【问题讨论】:

    标签: java


    【解决方案1】:

    您可能希望将三角形定义为 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);
    

    现在只需使用 widthheight 变量,根据需要定义三角形的三个点。

    这是您可以使用的点的示例:

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

    【讨论】:

    • 多边形三角形 = new Polygon();三角形.addPoint(宽度,高度); g.setColor(Color.green); g.fillPolygon(三角形); //g.fillPolygon(100, 100, width, height);
    • 所以我尝试将上面的代码放在 else if(shape.equals("rtTriangle")){ }
    • 但是三角形还是没有出现或者被填满了。
    • 它是一个三角形,它需要3个点而不仅仅是1个,否则它甚至不是一个有效的形状。每个点都是三角形一个角的位置。
    • 我没明白你的意思?
    猜你喜欢
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    相关资源
    最近更新 更多