Here's the code developed yesterday in LH102.
package assignment3.demo;
import BreezyGUI.*;
import java.awt.*;
public class Drawing extends GBFrame {
Circle[] circles = new Circle[10];
Circle selectedCircle;
public void paint(Graphics d) {
for (int i = 0; i < circles.length; i++)
if (circles[i] != null)
circles[i].draw(d);
System.out.println("Paint has been called");
}
public void mousePressed(int x, int y) {
for (int i = 0; i < circles.length; i++) {
if (circles[i].contains(x, y)) {
selectedCircle = circles[i];
return;
}
}
System.out.println("Mouse pressed at " + x + " " + y);
}
public void mouseDragged(int x, int y) {
if (selectedCircle != null) {
selectedCircle.draw(getGraphics());
selectedCircle.move(x, y);
selectedCircle.draw(getGraphics());
}
System.out.println("Mouse dragging detected at " + x + " " + y);
}
public void mouseReleased(int x, int y) {
System.out.println("Mouse released at " + x + " " + y);
}
public void mouseMoved(int x, int y) {
System.out.println("Mouse movement detected at " + x + " " + y);
}
int x = 50; int y = 50;
void newCircle() {
x += 10; y += 10;
Circle c = new Circle(
x, y, 40, new Color((int)(Math.random() * 255),
(int)(Math.random() * 255),
(int)(Math.random() * 255))
);
for (int i = 0; i < circles.length; i++)
if (circles[i] == null) {
circles[i] = c;
return;
}
}
public static void main(String[] args) {
Drawing f = new Drawing();
f.newCircle();
f.newCircle();
f.newCircle();
f.newCircle();
f.newCircle();
f.setSize(200, 200);
f.setVisible(true);
}
}
class Circle {
int xC, yC;
int radius;
Color color;
Circle (int x, int y, int r, Color c) {
xC = x; yC = y; radius = r; color = c;
}
boolean contains(int x, int y) {
return (Math.sqrt( (x - xC) * (x - xC) + (y - yC) * (y - yC))) < radius;
}
void move(int x, int y) {
xC = x; yC = y;
}
void draw(Graphics g) {
g.setColor(color);
g.setXORMode(Color.white);
g.fillOval(xC - radius, yC - radius, radius * 2, radius * 2);
}
}
Here's what you need to do to finish the homework.
Yoru Drawing objects keep state in Circle variable
that points to the currently selected circle. It's the circle that you should
control through the buttons of the second frame that you should build, let's
say that it's class's name is Controls.
The frame with buttons needs to react to its buttons being pressed by
sending appropriate messages to the controlled Drawing frame.
So the first thing that you should do is to provide the Drawing
objects with the capability of receiving those messages: define instance
methods in class Drawing that could be called on instances
of class Drawing to
moveSelectedCircleUp (int dy)
moveSelectedCircleDown (int dy)
moveSelectedCircleLeft (int dx)
moveSelectedCircleRight(int dx)
enlargeSelectedCircleBy(int dr)
shrinkSelectedCircleBy (int dr)
createNewSelectedCircle() and
quitProgram()
Controls
object keeps track of the Drawing object (by storing a reference
to it in an instance variable) and sends the appropriate message through it
when a button is pressed.
It is likely that you will start the Controls program first, and
inside it, the frame with buttons will create a
new Drawing()
and store the returned address in a reference of type Drawing that
will be then used as described above. Creating a frame with buttons is described
in the textbook very thoroughly.