Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Rectangles moving and responding to the mouse

2 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

3 Classes Needed Rectangle –Particular requirement: needs to know if its filled or not –Needs to know how to draw itself

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

5 public void setfillRect(boolean fillRect) { this.fillRect = fillRect; } public boolean getfillRect() { return fillRect; }

6 public void drawMyRect (Graphics g) { Graphics2D g2 = (Graphics2D) g; if (this.getfillRect()) g2.fill(this); else g2.draw(this); } }

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

8 Iterators Cf Chapter 6 in the subject guide

9

10 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…

11 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

12 Method 1: Constructor public View(java.util.ArrayList d) { rectPile = d; this.addMouseListener(this); this.addMouseMotionListener(this); this.setFocusable(true); this.setEnabled(true); }

13 MouseClicking 1.Change your mind about filling 2.Repaint

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

15 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

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

17 Mouse Dragging public void mouseDragged(MouseEvent e) { if (dragging) { n.setLocation((int) (e.getX() + offxs), e.getY() + offys); repaint(); }

18 Moue Released Stop dragging

19 public void mouseReleased(MouseEvent e) { if (dragging) { dragging = false; n = null; repaint(); }

20 Main Application public static void main(String[] args) { parseArgs(args); Lab3Application pa = new Lab3Application("LAB3"); }

21 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

22 public static void parseArgs(String[] args) { if (args.length == 1) { nbrRectangles = Integer.parseInt(args[0]); }

23 The Application is a kind of Frame public class Lab3Application extends JFrame { private final static int HEIGHT = 300; private final static int WIDTH = 400;

24 Data The Size of the Frame The number of Rectangles The RectanglePile itself

25 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

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

27 Creating the Pile private void createInitialPile() { for (int i = 0; i < nbrRectangles; i++) { d.add(new Rect(20, 20)); }


Download ppt "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."

Similar presentations


Ads by Google