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

Create a drawing JFrame application that draws lines, rectangles, and ovals. For

ID: 3857837 • Letter: C

Question

Create a drawing JFrame application that draws lines, rectangles, and ovals. For this purpose, create a set of JPanel's shape classes MyLine, MyRect, and MyOval. The data for class MyLine should include x1, y1, x2, and y2 coordinates. Method drawLine of class Grahpies is used to connect the two points to draw the line. The data of classes MyRect and MyOval should include the upper-left-x-coordinate value, an upper-left-y- coordinate value, a width (non-negative) and a height (non-negative). All data in each class must be private. In addition to the data, each class should define at least the following methods: a) A constructor with no arguments that sets the coordinates to 0. b) A constructor with arguments that sets the coordinates to the supplied values. c) Set methods for each individual piece of data that allows the programmer to modify any piece of data in a shape (i.e. if you have a variable xl then you should have a method setX1). d) Get methods for each individual piece of data that allows the programmer read any piece of data in a shape (i.e. if you have a variable xl then you should have a method getXI) e) A draw method with the first line: public void paintComponent (Graphics g that is responsible of drawing its corresponding shape, and it would be called by JPanel draw a shape on the screen. In your JFrame application, you should ask the user for a shape to draw (oval, line, or rectangle). The application generate the coordinates and dimensions randomly (horizontally between 0 and getWidth0/2 and vertically between 0 and getHeight0/2), and utilizes the class to draw the requested shape.

Explanation / Answer

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class DrawShapes {

public static void main(String[] args) {
new DrawShapes();
}

public DrawShapes() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new DrawPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class DrawPane extends JPanel {

public DrawPane() {

setLayout(new BorderLayout());

RenderPane rp = new RenderPane();
add(new ControlsPane(rp), BorderLayout.NORTH);
add(rp);

}

}

public class ControlsPane extends JPanel {

public ControlsPane(RenderPane rp) {

JRadioButton[] btns = new JRadioButton[4];
btns[0] = new JRadioButton(new MyLine(rp));
btns[1] = new JRadioButton(new MyRect(rp));
btns[2] = new JRadioButton(new MyOval(rp));
btns[3] = new JRadioButton(new ClearAction(rp));

ButtonGroup bg = new ButtonGroup();
for (JRadioButton btn : btns) {
bg.add(btn);
add(btn);
}

}

}

public class RenderPane extends JPanel {

private Shape shape;

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}

public void setShape(Shape shape) {
this.shape = shape;
repaint();
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (shape != null) {
g2d.setColor(Color.RED);
g2d.draw(shape);
}
g2d.dispose();
}

}

public class MyLine extends AbstractRenderAction {

public MyLine(RenderPane renderPane) {
super(renderPane);
putValue(NAME, "Line");
}

@Override
public Shape getShape() {
return new Line2D.Float(0f, 0f, getRenderPane().getWidth(), getRenderPane().getHeight());
}

}

public class MyRect extends AbstractRenderAction {

public MyRect(RenderPane renderPane) {
super(renderPane);
putValue(NAME, "Rectangle");
}

@Override
public Shape getShape() {
return new Rectangle2D.Float(10, 10, getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
}

}

public class MyOval extends AbstractRenderAction {

public MyOval(RenderPane renderPane) {
super(renderPane);
putValue(NAME, "Oval");
}

@Override
public Shape getShape() {
float radius = Math.min(getRenderPane().getWidth() - 20, getRenderPane().getHeight() - 20);
return new Ellipse2D.Float(10, 10, radius, radius);
}

}

public class ClearAction extends AbstractRenderAction {

public ClearAction(RenderPane renderPane) {
super(renderPane);
putValue(NAME, "Clear");
}

@Override
public Shape getShape() {
return null;
}

}

public abstract class AbstractRenderAction extends AbstractAction {

private RenderPane renderPane;

public AbstractRenderAction(RenderPane renderPane) {
this.renderPane = renderPane;
}

public RenderPane getRenderPane() {
return renderPane;
}

public abstract Shape getShape();

@Override
public void actionPerformed(ActionEvent e) {
getRenderPane().setShape(getShape());
}

}

}

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