1 Object-Oriented Programming (Java), Unit 20 Kirk Scott.

Slides:



Advertisements
Similar presentations
Web Design & Development Lecture 19. Java Graphics 2.
Advertisements

Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
For IST410 Students only Events-1 Event Handling.
IEEM 110 Computing in Industrial Applications Basic User Interface in Java.
Event-Driven Programming
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Web Design & Development Lecture 18. Java Graphics.
Lesson 35: Review of the Java GUI. The JFrame, Container and JButton.
1 Object-Oriented Programming (Java), Unit 22 Kirk Scott.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
Chapter 28 Iterator Summary prepared by Kirk Scott 1.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Layout Management Containers can arrange their components. Our container is a JPanel, and it can set the way it’s components will be laid out : mypanel.setLayout.
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Object-Oriented Programming (Java), Unit 26 Kirk Scott 1.
Adding Graphics to a Frame Application Applets: Can generate drawings by overriding paint Frame: Do not draw directly on a frame. Draw graphics on a JPanel.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
1 Object-Oriented Programming (Java), Unit 9 (comes before Unit 20) Kirk Scott.
Object-Oriented Programming (Java), Unit 26 Kirk Scott 1.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
SD2071 Games Programming Abstraction, inheritance and interfaces Exceptions Two dimensional arrays Java collections framework Files Aaron Kans.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
1 Object-Oriented Programming (Java), Unit 23 Kirk Scott.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
OOP (Java): GUI II/ OOP Objectives – –describe some more GUI features: JPanel, and mouse listeners/adapters Semester 2,
1 Object-Oriented Programming (Java), Unit 17 Kirk Scott.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
The Drawing program – Java Applets
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
Copyright © 2002, Systems and Computer Engineering, Carleton University b-Gui2.ppt * Object-Oriented Software Development Part 18-b Building.
1 Object-Oriented Programming (Java), Unit 17 Kirk Scott.
Event Driven Programming. Event-driven Programming In the early days of computing communication with the outside world was accomplished using a technique.
Lesson 39: More wrapup on GUIs. 1.This presentation will focus on the decisions around software architecture and the decisions that will drive the code.
Object-Oriented Programming (Java), Unit 29 Kirk Scott 1.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Event Handling.
Java Dynamic Graphics.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 10: Event Handling 1 Chapter 10 Event Handling.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Java Swing One of the most important features of Java is its ability to draw graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
1/18H212Mouse and Timer Events H212 Introduction to Software Systems Honors Lecture #16: Mouse and Timer Events October 26, 2015.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Lesson 28: More on the GUI button, frame and actions.
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
GUI Programming in Java Hao Jiang Boston College April, 2009.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Java Graphics.
Object-Oriented Programming (Java), Unit 22
Java Graphics CS 2511.
Summary prepared by Kirk Scott
Computer Science 209 Graphics and GUIs.
Object-Oriented Programming (Java), Unit 23
Summary prepared by Kirk Scott
Presentation transcript:

1 Object-Oriented Programming (Java), Unit 20 Kirk Scott

2 Clickable Objects 20.1 Clickable Objects 20.2 Graphical Objects within Objects 20.3 Changing the Mouse Pointer 20.4 Implementation Choices

Toothcomb 3

Ring-tailed lemur’s toothcomb 4

Sublingua of a slow loris 5

Philippine flying lemur (colugo) mother with baby 6

Colugo (Singapore) 7

8

9 ClickCup: This program has 2 cups in the panel. Clicking one cup moves its contents to the other. This is how it appears :

10 The structure diagram for ClickCup:

11

12 The sequence diagram for the input side of ClickCup:

13

14 The sequence diagram for the output side of ClickCup:

15

16 The code for ClickCup: import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.Graphics2D; import java.awt.Rectangle; import java.lang.*; public class ClickCup { public static void main(String[] args) { ClickCupFrame myframe = new ClickCupFrame(); myframe.setVisible(true); }

17 class ClickCupFrame extends JFrame { private ClickCupPanel myPanel; private final int FRAMEW = 500; private final int FRAMEH = 500; public ClickCupFrame() { setTitle("ClickCup Frame"); setSize(FRAMEW, FRAMEH); myPanel = new ClickCupPanel(); Container contentPane = getContentPane(); contentPane.add(myPanel, "Center"); addWindowListener(new WindowCloser()); } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); }

