0

I am trying to create two triangles with one being upside down and on top of the other. However, the program is only painting the first triangle. What am I doing wrong?

   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);
  }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Hundo
  • 13
  • 4
  • Are you calling `newTriangle` anywhere? If not, there's your answer. – weston Feb 10 '17 at 23:36
  • But I do not call paint anywhere either and it still draws the triangle. – Hundo Feb 10 '17 at 23:38
  • `paint` is called by another class which knows about that method because it's declared in `Applet`. No other class has any knowledge of your method, it's brand new, so no one is calling it. – weston Feb 10 '17 at 23:40
  • So how would I call it inside of that same class? – Hundo Feb 10 '17 at 23:45
  • Look at my answer – weston Feb 10 '17 at 23:45
  • 1) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Feb 12 '17 at 21:08

1 Answers1

0

paint is called by another class which knows about that method because it's declared in Applet. No other class has any knowledge of your method, it's brand new, so no code is calling it. If you want it to be called you'll have to do it yourself:

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);
  }
}
weston
  • 54,145
  • 21
  • 145
  • 203