Top Banner
Rectangles moving and responding to the mouse
27

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.

Mar 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Rectangles moving and responding to the mouse

Page 2: 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.

• 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

Page 3: 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.

Classes Needed

• Rectangle– Particular requirement: needs to know if its

filled or not– Needs to know how to draw itself

Page 4: 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.

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; }

Page 5: 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.

public void setfillRect(boolean fillRect) {

this.fillRect = fillRect;

}

public boolean getfillRect() {

return fillRect;

}

Page 6: 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.

public void drawMyRect (Graphics g)

{

Graphics2D g2 = (Graphics2D) g;

if (this.getfillRect())

g2.fill(this);

else

g2.draw(this);

}

}

Page 7: 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.

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)

Page 8: 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.

Iterators

• Cf Chapter 6 in the subject guide

Page 9: 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.
Page 10: 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.

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…

Page 11: 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.

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

Page 12: 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.

Method 1: Constructor

public View(java.util.ArrayList d) {

rectPile = d;

this.addMouseListener(this);

this.addMouseMotionListener(this);

this.setFocusable(true);

this.setEnabled(true);

}

Page 13: 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.

MouseClicking

1. Change your mind about filling

2. Repaint

Page 14: 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.

public void mouseClicked(MouseEvent e) { { n = findRectAtPoint(e.getPoint()); if (n != null) { if (n.getfillRect()) n.setfillRect(false); else n.setfillRect(true);} } repaint(); }

Page 15: 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.

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

Page 16: 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.

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(); } }

Page 17: 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.

Mouse Dragging

public void mouseDragged(MouseEvent e) {

if (dragging) {

n.setLocation((int) (e.getX() + offxs), e.getY() + offys);

repaint();

}

}

Page 18: 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.

Moue Released

• Stop dragging

Page 19: 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.

public void mouseReleased(MouseEvent e) {

if (dragging) {

dragging = false;

n = null;

repaint();

}

}

Page 20: 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.

Main Application

public static void main(String[] args)

{

parseArgs(args);

Lab3Application pa = new Lab3Application("LAB3");

}

Page 21: 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.

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

Page 22: 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.

public static void parseArgs(String[] args)

{

if (args.length == 1) {

nbrRectangles = Integer.parseInt(args[0]);

}

}

Page 23: 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.

The Application is a kind of Frame

public class Lab3Application extends JFrame {

private final static int HEIGHT = 300;

private final static int WIDTH = 400;

Page 24: 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.

Data

• The Size of the Frame

• The number of Rectangles

• The RectanglePile itself

Page 25: 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.

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

Page 26: 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.

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); }

Page 27: 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.

Creating the Pile

private void createInitialPile()

{

for (int i = 0; i < nbrRectangles; i++)

{

d.add(new Rect(20, 20));

}

}