Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal is to practice: How to reuse previously implemented classes Observe how

ID: 3589226 • Letter: T

Question

The goal is to practice:

How to reuse previously implemented classes

Observe how to use Graphics to Draw on JPanel

To extract java files from a jar file

Observe how to use mouse events

You are going to implement a GUI to draw certain objects on the Drawing Panel. I provided you the toolbar, panel creation piece of code, hence all you need to do is implement the paint functions of the classes Circle, Cube and Square. Also, please explore in detail the project. Observe how OO paradigm is applied and how it makes the life easy in terms of modularity, reusability and maintenance.

In the implementation, I reused my solution to the 3rd Assignment, and modified it tiny-bit. I added position information on the panel, abstract paint function and also updated the mSide and mRadius types to int from float according to needs of the current assignment.

A small note: I used a black boundary color for the square and the circle, but it is completely optional.

Run Scenario:

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 point

// Assignment4.java

package ShareWithStudents;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.border.EtchedBorder;

public class Assignment4

extends JFrame
implements MouseListener
{
final int NUMBER_OF_SHAPES = 3;
  
JToolBar mToolbar;
ShapeSelected mSelectedAction;
JToggleButton mCircleButton;
JToggleButton mCubeButton;
JToggleButton mSquareButton;
DrawPanel mDrawPanel;
static ArrayList mShapes = new ArrayList();
  
public static void main(String[] args)
{
new Assignment4();
}
  
private JToggleButton addButton(String title, final ShapeSelected shapeSelected)
{
JToggleButton button = new JToggleButton(title);
button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
if (ev.getStateChange() == 1)
{

switch (Assignment4.3.$SwitchMap$ShareWithStudents$ShapeSelected[mSelectedAction.ordinal()]) {
case 1:
mCircleButton.setSelected(false);
break;
case 2:
mCubeButton.setSelected(false);
break;
case 3:
mSquareButton.setSelected(false);
}
  
  

mSelectedAction = shapeSelected;
}
else if (ev.getStateChange() == 2) {
mSelectedAction = ShapeSelected.None;
}
  
}
});
mToolbar.add(button);
  
return button;
}
  

public Assignment4()
{
int WIDTH = 1000;
int HEIGHT = 800;
  
mSelectedAction = ShapeSelected.None;
  
JFrame guiFrame = new JFrame();
  

guiFrame.setDefaultCloseOperation(3);
guiFrame.setTitle("SHAPE GUI");
guiFrame.setSize(1000, 800);
  

guiFrame.setLocationRelativeTo(null);
  

mToolbar = new JToolBar();
mToolbar.setBorder(new EtchedBorder());
  
mDrawPanel = new DrawPanel();
  
mDrawPanel.setBackground(Color.blue);
mDrawPanel.paintDisplay(mShapes);
  
guiFrame.add(mDrawPanel);
guiFrame.add(mToolbar, "North");
  
mCircleButton = addButton("Circle", ShapeSelected.Circle);
mCubeButton = addButton("Cube", ShapeSelected.Cube);
mSquareButton = addButton("Square", ShapeSelected.Square);
  

JButton button = new JButton("Quit");
button.setToolTipText("Quit the program");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mToolbar.add(button);
  
guiFrame.addMouseListener(this);
  

guiFrame.setVisible(true);
}
  

public void mouseClicked(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
  
switch (3.$SwitchMap$ShareWithStudents$ShapeSelected[mSelectedAction.ordinal()]) {
case 1:
Circle circle = new Circle(Color.yellow, "crl", 50);
circle.setPos(x, y - 50);
mShapes.add(circle);
break;
case 2:
Cube cube = new Cube(Color.red, "cb", 100);
cube.setPos(x, y - 50);
mShapes.add(cube);
break;
case 3:
Square square = new Square(Color.green, "sq", 100);
square.setPos(x, y - 50);
mShapes.add(square);
}
  
  
mDrawPanel.paintDisplay(mShapes);
}
  
public void mousePressed(MouseEvent e) {}
  
public void mouseReleased(MouseEvent e) {}
  
public void mouseEntered(MouseEvent e) {}
  
public void mouseExited(MouseEvent e) {}
}

// Circle.java

package ShareWithStudents;

import java.awt.Color;
import java.awt.Graphics;
import java.io.PrintStream;

public class Circle
extends Shape
implements I_twoD
{
private int mRadius;
  
public Circle(Color color, String name, int radius)
{
super(color, name);
mRadius = radius;
mX = -1;
mY = -1;
}
  
public float computeArea()
{
return (float)(3.141592653589793D * mRadius * mRadius);
}
  
public float computePerimeter()
{
return (float)(6.283185307179586D * mRadius);
}
  
public void print()
{
System.out.println("Circle (" + mName + ") radius : " + mRadius + " and color : " + mColor.toString());
}
  
public void paint(Graphics g) {}
}

