Hey, I have a lab to do, but I'm stuck on one part of it which is probably really easy but I can't figure it out. This is what I have so far, but it doesn't compile, and then I have to use polymorphism to output it. The purpose of this method is to truly demonstrate polymorphism since one output method will be capable of displaying both the shape and the number of sides of ANY class that implements Shape.

import java.util.Random;
import java.awt.*;
import java.awt.event.*;


public class Lab21a {
    @SuppressWarnings("deprecation")
    public static void main(String args[]) {
        Windows win = new Windows();
        win.setSize(800, 600);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        win.show();
    }
}

@SuppressWarnings("serial")
class Windows extends Frame {

    public void paint(Graphics g) {  
        drawGrid(g);  

        Square shape1 = new Square(g);  
        Triangle shape2 = new Triangle(g);  
        Octagon shape3 = new Octagon(g);  
        Circle shape4 = new Circle(g);  

        output(g, shape1);  
        output(g, shape2);  
        output(g, shape3);  
        output(g, shape4);  
    } 

    public void drawGrid(Graphics g) {
        g.drawLine(0, 300, 800, 300);
        g.drawLine(400, 0, 400, 600);
        g.drawString("Square", 20, 50);
        g.drawString("Triangle", 420, 50);
        g.drawString("Octagon", 20, 320);
        g.drawString("Circle", 420, 320);
    }

}

abstract interface Shape {
    public final double PI = Math.PI;

    public abstract void drawShape(Graphics g);

    public abstract void displayNumSides(Graphics g);

}
abstract class Shape2 implements Shape{

    private int numSides;
    private String shapeName;
    private Graphics g;
    private int messageX, messageY;
    public void displayNumSides(Graphics g) {
        g.drawString("A " + shapeName + " has " + numSides + " sides.",
            messageX, messageY);
    }
class Square{
    public Square(Graphics g1) {
        numSides = 4;
        shapeName = "Square";
        g = g1;
        messageX = 100;
        messageY = 250;
        }

    public void drawShape(Graphics g) {
        g.fillRect(100, 50, 150, 150);
    }
}
class Triangle {
    public Triangle(Graphics g1) {
        numSides = 3;
        shapeName = "Triangle";
        g = g1;
        messageX = 500;
        messageY = 250;
    }

    public void drawShape(Graphics g) {
        Polygon poly = new Polygon();
        poly.addPoint(600, 50);
        poly.addPoint(700, 200);
        poly.addPoint(500, 200);
        g.fillPolygon(poly);
    }
}

class Octagon {
    public Octagon(Graphics g1) {
        numSides = 8;
        shapeName = "Octagon";
        g = g1;
        messageX = 100;
        messageY = 550;
    }

    public void drawShape(Graphics g) {
        Polygon poly = new Polygon();
        poly.addPoint(100, 400);
        poly.addPoint(150, 350);
        poly.addPoint(200, 350);
        poly.addPoint(250, 400);
        poly.addPoint(250, 450);
        poly.addPoint(200, 500);
        poly.addPoint(150, 500);
        poly.addPoint(100, 450);
        g.fillPolygon(poly);
    }
}
class Circle {
    public Circle(Graphics g1) {
        numSides = 0;
        shapeName = "Circle";
        g = g1;
        messageX = 500;
        messageY = 550;
    }
    public void drawShape(Graphics g) {
        g.fillOval(500, 350, 150, 150);
    }
}
}

Recommended Answers

All 8 Replies

  1. If you get an error ALWAYS post the complete error message here. Don't exp3ect people to guess.
  2. You are using AWT (Frame) rather than Swing (JFrame). AWT has been obsolete for over a decade now. You should update your code to use Swing components - it's not hard.
  3. Your interface has two abstract methods so you MUST define both those methods in any non-abstract class that implements the interface, even if you are only using one of them.

u have a interface name Shape,
next you created an abstract class named Shape2 that implement the interface,
next you created 4 classes (Square, Triangle, Octagon,Circle) that should extends the abstract class,
so this is the first error i think, you should replace "class Square" with "class Square extends Shape2" and the same for the 3 others class.
and i'm not sure what the methods in line 31,32,33,34, if you mean to draw the shapes i think you should use the method from the abstract class
shape1.drawShape(g), and the same for the other shapes. I hope this help.

Well the error is really long, but in Eclipse, the only thing that it tells me is that it says Square, Triabgle, Circle and Octagon are not defined in line 29-30.

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems: 
    Square cannot be resolved to a type
    Square cannot be resolved to a type
    Triangle cannot be resolved to a type
    Triangle cannot be resolved to a type
    Octagon cannot be resolved to a type
    Octagon cannot be resolved to a type
    Circle cannot be resolved to a type
    Circle cannot be resolved to a type

    at Windows.paint(Lab21a.java:30)
    at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
    at sun.lwawt.LWRepaintArea.paintComponent(LWRepaintArea.java:54)
    at sun.awt.RepaintArea.paint(RepaintArea.java:240)
    at sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1277)
    at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1165)
    at java.awt.Component.dispatchEventImpl(Component.java:4937)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

looks like you forgot the import statements for the types
Square, Triangle, Octagon and Circle.

I added this statement import Shape2.Circle;
but it highlights Shape2 in red and it says "The import Shape2 cannot be resolved"
And it still doesn't compile.

It's nothing to do with imports - the classes are defined in your source file.
@hannahaddad told you what to do a day ago. Until you follow that advice you will remain stuck.

I made those changes, and I extended all of the classes and used the drawShape method, but it still doesn't compile. I honestly don't know what to do from here. This is my new line 26-29

public void paint(Graphics g) {  
        drawGrid(g);  

        Square shape1 = new Square(g);  
        shape1.drawShape(g);
        Triangle shape2 = new Triangle(g);  
        shape2.drawShape(g);
        Octagon shape3 = new Octagon(g);  
        shape3.drawShape(g);
        Circle shape4 = new Circle(g);  
        shape4.drawShape(g);

"it still doesn't compile" tells us nothing. Post the complete exact error messages!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.