I'm trying to create a triangle that would be display along side rectangles and circles after I run NervousShapes. I can't seem to get it right. I try doing it with the math but doesn't create the triangle the way I want it. If anyone one has done the NervousShapes program and knows how to create a triangle please help (even if you have not). I know I'm missing something but don't quite get it.

It is this class that's the problem the other class that I have work just fine.

import java.awt.*;

public class Triangle extends Shape {
  // Instance variables
  private int diameter;
  private static final int N_POINTS = 3;
  private static int[] x = new int[3];
  private static int[] y = new int[3];
  

  // Constructor
  public Triangle(int x, int y, Color color, int diameter) {
    super(x, y, color);
    this.diameter = diameter;
  }

  // Instance methods
  public void draw(Graphics g) {
    g.setColor(getColor());
    
   // int[] x = {(getX()/3), getX(), (getX() + (getX()/3))};
   // int[] y = {(getY()+getY()), getY(), (getY()+getY())};
    
    g.fillPolygon(x, y, N_POINTS);
    g.drawPolygon(x, y, N_POINTS);
  }

  public int getHeight() {
    return diameter;
  }

  public int getWidth() {
    return diameter;
  }
  
 private static int getNpoints()
  {
    return N_POINTS;  
  }
  
  }

What can be done with this class?
It needs a main method and a lot more to be able to execute it to see the problem.

the main method is in another class. The other classes work just fine when I run all this classes in eclipse. I put up the rest of my code here. Take a look.

import java.awt.*;
//import jpb.*;

public class NervousShapes {
  // Constants
  private static final int DELAY = 10;
    // Animation delay (milliseconds)
  private static final int MAX_SIZE = 20;
    // Maximum width and height of a shape
  private static final int MIN_SIZE = 10;
    // Minimum width and height of a shape
  private static final int NUM_SHAPES = 50;
    // Number of shapes
  private static final int WINDOW_SIZE = 500;
    // Width and height of drawable portion of frame

  // Class variables
  private static DrawableFrame df;
    // Frame in which shapes are displayed
  private static Graphics g;
    // Graphics context for frame
  private static Shape shapes[] = new Shape[NUM_SHAPES];
    // Array of shapes

  public static void main(String[] args) {
    createWindow();
    createShapes();
    animateShapes();
  }

  ///////////////////////////////////////////////////////////
  // NAME:       createWindow
  // BEHAVIOR:   Creates a frame labeled "Nervous Shapes",
  //             displays the frame, and sets the size of
  //             the frame (using the WINDOW_SIZE class
  //             variable). Assigns the frame to the df
  //             class variable, and assigns the frame's
  //             graphics context to the g class variable.
  // PARAMETERS: None
  // RETURNS:    Nothing
  ///////////////////////////////////////////////////////////
  private static void createWindow() {
    // Create a frame labeled "Nervous Shapes" and set its
    // size
    df = new DrawableFrame("Nervous Shapes");
    df.show();
    df.setSize(WINDOW_SIZE, WINDOW_SIZE);
    df.setLocation(290,140);

    // Get the frame's graphics context
    g = df.getGraphicsContext();
  }

  ///////////////////////////////////////////////////////////
  // NAME:       createShapes
  // BEHAVIOR:   Creates enough Circle and Rectangle objects
  //             to fill the shapes array. Each shape has a
  //             random color, size, and position. The height
  //             and width of each shape must lie between
  //             MIN_SIZE and MAX_SIZE (inclusive). The
  //             position is chosen so that the shape is
  //             completely within the drawing area.
  // PARAMETERS: None
  // RETURNS:    Nothing
  ///////////////////////////////////////////////////////////
  private static void createShapes() {
    for (int i = 0; i < shapes.length; i++) 
    {
      // Select a random color
      int red = generateRandomInt(0, 255);
      int green = generateRandomInt(0, 255);
      int blue = generateRandomInt(0, 255);
      double randomValue;
      Color color = new Color(red, green, blue);
      randomValue = Math.random();

      // Decide whether to create a circle or a rectangle
      if (randomValue < 0.3)  
      {
        // Generate a circle with a random size and position
        int diameter = generateRandomInt(MIN_SIZE, MAX_SIZE);
        int x = generateRandomInt(0, WINDOW_SIZE - diameter);
        int y = generateRandomInt(0, WINDOW_SIZE - diameter);
        shapes[i] = new Circle(x, y, color, diameter);
      } 
      else if (randomValue > 0.3 && randomValue < 0.6) 
      {
        // Generate a rectangle with a random size and
        // position
        int width = generateRandomInt(MIN_SIZE, MAX_SIZE);
        int height = generateRandomInt(MIN_SIZE, MAX_SIZE);
        int x = generateRandomInt(0, WINDOW_SIZE - width);
        int y = generateRandomInt(0, WINDOW_SIZE - height);
        shapes[i] = new Rectangle(x, y, color, width, height);
      }
      else if (randomValue > 0.6)
      {
        int diameter = generateRandomInt (MIN_SIZE, MAX_SIZE);
        int x1 = generateRandomInt (0, WINDOW_SIZE - (diameter + 10));
        int y1 = generateRandomInt (0, WINDOW_SIZE - (diameter + 10));
        shapes[i] = new Triangle (x1,y1,color,diameter);
      }
       
    }
  }