// Cube.java

package ShareWithStudents;

import java.awt.Color;
import java.awt.Graphics;
import java.io.PrintStream;


public class Cube
extends Square
implements I_threeD
{
public Cube(Color color, String name, int side)
{
super(color, name, side);
}
  

public float computeVolume()
{
return mSide * mSide * mSide;
}
  
public void print() {
System.out.println("Cube (" + mName + ") side : " + mSide + " and color : " + mColor.toString());
}
  
public void paint(Graphics g)
{
int step = 20;
}
}

// DrawPanel.java

package ShareWithStudents;

import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;


class DrawPanel
extends JPanel
{
ArrayList mShapes;
  
DrawPanel() {}
  
public void paintComponent(Graphics g)
{
super.paintComponent(g);
  
for (Shape s : mShapes) {
s.paint(g);
}
}
  
public void paintDisplay(ArrayList shapes) {
mShapes = shapes;
repaint();
}
}

// I_threeD.java

package ShareWithStudents;

public abstract interface I_threeD
{
public abstract float computeVolume();
}

// I_twoD.java

package ShareWithStudents;

public abstract interface I_twoD
{
public abstract float computeArea();
  
public abstract float computePerimeter();
}

// Shape.java

package ShareWithStudents;

import java.awt.Color;
import java.awt.Graphics;


public abstract class Shape
{
protected Color mColor;
String mName;
protected int mX;
protected int mY;
  
public Shape(Color color, String name)
{
mColor = color;
mName = name;
}
  
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();
  
public abstract void paint(Graphics paramGraphics);
  
public void setPos(int x, int y) {
mX = x;
mY = y;
}
}

// ShapeSelected.java

package ShareWithStudents;

enum ShapeSelected
{
None, Circle, Square, Cube;
  
private ShapeSelected() {}
}

// Square.java

package ShareWithStudents;

import java.awt.Color;
import java.awt.Graphics;
import java.io.PrintStream;

public class Square
extends Shape
implements I_twoD
{
protected int mSide;
  
Square(Color color, String name, int side)
{
super(color, name);
mSide = side;
}
  

public float computeArea()
{
return mSide * mSide;
}
  

public float computePerimeter()
{
return 4.0F * mSide;
}
  
public void print() {
System.out.println("Square (" + mName + ") side : " + mSide + " and color : " + mColor.toString());
}
  
public void paint(Graphics g) {}
}

Explanation / Answer

Whenever we want to use previously implemented classes in Java, we declare them under the package.

The declaration for package in the above code is:

package ShareWithStudents;//ShareWithStudents is the name of the package

Each class defined in the code is written under the package ShareWithStudents.

When we want to reuse these previously implemented classes we simply import the package.

Java includes the import statement to bring certain classes, or entire packages into visibility. Once imported, a class defined in the package can be referred to direclty, using only its name. In java file, import statements occur immediately following the package statement(if it exists) and before any class definitions. For instance in the above case:

import ShareWithStudents.*;

Note: Call the having Assignment4 class as Assignment4.java and put it in a directory called ShareWithStudents and likewise for other classes.

Execute the Assignment4 class using the following command line:

java ShareWithStudents.Assignment4

and remember to be in the directory above ShareWithStudents while executing this command. Also, Assignment4 must be qualified with its package name while running.

Another way to reuse previously implemented classes is using interface. Using the keyword interface, you can fully abstract a class' interface from its implementation. Using interface, you can specify what a class must do, but not how it does it. In the program, classes for different shapes implement intefaces and defines their own method to how the work is done(as in case of calculating ares, perimeters,volumes etc).

You can always extract java file from jar file. Use the command:

jar xf filename.jar

Ex. jar xf Assignment4_ShareWithStudents.jar

Update your paint function of Circle class by adding the following in the method definition:

g.drawOval(mX-mRadius,mY-mRadius,mRadius*2,mRadius*2);

For Square class:

g.drawRect(mX,mY,mSide,mSide);

For Cube class:

g.drawRect(mX,mY,mSide,mSide);

g.drawRect(mX+step,mY+step,mSide,mSide);

g.drawLine(mX,mY,mX+step,mY+step);

g.drawLine(mX+mSide,mY,mX+mSide+step,mY+step);

g.drawLine(mX,mY-mSide,mX+step,mY-mSide+step);

g.drawLine(mX+mSide,mY-mSide,mX+mSide+step,mY-mSide+step);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote