我想填补一个三角形

时间:2023-02-09 21:37:56

I am trying to fill a triangle. I tried to do it myself using

我想填补一个三角形。我试着用自己做的

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

but I am getting some errors. Can anyone help me with this?

但是我遇到了一些错误。谁能帮我这个?

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 个解决方案

#1


2  

You're welcome and I suggest to use oracle java api sometimes. It's really developers friendly. :)

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

You don't have any Triangle class with static x1... fields so why Triangle.x1...? :) And why own block to invoke triangle()?

你没有任何带有静态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();
    }

}

#2


2  

nvrfrgt answer works and fixes any bugs you have (gave him a +1) but there are some other things you could clean up that are worth keeping in mind (and were too long for a comment).

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

First off, I think you meant for HEIGHT to be final? Otherwise make it lowercase.

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

A performance thing that might make a difference later if you are drawing a bunch of racing triangles or something - you are drawing the same triangle twice (never change your x and y values), so one of these is wasted.

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

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

How you initialized your x and y values isn't the cleanest either. You set them all to specific numbers, and then turn around and change them in the next line. Why have the initial values? The snippet below makes more sense to me anyway (though that still seems a bit too magic numbery)

如何初始化x和y值也不是最干净的。您将它们全部设置为特定数字,然后转向并在下一行中更改它们。为什么有初始值?无论如何,下面的片段对我来说更有意义(虽然这似乎有点太神奇了)

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

If you are going to draw the triangle and fill it in the same color, then drawing the lines might be unnecessary for you (makes 1pixel difference?). If that's the case, you can remove the triangle() call, or replace your triangle implementation with the fill polygon. In the end, something like this is a bit more sleek (still can work on it though):

如果您要绘制三角形并用相同的颜色填充它,那么绘制线条可能对您来说是不必要的(使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();
  }

#3


0  

first set color:

第一套颜色:

g.setColor(R, G, B);

Then fill it:

然后填写:

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

#1


2  

You're welcome and I suggest to use oracle java api sometimes. It's really developers friendly. :)

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

You don't have any Triangle class with static x1... fields so why Triangle.x1...? :) And why own block to invoke triangle()?

你没有任何带有静态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();
    }

}

#2


2  

nvrfrgt answer works and fixes any bugs you have (gave him a +1) but there are some other things you could clean up that are worth keeping in mind (and were too long for a comment).

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

First off, I think you meant for HEIGHT to be final? Otherwise make it lowercase.

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

A performance thing that might make a difference later if you are drawing a bunch of racing triangles or something - you are drawing the same triangle twice (never change your x and y values), so one of these is wasted.

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

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

How you initialized your x and y values isn't the cleanest either. You set them all to specific numbers, and then turn around and change them in the next line. Why have the initial values? The snippet below makes more sense to me anyway (though that still seems a bit too magic numbery)

如何初始化x和y值也不是最干净的。您将它们全部设置为特定数字,然后转向并在下一行中更改它们。为什么有初始值?无论如何,下面的片段对我来说更有意义(虽然这似乎有点太神奇了)

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

If you are going to draw the triangle and fill it in the same color, then drawing the lines might be unnecessary for you (makes 1pixel difference?). If that's the case, you can remove the triangle() call, or replace your triangle implementation with the fill polygon. In the end, something like this is a bit more sleek (still can work on it though):

如果您要绘制三角形并用相同的颜色填充它,那么绘制线条可能对您来说是不必要的(使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();
  }

#3


0  

first set color:

第一套颜色:

g.setColor(R, G, B);

Then fill it:

然后填写:

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