我正在尝试创建两个三角形,其中一个是倒置的,另一个是另一个。但是,程序只绘制第一个三角形。我做错了什么?
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 :(得分: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);
}
}