  ///////////////////////////////////////////////////////////
  // NAME:       animateShapes
  // BEHAVIOR:   Establishes an infinite loop in which the
  //             shapes are animated. During each loop
  //             iteration, the drawing area is cleared and
  //             the shapes are then drawn at new positions.
  //             The new x and y coordinates for each shape
  //             will either be the same as the old ones,
  //             one pixel smaller, or one pixel larger. A
  //             shape is not moved if doing so would cause
  //             any portion of the shape to go outside the
  //             drawing area. At the end of each animation
  //             cycle, there is a brief pause, which is
  //             controlled by the delay constant.
  // PARAMETERS: None
  // RETURNS:    Nothing
  ///////////////////////////////////////////////////////////
  private static void animateShapes() {
    while (true) {
      // Clear drawing area
      g.setColor(Color.white);
      g.fillRect(0, 0, WINDOW_SIZE - 1, WINDOW_SIZE - 1);

      for (int i = 0; i < shapes.length; i++) {
        // Change the x coordinate for shape i
        int dx = generateRandomInt(-1, +1);
        int newX = shapes[i].getX() + dx;
        if (newX >= 0 && 
            newX + shapes[i].getWidth() < WINDOW_SIZE)
          shapes[i].move(dx, 0);

        // Change the y coordinate for shape i
        int dy = generateRandomInt(-1, +1);
        int newY = shapes[i].getY() + dy;
        if (newY >= 0 && 
            newY + shapes[i].getHeight() < WINDOW_SIZE)
          shapes[i].move(0, dy);

        // Draw shape i at its new position
        shapes[i].draw(g);
      }

      // Call repaint to update the screen
      df.repaint();

      // Pause briefly
      try {
        Thread.sleep(DELAY);
      } catch (InterruptedException e) {}
    }
  }

  ///////////////////////////////////////////////////////////
  // NAME:       generateRandomInt
  // BEHAVIOR:   Generates a random integer within a
  //             specified range.
  // PARAMETERS: min - the lower bound of the range
  //             max - the upper bound of the range
  // RETURNS:    A random integer that is greater than or
  //             equal to min and less than or equal to max
  ///////////////////////////////////////////////////////////
  private static int generateRandomInt(int min, int max) {
    return (int) ((max - min + 1) * Math.random()) + min;
  }
}
import java.awt.*;
import java.awt.event.*;

public class DrawableFrame extends Frame {
 
  private static final long serialVersionUID = -7655220196707375886L;
  private  Image imageBuffer = null;
  private Insets insets;
  //private serialVersionUID = 1;

  // Constructor
  public DrawableFrame(String title) {
    super(title);
    addWindowListener(new WindowCloser());
  }

  // Called automatically to display the contents of the
  // frame
  public void paint(Graphics g) {
    if (imageBuffer != null)
      g.drawImage(imageBuffer, insets.left, insets.top, null);
  }

  // Called automatically by repaint. Used to reduce flicker.
  public void update(Graphics g) {
    paint(g);
  }

  // Sets the size of the frame. The width and height
  // parameters control the size of the drawable portion of
  // the frame. The frame itself is somewhat larger.
  public void setSize(int width, int height) {
    insets = getInsets();
    super.setSize(width + insets.left + insets.right,
                  height + insets.top + insets.bottom);
    imageBuffer = createImage(width, height);
  }

  // Returns the graphics context associated with the image
  // buffer
  public Graphics getGraphicsContext() {
    return imageBuffer.getGraphics();
  }

  // Listener for window
  class WindowCloser extends WindowAdapter {
    public void windowClosing(WindowEvent evt) {
      System.exit(0);
    }
  }
}
import java.awt.*;

public class Circle extends Shape {
  // Instance variables
  private int diameter;

  // Constructor
  public Circle(int x, int y, Color color, int diameter) {
    super(x, y, color);
    this.diameter = diameter;
  }

  // Instance methods
  public void draw(Graphics g) {
    g.setColor(getColor());
    g.fillOval(getX(), getY(), diameter, diameter);
  }

  public int getHeight() {
    return diameter;
  }

  public int getWidth() {
    return diameter;
  }
}
import java.awt.*;

public abstract class Shape {
  // Instance variables
  private int x;
  private int y;
  private Color color;

  // Constructor
  protected Shape(int x, int y, Color color) {
    this.x = x;
    this.y = y;
    this.color = color;
  }

  // Abstract methods
  public abstract void draw(Graphics g);
  public abstract int getHeight();
  public abstract int getWidth();

  // Other instance methods
  public Color getColor() {
    return color;
  }

  public int getX() {
    return x;
  }

  public int getY() {
    return y;
  }

  public void move(int dx, int dy) {
    x += dx;
    y += dy;
  }

  public void setColor(Color color) {
    this.color = color;
  }
}
import java.awt.*;

public class Rectangle extends Shape {
  // Instance variables
  private int width;
  private int height;

  // Constructor
  public Rectangle(int x, int y, Color color,
                   int width, int height) {
    super(x, y, color);
    this.width = width;
    this.height = height;
  }

  // Instance methods
  public void draw(Graphics g) {
    g.setColor(getColor());
    g.fillRect(getX(), getY(), width, height);
  }

  public int getHeight() {
    return height;
  }

  public int getWidth() {
    return width;
  }
}

I don't need to do any thing in the triangle class except create the triangle. Moving it and drawing it already has been taking care of in the other classes. I tried creating them but they won't come up right.
I used the following x and y and put them in fillpolygon and drawpolygon parameters instead of the two static int variables. It created the triangles but were two big and didn't know how to fix that.

// int[] x = {(getX()/3), getX(), (getX() + (getX()/3))};
// int[] y = {(getY()+getY()), getY(), (getY()+getY())};

g.fillPolygon(x, y, N_POINTS);
g.drawPolygon(x, y, N_POINTS);

It created the triangles but were too big and didn't know how to fix that.

What size do you want the triangles to be? Take a piece of graph paper, draw some triangles and work out how to compute the size that you want.
How do the other class compute their sizes?

This article has been dead for over six months. Start a new discussion instead.