我想填补一个三角形

时间:2012-05-31 19:24:38

标签: java geometry shape fill

我想填补一个三角形。我尝试使用

自己做
g.fillPolygon(Triangle.x1, Triangle.x2, Triangle.x3, Triangle.y1, 
         Triangle.y2, Triangle.y3); 

但是我遇到了一些错误。任何人都可以帮我这个吗?

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Vehicle extends JFrame

 {
    final int WIDTH = 900; int HEIGHT = 650;



public Vehicle()
    {
        //the following code creates the JFrame
        super("Radical Racing");
        setSize(WIDTH,HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

}
public void paint(Graphics g)
{

    g.setColor(Color.DARK_GRAY);
    g.fillRect(0,0,WIDTH,HEIGHT);


    int x1, y1 ,x2, y2, x3,y3;
    x1 = x2 = x3 = 200;
    y1 = y2 = y3 = 390;

    {
          triangle (g, x1=174, y1, x2=204, y2, x3=189, y3=360);

    }
    g.setColor(Color.green);
    g.fillPolygon(Triangle.x1, Triangle.x2, Triangle.x3, Triangle.y1, Triangle.y2, Triangle.y3);
    triangle(g,x1,y1,x2,y2,x3,y3);
}

void triangle(Graphics g,int x1,int y1,
        int x2, int y2, int x3, int y3){
      g.drawLine (x1, y1, x2, y2);
      g.drawLine (x2, y2, x3, y3);
      g.drawLine (x3, y3, x1, y1);
}



public static void main(String[]args)
    {
        new Vehicle();
    }

}

3 个答案:

答案 0 :(得分:2)

欢迎您,我建议有时使用oracle java api。这真的是开发人员的友好。 :)

你没有任何带有静态x1 ...字段的Triangle类,那么为什么Triangle.x1 ......? :)为什么自己的块调用triangle()?

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Vehicle extends JFrame {

    final int WIDTH = 900;
          int HEIGHT = 650;

    public Vehicle() {
        // the following code creates the JFrame
        super("Radical Racing");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

    }
    public void paint(Graphics g) {

        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        int x1, y1, x2, y2, x3, y3;
        x1 = x2 = x3 = 200;
        y1 = y2 = y3 = 390;
        triangle(g, x1 = 174, y1, x2 = 204, y2, x3 = 189, y3 = 360);

        g.setColor(Color.green);
        g.fillPolygon(new int[] {x1, x2, x3}, new int[] {y1, y2, y3}, 3);
        triangle(g, x1, y1, x2, y2, x3, y3);
    }

    void triangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) {
        g.drawLine(x1, y1, x2, y2);
        g.drawLine(x2, y2, x3, y3);
        g.drawLine(x3, y3, x1, y1);
    }

    public static void main(String[] args) {
        new Vehicle();
    }

}

答案 1 :(得分:2)

nvrfrgt回答可以解决你遇到的任何错误(给他一个+1),但还有其他一些你可以清理的东西值得记住(而且评论太长了)。

首先,我认为你的意思是HEIGHT是最终的?否则将其设为小写。

如果您正在绘制一堆赛车三角形或其他东西,以后可能会产生影响的性能事情 - 您正在绘制相同的三角形两次(从不更改您的x和y值),因此浪费了其中一个。

triangle (g, x1=174, y1, x2=204, y2, x3=189, y3=360);
...
triangle(g, x1, y1, x2, y2, x3, y3);

如何初始化x和y值也不是最干净的。您将它们全部设置为特定数字,然后转向并在下一行中更改它们。为什么有初始值?下面的代码段对我来说更有意义(尽管看起来仍然有点magic numbery

int x1 = 174; int x2 = 204; int x3 = 189;
int y1 = 390; int y2 = 390; int y3 = 360;
triangle(g, x1, y1, x2, y2, x3, y3);

如果您要绘制三角形并用相同的颜色填充,那么绘制线条可能对您来说是不必要的(使1像素不同?)。如果是这种情况,您可以删除triangle()调用,或用填充多边形替换三角形实现。最后,像这样的东西更加圆滑(虽然仍可以使用):

public class Vehicle extends JFrame {

  final int WIDTH = 900;
  final int HEIGHT = 650; // Made this final

  public Vehicle() {
      // the following code creates the JFrame
      super("Radical Racing");
      setSize(WIDTH, HEIGHT);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
  }

  public void paint(Graphics g) {

    g.setColor(Color.DARK_GRAY);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    int x1 = 174; int x2 = 204; int x3 = 189;
    int y1 = 390; int y2 = 390; int y3 = 360;
    g.setColor(Color.green);
    // Got rid of redundant drawing
    triangle(g, x1, y1, x2, y2, x3, y3);
  }

  void triangle(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3) {
    // Left both the lines and the fill so you could play with color or something.
    g.drawLine(x1, y1, x2, y2);
    g.drawLine(x2, y2, x3, y3);
    g.drawLine(x3, y3, x1, y1);
    g.fillPolygon(new int[] {x1, x2, x3}, new int[] {y1, y2, y3}, 3);
  }

  public static void main(String[] args) {
    // Want to save this as something so you can move it around later
    Vehicle greenTriangle = new Vehicle();
  }

答案 2 :(得分:0)

首先设置颜色:

g.setColor(R, G, B);

然后填写:

g.fillTriangle(x1, y1, x2, y2, x3, y3);