1 / 27

Rectangles moving and responding to the mouse

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

long
Download Presentation

Rectangles moving and responding to the mouse

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

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

  12. MouseClicking • Change your mind about filling • Repaint

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

  14. Mouse Pressing You are getting ready to drag • Work out what rectangle you have pressed • Work out how far you are from the UL corner • Set dragging to true

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

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

  17. Moue Released • Stop dragging

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

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

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

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

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

  23. Data • The Size of the Frame • The number of Rectangles • The RectanglePile itself

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

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

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

More Related