Date post: | 28-Mar-2015 |
Category: |
Documents |
Upload: | victoria-hawkins |
View: | 213 times |
Download: | 0 times |
Rectangles moving and responding to the mouse
• We want a window with a pile of rectangles in it
• When we click a rectangle it changes from filled to unfilled or back again– Starting unfilled
• We can drag rectangles too
Classes Needed
• Rectangle– Particular requirement: needs to know if its
filled or not– Needs to know how to draw itself
import java.awt.*;
public class Rect extends Rectangle {
private boolean fillRect = false;
public Rect() { this.x = 100;
this.y = 100; this.height = 40; this.width = 30; }
public void setfillRect(boolean fillRect) {
this.fillRect = fillRect;
}
public boolean getfillRect() {
return fillRect;
}
public void drawMyRect (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
if (this.getfillRect())
g2.fill(this);
else
g2.draw(this);
}
}
Pile of Rectangles
• Need to be able to keep track of your place and cycle through
• In Java, you do that with Iterators (see Laszlo Design Patterm Chapter)
Iterators
• Cf Chapter 6 in the subject guide
Classes Needed 2
• Panel (we will call it View)– Needs to Listen for Mouse clicking and Mouse
dragging– So needs to implement Mouse Listeners
Clicking, Entering, dragging, Pressing, Releasing…
Data for View
• Various Booleans to keep track of what you are doing:– Maybe just dragging
• Rectangle Pile– ArrayList
• We will need two variables for Rectangles
Method 1: Constructor
public View(java.util.ArrayList d) {
rectPile = d;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setFocusable(true);
this.setEnabled(true);
}
MouseClicking
1. Change your mind about filling
2. Repaint
public void mouseClicked(MouseEvent e) { { n = findRectAtPoint(e.getPoint()); if (n != null) { if (n.getfillRect()) n.setfillRect(false); else n.setfillRect(true);} } repaint(); }
Mouse Pressing
You are getting ready to drag
1. Work out what rectangle you have pressed
2. Work out how far you are from the UL corner
3. Set dragging to true
Mouse Pressing
public void mousePressed(MouseEvent e) { n = findRectAtPoint(e.getPoint()); if (n != null) { dragging = true; offxs = (int) n.getX() - e.getX(); offys = (int) n.getY() - e.getY(); } }
Mouse Dragging
public void mouseDragged(MouseEvent e) {
if (dragging) {
n.setLocation((int) (e.getX() + offxs), e.getY() + offys);
repaint();
}
}
Moue Released
• Stop dragging
public void mouseReleased(MouseEvent e) {
if (dragging) {
dragging = false;
n = null;
repaint();
}
}
Main Application
public static void main(String[] args)
{
parseArgs(args);
Lab3Application pa = new Lab3Application("LAB3");
}
Arguments
• What we will do with the arguments here is to set the number of rectangles
• Therefore the number of arguments we will need is really one and it is an int
• But for main we have to make the input an array of strings– parseArgs is to get from the String Array to what we
rea,,y need
public static void parseArgs(String[] args)
{
if (args.length == 1) {
nbrRectangles = Integer.parseInt(args[0]);
}
}
The Application is a kind of Frame
public class Lab3Application extends JFrame {
private final static int HEIGHT = 300;
private final static int WIDTH = 400;
Data
• The Size of the Frame
• The number of Rectangles
• The RectanglePile itself
Constructor
• It will take the title as a parameter
• Make the pile:– Make a pile object– Call a method that will create the pile
• Make a View and add it as the Content Pane
• And it will set a few attributes of the frame
public Lab3Application(String title) { super(title); this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d = new ArrayList(); createInitialPile(); View pv = new View(d); this.getContentPane().add(pv); this.setSize(HEIGHT, WIDTH); this.setVisible(true); }
Creating the Pile
private void createInitialPile()
{
for (int i = 0; i < nbrRectangles; i++)
{
d.add(new Rect(20, 20));
}
}