100 likes | 263 Views
CS12420 – Lecture 5 Interactive Graphics. Lynda Thomas ltt@aber.ac.uk. In This Lecture. Interactive graphics. First look at a couple of failed attempts. In basic-graphics-withcomments Last time we just printed where the mouse is Need a way to store data as well.
E N D
CS12420 – Lecture 5Interactive Graphics Lynda Thomas ltt@aber.ac.uk
In This Lecture • Interactive graphics
First look at a couple of failed attempts • In basic-graphics-withcomments • Last time we just printed where the mouse is • Need a way to store data as well.
Mouse Project Specification(note this is not on drag but on click) Title Panel 300x300 Mouse pointer left click: add right click: remove position: 137 53 number of clicks: 27 no. of circles: 2 mouse is in component: yes
Look at files in directory • Need to save the circles so can repaint properly • Think about the last version: • Where do you know you need a circle - when you get a click - the MyMouseListener • Where do you need to save the circles – one theory is where you redraw them – the MouseEventPanel
So, need to add a link back from the MyMouseListener to the drawing panel In the Frame: mL = new MyMouseListener(stPanel,mePanel); In the MyMouseListener: MyMouseListener(StatusPanel sp,MouseEventPanel mp) { statusPane = sp; mousePane = mp; } And then when the Mouse is clicked ….
public void mouseClicked(MouseEvent e) { statusPane.incrementClickCount(); if(SwingUtilities.isLeftMouseButton(e)) { mousePane.addCircle(e.getX(),e.getY()); } else { mousePane.removeNearestCircle (e.getX(),e.getY()); } //will have to write the methods for addCircle and remove statusPane.setNoOfCircles(mousePane.getNoOfCircles()); }
VectorOfCircles - circles : Vector<Circle> + addCircle() + removeNearestCircle() + drawAll() + getNoOfCircles() : int SimpleFrame + showIt() MouseEventDriver + main() UML Class Diagram MyMousePositionListener - stPanel : StatusPanel + mouseMoved(e : MouseEvent) StatusPanel - posText …. dummy3 : JLabel - clickCount : int + setCoordinates(x,y : int) + setInOut(s : String) + incrementClickCount() + setNoOfCircles(n : int) MyMouseListener - stPanel : StatusPanel - mePanel : MouseEventPanel + mouseEntered(e : MouseEvent) + mouseExited(e : MouseEvent) + mouseClicked(e : MouseEvent) MouseEventPanel - voc : VectorOfCircles - r : double + paintComponent(g : Graphics) + addCircle(x,y : int) + removeNearestCircle(x,y : int) + getNoOfCircles() : int MouseEventFrame - mePanel : MouseEventPanel - stPanel : StatusPanel Circle - x,y,r : double + draw(g : Graphics) + distanceTo(x,y : double) : double
In This Lecture • Interactive graphics • There is lots more left for you to discover! • Your next mini-assignment (10%) is about this (you are asked to drag a circle around the screen)