Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Software Engineering Using Threads and simple Animation.

Similar presentations


Presentation on theme: "Object-Oriented Software Engineering Using Threads and simple Animation."— Presentation transcript:

1 Object-Oriented Software Engineering Using Threads and simple Animation

2 UniS Contents CoordinatesDemo (Mark 1) case study Painting Graphic Objects Primitive Animation with Move Repaint Cycle The paintComponent method Dynamic thread creation, MouseInputListener interface. Resource Contention, Racing Threads

3 UniS CoordinatesDemo (Mark1)Thread Case Study Square created by mouse click Square oscillates across grid and exits left. Square is created and controlled by thread class DrawRect. CoordinateArea JFrame draw_rect: DrawRect Full code available on web site for this lecture

4 UniS CoordinatesDemo Inner Classes CoordinateAreaDrawRect Can access all methods of outer class Instances are created dynamically by Mouse event

5 UniS Class Diagram CoordinatesDemo CoordinateArea DrawRect JComponent MouseInputListener Thread inner class dependency inner class dependency JFrame contained in Graphics paints with Black triangles indicate direction to read association

6 UniS Painting Graphic Objects Animation is achieved in Java application by the CoordinateArea continually 'repainting' itself every few mille-seconds. Every Swing component that extends JComponent can override the protected void paintComponent(Graphics g) method. CoordinateArea

7 UniS createAndShowGUI method private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("CoordinatesDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane. CoordinatesDemo controller = new CoordinatesDemo(); controller.buildUI(frame.getContentPane()); //Display the window. frame.pack(); frame.setVisible(true); } This part as usual Here we have to directly manipulate content pane of JFrame to animate graphics

8 UniS buildUI method private void buildUI(Container container) { container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); CoordinateArea coordinateArea = new CoordinateArea(this); container.add(coordinateArea); label = new JLabel(); resetLabel(); container.add(label); //Align the left edges of the components. coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT); label.setAlignmentX(Component.LEFT_ALIGNMENT); //redundant } value will be frame.getContentPane() attach the coordinateArea to the content panecreate new coordinateArea whose parent, this, is the CoordinatesDemo application

9 UniS Overview of createAndShowGUI method controller: CoordinatesDemo frame:JFrame coordinateArea: CoordinateArea > getContentPane() container container: Container add(coordinateArea)

10 UniS frame: JFrame container (frame's content pane) coordinateArea

11 UniS Painting Graphic Objects When an JComponent is first created its paintComponent method is called. From then on if something is modified, the component can be made to repaint itself with a call to the repaint( ) method.

12 UniS Painting Graphic Objects When a left mouse click occurs, this creates an instance of a new thread, DrawRect. The square is created and controlled by the separate thread. The run( ) method of the thread gradually moves the square across the content pane. The thread moves the square within the graphical object belonging to the content pane, and then repaints the content pane.

13 UniS Move Repaint while ((x + rect_size) > 0) { try { Thread.currentThread().sleep(40); x -= 2; new_y = new Double(y + Math.cos(x/(Math.PI*2))*10); point = new Point(x, new_y.intValue()); controller.updateClickPoint(point); repaint(); } catch (InterruptedException exp2) { // do nothing } move Code fragment from run( ) method of DrawRect thread sleep controls rate of motion repaint to make changes appear on content pane repaint( ) move

14 UniS protected void paintComponent(Graphics g) protected void paintComponent(Graphics g) { //Paint background if we're opaque. if (isOpaque()) { g.setColor(getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); } //Paint 20x20 grid. g.setColor(Color.GRAY); drawGrid(g, 20); // Draw rectangle according to modified values defined by DrawRect thread if (point != null) { g.setColor(getForeground()); g.fillRect(point.x - rect_ofset, point.y - rect_ofset, rect_size, rect_size); }

15 UniS Graphics Objects For this course we are not concerned with the detailed behaviour of graphics rendering within a GUI. Roughly a Graphics object captures the current context for manipulating the appearance of a content pane. Graphics object defines coordinate system for controlling exact location of shapes within content pane. Graphics object has a number of methods for making simple shapes appear in the content pane. E.g. g.fillRect(x,y,width, height)

16 UniS CoordinateArea Constructor public CoordinateArea(CoordinatesDemo controller) { this.controller = controller; //Add a border of 5 pixels at the left and bottom, //and 1 pixel at the top and right. setBorder(BorderFactory.createMatteBorder(1,5,5,1, Color.RED)); addMouseListener(this); addMouseMotionListener(this); setBackground(Color.WHITE); setOpaque(true); } Mouse listener and mouse motion listener added to CoordinateArea itself

17 UniS Dynamic thread creation, MouseInputListener interface. public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); int itr_rect = 6; if (point == null) { point = new Point(x, y); } else { point.x = x; point.y = y; } DrawRect draw_rect = new DrawRect(point); draw_rect.start(); } get coordinates modify point to store coordinates start new thread

18 UniS MouseInputListener interface. public void mouseMoved(MouseEvent e) { controller.updateCursorLocation(e.getX(), e.getY()); } public void mouseExited(MouseEvent e) { controller.resetLabel(); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } must implement all methods of interface, even if with empty methods when mouse not over content pane

19 UniS Mouse Left Click Event coordinateArea: CoordinateArea e:Mouse Event > left_mouse_click notify(e ) query event > draw_rect: DrawRect start( ) coordinateArea notifies itself when a mouse event occurs

20 UniS Point Object Point object is an easy way to pass (x,y) coordinates around in a Graphics context. (x,y) values of a Point object are given by p.x and p.y where p is an instance of Point point instance field of CoordinateArea used to control where square appears within content pane. point.x = 145, point.y = 30

21 UniS Resource Contention, Race Each mouse click creates an instance of DrawRect Each DrawRect object is an independent thread There can only be one occurrence of a square in the content pane Each thread will set the location of the square according to where it was created Hence the location of the square will change when each thread becomes active This leads to racing between the DrawRect threads with unpleasant results.

22 UniS Overview Sequence Diagram for Race point:Point coordinateArea: CoordinateArea draw_rect(1) :DrawRect draw_rect(2) :DrawRect change value repaint( ) change value repaint( ) looppar

23 UniS Notation for sequence diagram Parallel construct, keyword par, denotes different regions that occur at the same time. More precisely regions that are interleaved in an arbitrary manner. In a sequence diagram a region can contain a loop that will iterate some arbitrary number of times. This has keyword loop. When a par is contained in a loop, the contents of the par will be interleaved with each iteration of the loop. BUT each time the interleaving may be different.


Download ppt "Object-Oriented Software Engineering Using Threads and simple Animation."

Similar presentations


Ads by Google