Question: Using/in Java Swing create an application that allows the user to drag a geometric shape (2D ellipse) with the mouse and move it around with
Using/in Java Swing create an application that allows the user to drag a geometric shape (2D ellipse) with the mouse and move it around with the keyboard Must use JPanel, JFrame , geometric 2D, and Mouse Listeners the program react to a mouse or keyboard event by printing a message to the console when this event occurs
public class Panel extends javax.swing.JPanel
{
private final int INIT_X = 75;
private final int INIT_Y = 75;
private final int DIAMETER = 60;
private SmartEllipse _sunny;
public Panel()
{
super();
this.setBackground(java.awt.Color.white);
_sunny = new SmartEllipse(java.awt.Color.red);
_sunny.setLocation(INIT_X, INIT_Y);
_sunny.setSize(DIAMETER, DIAMETER);
// The panel needs to add a new MouseListener, KeyListener, and
// MouseMotionListener in its constructor. To receive key events, it must also
// be focusable.
}
public void paintComponent(java.awt.Graphics aBrush)
{
super.paintComponent(aBrush);
java.awt.Graphics2D betterBrush = (java.awt.Graphics2D) aBrush;
_sunny.fill(betterBrush);
}
// You can create one or more private listener classes here. These classes must
// implement MouseListener, KeyListener, and MouseMotionListener (you can make
// one class for all three, a separate class for each, or have the Panel
// implement them instead). Make sure they're working on printing a message in
// the mousePressed, keyPressed, and mouseDragged methods.
}
public class SmartEllipse extends java.awt.geom.Ellipse2D.Double
{
private java.awt.Color _borderColor, _fillColor; // attributes
private int _rotation;
private final int STROKE_WIDTH = 2;
public SmartEllipse(java.awt.Color aColor)
{
super();
_borderColor = aColor;
_fillColor = aColor; // solid color to start
_rotation = 0; // no rotation for now
}
// methods not provided by the super class
public void setBorderColor(java.awt.Color aColor)
{
_borderColor = aColor;
}
public void setFillColor(java.awt.Color aColor)
{
_fillColor = aColor;
}
public void setRotation(int aRotation)
{
_rotation = aRotation;
}
// more readable versions of methods provided by super class
public void setLocation(double x, double y)
{
this.setFrame(x, y, this.getWidth(), this.getHeight());
}
public void setSize(int aWidth, int aHeight)
{
this.setFrame(this.getX(), this.getY(), aWidth, aHeight);
}
public void move(int aChangeInX, int aChangeInY)
{
this.setFrame((int) this.getX() + aChangeInX, (int) this.getY() + aChangeInY, this.getWidth(),
this.getHeight());
}
/**
* A method for painting the "inside" of the SmartEllipse
*
* @param aBetterBrush
*/
public void fill(java.awt.Graphics2D aBetterBrush)
{
java.awt.Color savedColor = aBetterBrush.getColor();
aBetterBrush.setColor(_fillColor);
aBetterBrush.fill(this);
aBetterBrush.setColor(savedColor);
}
/**
* A method for painting the outline/border of the SmartEllipse
*
* @param aBrush
*/
public void draw(java.awt.Graphics2D aBrush)
{
java.awt.Color savedColor = aBrush.getColor();
aBrush.setColor(_borderColor);
java.awt.Stroke savedStroke = aBrush.getStroke();
aBrush.setStroke(new java.awt.BasicStroke(STROKE_WIDTH));
aBrush.draw(this);
aBrush.setStroke(savedStroke);
aBrush.setColor(savedColor);
}
}?
public class BallApp extends javax.swing.JFrame
{
public BallApp(String title)
{
super(title);
this.setSize(600, 450);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.add(new BallPanel());
this.setVisible(true);
}
public static void main(String[] args)
{
BallApp app = new BallApp("This is a JFrame!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
