-9

all! I appear to be having a problem with integrating a triangle shape to appear! This is the main:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


// Class definition

public class Assignment extends JPanel implements ActionListener {

private JButton moveLeft, moveRight, makeBigger, makeSmaller;
private JComboBox shapeChoice;
private Point firstPoint;
private Circle firstCircle;
private Square firstSquare;
private Doughnut firstDoughnut;
private Point aShape;
private Triangle firstTriangle;
private JLabel cat;


// Constructor method declaration

public Assignment() {
    cat = new JLabel("Area:");
    add(cat);
    moveLeft = new JButton("Move Left");
    add(moveLeft);
    moveLeft.addActionListener(this);

    moveRight = new JButton("Move Right");
    add(moveRight);
    moveRight.addActionListener(this);

    makeBigger = new JButton("Make Bigger");
    add(makeBigger);
    makeBigger.addActionListener(this);

    makeSmaller = new JButton("Make Smaller");
    add(makeSmaller);
    makeSmaller.addActionListener(this);

    shapeChoice = new JComboBox();
    shapeChoice.addItem("Point");
    shapeChoice.addItem("Square");
    shapeChoice.addItem("Circle");
    shapeChoice.addItem("Doughnut");
    shapeChoice.addItem("Triangle");
    add(shapeChoice);
    shapeChoice.addActionListener(this);


    // Instantiate shape classes
    firstPoint = new Point();
    firstCircle = new Circle();
    firstSquare = new Square();
    firstDoughnut = new Doughnut();
    firstTriangle = new Triangle();
    // Initially assign aShape a Point object
    aShape = firstPoint;

    // Create a new window using the Swing class JFrame and add this panel
    makeFrame();
}

public void actionPerformed(ActionEvent e) {
    // Manipulate respective object when buttons pressed
    if (e.getSource() == moveLeft) {
          aShape.moveXY(-20, 0);
    }

   else if (e.getSource() == moveRight) {
          aShape.moveXY(20, 0);
    }

    else if (e.getSource() == makeBigger) {
          aShape.reSize(20);
    }

    else if (e.getSource() == makeSmaller) {
          aShape.reSize(-20);
    }

    // checkbox assigns object to aShape
    else if (e.getSource() == shapeChoice) {
        if (shapeChoice.getSelectedItem().equals("Circle"))
            aShape = firstCircle;

        else if (shapeChoice.getSelectedItem().equals("Square"))
            aShape = firstSquare;
        else if (shapeChoice.getSelectedItem().equals("Point"))
            aShape = firstPoint;
        else if (shapeChoice.getSelectedItem().equals("Doughnut"))
            aShape = firstDoughnut;
        else if (shapeChoice.getSelectedItem().equals("Triangle"))
            aShape = firstTriangle;
    }


    // Ask Java to repaint the window to reflect the updates made
    repaint();
}

// Provide this method to instruct Java what to do when repainting the window
public void paintComponent(Graphics g) {

    super.paintComponent(g);

    // Call the respective methods from respective class to print the new co-ordinate values
    aShape.outputXYCoords(g);

    // Call the respective methods from respective class to redraw the shape
    aShape.display(g);
}


// Create a window frame

public void makeFrame() {

    // Instantiate a window frame using Swing class JFrame
    JFrame frame = new JFrame("PolyMorphDemo");

    // When the window is closed terminate the application
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // set initial size of window
    frame.setSize(800, 600);

    // add the current object to the centre of the frame and make visible
    frame.getContentPane().add(this, BorderLayout.CENTER);
    frame.setVisible(true);
}   

}

This is the triangle class import java.awt.*;

// Class definition
public class Triangle extends Point {
protected int size;

// Constructor method declaration
public Triangle() {
    super();
    // set size and offset y co-ordinate to display below point
    size = 50;
    yPos = 100;
    xPos = 100;
}

// Method to update new size of the circle
public void reSize(int newSize) {
    size = size + newSize;
}

// An overiding method to display circle on screen as inherited method from class Point not suitable
public void display(Graphics g) {
    // Call method fillRect from class Graphics to draw the square of variable length of size 
    g.fillPolygon(new int[]{xPos, xPos+size, xPos+(2*size)}, new int[]{yPos, yPos+size, yPos+(2*size)},3);
}
}

If anybody can tell me what exactly I might be doing wrong or what could be the problem I'd be extremely thankful!

Here is the Point class: import java.awt.*;

// Class definition
public class Point extends Shape
{

protected int xPos, yPos;
protected int startX, startY, endX, endY;
// Constructor method declaration

public Point() {
    xPos = 100;
    yPos = 100;
}

// Method to update new position of point on screen
public void moveXY(int newX, int newY) {
    xPos = xPos +newX;
    yPos = yPos +newY;
}

// Cannot resize a point but method required to support polymorphism
public void reSize(int newSize) {
}

// Method to print X,Y co-ordinates of point on screen
public void outputXYCoords(Graphics g) {
    g.drawString("X Coord = " + xPos + 
            "   Y Coord = " + yPos, 5, yPos-10);
}

// Method to draw point on screen at new X.Y position
public void display(Graphics g) {
    g.fillOval(xPos, yPos, 5, 5);
}
}

Sorry for the lack of explanation, a bit tired. Basically the program starts up. It shows the Circle/Square/Doughnut options perfectly and they move around. And when I select Triangle from the dropdown menu, nothing shows up. It just stays empty.

user3186187
  • 37
  • 1
  • 2
  • 7

1 Answers1

4

The basic problem, is your Triangle class is generating a stright line...

g.fillPolygon(
        new int[]{xPos, xPos + size, xPos + (2 * size)}, 
        new int[]{yPos, yPos + size, yPos + (2 * size)}, 3);

So, based on the values you have supplied, this would produce...

  1. 100x100
  2. 150x150
  3. 200x200

Instead, you could use something like...

g.fillPolygon(
        new int[]{xPos, xPos + size, xPos - size},
        new int[]{yPos, yPos + size, yPos + size}, 3);

Which would produce...

  1. 100x100
  2. 150x150
  3. 50x150

Which would generate something like...

Triangle

MadProgrammer
  • 323,026
  • 21
  • 204
  • 329