18 class ClickCupPanel extends JPanel { private ClickCupCup myCupA; private ClickCupCup myCupB; private ClickCupCup whichCupIsActive; public ClickCupPanel() { myCupA = new ClickCupCup(4, 200, 200, 40, 40); myCupB = new ClickCupCup(0, 250, 200, 40, 40); whichCupIsActive = myCupA; addMouseListener(new MouseHandler()); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; super.paintComponent(g2); myCupA.drawCup(g2); myCupB.drawCup(g2); }

19 private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent event) { if(myCupA.getRectangle().contains(event.getPoint()) && whichCupIsActive == myCupA) { myCupA.moveCupValueTo(myCupB); whichCupIsActive = myCupB; repaint(); } elseif(myCupB.getRectangle().contains(event.getPoint()) && whichCupIsActive == myCupB) { myCupB.moveCupValueTo(myCupA); whichCupIsActive = myCupA; repaint(); } else { }

20 ClickDot This program contains a new class, Seed. The cups hold seeds, which are displayed as dots on the screen. Clicking on a cup causes the seeds to jump from that cup to the other. This also causes the representation on the screen to reflect that change.

21

22 The structure diagram for ClickDot:

23

24 No sequence diagram is given for the input side of ClickDot because it's the same as for ClickCup. Here is the sequence diagram for the output side of ClickDot:

25

26 The code for ClickDot: The code for ClickDot is based on the code for ClickCup. A Seed class is added. The SeedCup class is changed to accommodate seeds which are instances of the Seed class.

27 class SeedCup { private Rectangle cupRectangle; private ArrayList seedList; public SeedCup (int seedCountIn, int cupX, int cupY, int cupW, int cupH) { int i; cupRectangle = new Rectangle (cupX, cupY, cupW, cupH); seedList = new ArrayList (); for(i = 0; i < seedCountIn; i++) { Seed aSeed = new Seed(); seedList.add(aSeed); }

28 public void moveCupValueTo(SeedCup destination) { int i; for(Iterator myiterator = seedList.iterator(); myiterator.hasNext(); ) { destination.seedList.add((Seed) myiterator.next()); myiterator.remove(); } public Rectangle getRectangle() { return cupRectangle; }

29 public void drawCup(Graphics2D g2) { int i; int seedX, seedY; Random generator = new Random(); g2.draw(cupRectangle); for(Seed aSeed: seedList) { if((aSeed.getDiameter() <= cupRectangle.getWidth()) && (aSeed.getDiameter() <= cupRectangle.getHeight())) { seedX = (int) cupRectangle.getX() + generator.nextInt((int) cupRectangle.getWidth() - aSeed.getDiameter()); seedY = (int) cupRectangle.getY() + generator.nextInt((int) cupRectangle.getHeight() – aSeed.getDiameter()); aSeed.drawSeed(g2, seedX, seedY); }

30 class Seed { private final Color seedColor = Color.BLUE; private final int diameter = 6; public Seed() { } public int getDiameter() { return diameter; } public void drawSeed(Graphics2D g2, int xCoord, int yCoord) { Ellipse2D.Double dot = new Ellipse2D.Double(xCoord, yCoord, diameter, diameter); g2.setColor(seedColor); g2.fill(dot); g2.setColor(Color.BLACK); }

31 ClickHand This program shows a different mouse pointer, the hand, over whichever cup is active at a given time. There is no screen shot for this application because the screen shot does not show changes in the mouse pointer. The structure chart for ClickHand follows.

32

33 There is no sequence diagram for ClickHand. Here are the code changes: class ClickHandPanel extends JPanel { private SeedCup myCupA; private SeedCup myCupB; private SeedCup whichCupIsActive; public ClickHandPanel() { myCupA = new SeedCup(4, 200, 200, 40, 40); myCupB = new SeedCup(0, 250, 200, 40, 40); whichCupIsActive = myCupA; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); }

34 The mouseClicked() method has added to it the line of code at the bottom which sets the mouse cursor to its default value after an action has been taken: private class MouseHandler extends MouseAdapter { public void mouseClicked(MouseEvent event) { if(myCupA.getRectangle().contains(event.getPoint()) && whichCupIsActive == myCupA) { myCupA.moveCupValueTo(myCupB); whichCupIsActive = myCupB; repaint(); } else if(myCupB.getRectangle().contains(event.getPoint()) && whichCupIsActive == myCupB) { myCupB.moveCupValueTo(myCupA); whichCupIsActive = myCupA; repaint(); } else { } setCursor(Cursor.getDefaultCursor()); }

35 The the mouse motion handler continuously monitors the location of the mouse cursor and changes it to the hand if it is in the active cup. This functionality is based on the method getPoint(), like the functionality in the plain mouse handler. private class MouseMotionHandler extends MouseMotionAdapter { public void mouseMoved(MouseEvent event) { if(whichCupIsActive.getRectangle().contains(event.getPoint())) setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); else setCursor(Cursor.getDefaultCursor()); }

36 The End