I wrote this code in java and now i have to add a GUI to draw Cube,Square and Ci
ID: 3590456 • Letter: I
Question
I wrote this code in java and now i have to add a GUI to draw Cube,Square and Circle object on the Drawing Panel. I should add the paint functions of the classes Circle, Cube and Square.I should add position information on the panel, abstract paint function and also update the mSide and mRadius types to int from float according to needs of this new program
Run Scenario is:
Select an object on the toolbar
Click a point on the blue drawing panel
Selected object is drawn on the drawing panel:
Circle center at the clicked point
Square top-left corner at the clicked point
Cube front-top-left corner at the clicked poin
Here is my code, Could you please help me add those GUI and paint functions to my program?
Assignment3.java class
import java.awt.Color;
public class Assignment3
{
public static void main(String[] args)
{
Circle circle = new Circle(Color.RED, "myCircle", 2.f);
circle.print();
System.out.println("Area : "+circle.computeArea());
System.out.println("Perimeter : "+circle.computePerimeter());
Square square = new Square(Color.BLUE, "mySquare", 3.5f);
square.print();
System.out.println("Area : "+square.computeArea());
System.out.println("Perimeter : "+square.computePerimeter());
Cube cube = new Cube(Color.CYAN, "myCube", 2.3f);
cube.print();
System.out.println("Volume : "+cube.computeVolume());
}
}
Shape.java class
import java.awt.Color;
abstract class Shape
{
public Color mColor;
public String mName;
Shape(Color mColor, String mName)
{
this.mColor = mColor;
this.mName = mName;
}
public String getName()
{
return mName;
}
public Color getColor()
{
return mColor;
}
public void setName(String name)
{
mName = name;
}
public void setColor(Color color)
{
mColor = color;
}
public abstract void print();
}
Cube.java class
import java.awt.Color;
public class Cube extends Square
{
Cube(Color c, String n, float s)
{
super(c,n,s);
}
public float computeVolume()
{
return mSide*mSide*mSide;
}
public void print()
{
System.out.println("Type of the Shape is Cube");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is side ");
}
}
Square.java class
import java.awt.Color;
public class Square extends Shape
{
public float mSide;
public Square(Color c, String n, float s)
{
super(c,n);
mSide = s;
}
public float computeArea()
{
return mSide*mSide;
}
public float computePerimeter()
{
return 4 * mSide;
}
public void print()
{
System.out.println("Type of the Shape is Square");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is side ");
}
}
Circle.java class
import java.awt.Color;
import java.lang.Math;
public class Circle extends Shape
{
public float mRadius;
public Circle(Color c, String n, float r)
{
super(c,n);
mRadius = r;
}
public double computeArea()
{
return Math.PI*mRadius*mRadius;
}
public double computePerimeter()
{
return 2*Math.PI*mRadius;
}
public void print()
{
System.out.println("Type of the Shape is Circle");
//System.out.println("Name of the Shape is: "+getName());
System.out.println("Name of the Shape: "+ mName);
System.out.println("The parameter of the Shape is radius ");
}
}
Explanation / Answer
Assignment.java::
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;
public class Assignment3
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Shape GUI");
JToolBar tb = new JToolBar();
JButton circl = new JButton("Circle");
circl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) { //Check ouse clicked after Button Clicked
frame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
Circle c1=new Circle(Color.YELLOW,frame,e.getX(),e.getY());
c1.paint();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
}
});
JButton cub = new JButton("Cube");
cub.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
frame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
Cube c1=new Cube(Color.PINK,frame,e.getX(),e.getY());
c1.paint();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
}
});
JButton sq = new JButton("Square");
sq.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
frame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
Square c1=new Square(Color.GREEN,frame,e.getX(),e.getY());
c1.paint();
}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
});
}
});
JButton qt = new JButton("Quit");
qt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
frame.dispose(); //Close Frame
}
});
tb.add(circl);
tb.add(cub);
tb.add(sq);
tb.add(qt);
frame.setLayout(new BorderLayout());
frame.getContentPane().add(tb, BorderLayout.PAGE_START);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setVisible(true); //make frame visible
}
}
Shape.java::
import java.awt.Color;
import javax.swing.JFrame;
abstract class Shape
{
public Color mColor;
public JFrame frame;
public int x;
public int y;
Shape(Color mColor, JFrame frame,int x,int y)
{
this.mColor = mColor;
this.frame = frame;
this.x=x;
this.y=y;
}
public abstract void paint();
}
Cube.java
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Cube extends Square
{
public int side;
Cube(Color c,JFrame frame,int x,int y)
{
super(c,frame,x,y);
side=50;
}
public void paint()
{
Graphics g=frame.getGraphics();
g.setColor(mColor);
g.draw3DRect(x, y, side,side,ftrue);
}
}
Square.java::
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Square extends Shape
{
public int mSide;
public Square(Color c,JFrame frame,int x,int y)
{
super(c,frame,x,y);
mSide = 50;
}
public void paint()
{
Graphics g=frame.getGraphics();
g.setColor(Color.BLACK);
g.drawRect(x, y, mSide, mSide);
g.setColor(mColor);
g.fillRect(x, y, mSide, mSide);
}
}
Circle.java::
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Circle extends Shape
{
public int mRadius;
public Circle(Color c,JFrame frame,int x,int y)
{
super(c,frame,x,y);
this.mRadius=50;
}
public void paint()
{
Graphics g=frame.getGraphics();
g.setColor(Color.BLACK);
g.drawOval(x, y, mRadius, mRadius);
g.setColor(mColor);
g.fillOval(x, y, mRadius, mRadius);
}
}