2017-02-10 8 views
0

2つの三角形を上下逆さに作成しようとしています。しかし、プログラムは最初の三角形だけを描いています。私は間違って何をしていますか?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); 
    } 
} 
関連する問題