2017-02-10 113 views
0

我想创建两个三角形,一个倒过来,另一个倒上。但是,该程序仅绘制第一个三角形。我究竟做错了什么?试图绘制2个三角形

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

你是否在任何地方调用'newTriangle'?如果没有,那就有你的答案。 – weston

+0

但我不会在任何地方调用油漆,它仍然绘制三角形。 – Hundo

+0

'paint'由另一个知道该方法的类调用,因为它在'Applet'中声明。没有其他班级对你的方法有任何了解,这是全新的,所以没有人会这样称呼它。 – weston

回答

0

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