【问题标题】:Trying to draw 2 triangles尝试绘制 2 个三角形
【发布时间】:2017-06-29 10:48:05
【问题描述】:

我正在尝试创建两个三角形,其中一个倒置并在另一个之上。但是,该程序仅绘制第一个三角形。我做错了什么?

   public class Triangle extends Applet {


  public void paint( Graphics g ) {

    int[] xPoints = {10, 260, 135};
    int[] yPoints = {250, 250, 10};
    int numPoints = 3;
    // Set the drawing color to black
    g.setColor(Color.black);
    // Draw a filled in triangle
    g.fillPolygon(xPoints, yPoints, numPoints );  

 }


    public void newTriangle( Graphics h ) {

    int[] xPoints2 = {135, 395/2, 145/2};
    int[] yPoints2 = {250, 130, 130};
    int n = 3;

    h.setColor(Color.white);

    h.fillPolygon(xPoints2, yPoints2, n);
  }
}

【问题讨论】:

  • 你在哪里打电话给newTriangle?如果没有,这就是你的答案。
  • 但是我也没有在任何地方调用paint,它仍然绘制三角形。
  • paint 被另一个知道该方法的类调用,因为它在Applet 中声明。没有其他班级对您的方法有任何了解,它是全新的,所以没有人调用它。
  • 那么我如何在同一个类中调用它?
  • 看我的回答

标签: java applet awt graphics2d


【解决方案1】:

paint 被另一个知道该方法的类调用,因为它在Applet 中声明。没有其他类对您的方法有任何了解,它是全新的,因此没有代码调用它。如果你想调用它,你必须自己做:

public class Triangle extends Applet {

  @Override   //this is good practice to show we are replacing the ancestor's implementation of a method
  public void paint(Graphics g) {
    int[] xPoints = {10, 260, 135};
    int[] yPoints = {250, 250, 10};
    int numPoints = 3;
    // Set the drawing color to black
    g.setColor(Color.black);
    // Draw a filled in triangle
    g.fillPolygon(xPoints, yPoints, numPoints );  

    newTriangle(g); //call your method
 }

 public void newTriangle(Graphics h) {
    int[] xPoints2 = {135, 395/2, 145/2};
    int[] yPoints2 = {250, 130, 130};
    int n = 3;

    h.setColor(Color.white);

    h.fillPolygon(xPoints2, yPoints2, n);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2023-03-17
    • 2020-12-08
    相关资源
    最近更新 更多