1 object-oriented programming (java), unit 20 kirk scott
TRANSCRIPT
![Page 1: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/1.jpg)
1
Object-Oriented Programming (Java), Unit 20
Kirk Scott
![Page 2: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/2.jpg)
2
Clickable Objects
• 20.1 Clickable Objects• 20.2 Graphical Objects within Objects• 20.3 Changing the Mouse Pointer• 20.4 Implementation Choices
![Page 3: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/3.jpg)
Toothcomb
3
![Page 4: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/4.jpg)
Ring-tailed lemur’s toothcomb
4
![Page 5: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/5.jpg)
Sublingua of a slow loris
5
![Page 6: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/6.jpg)
Philippine flying lemur (colugo) mother with baby
6
![Page 7: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/7.jpg)
Colugo (Singapore)
7
![Page 8: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/8.jpg)
8
![Page 9: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/9.jpg)
9
ClickCup:
• This program has 2 cups in the panel. • Clicking one cup moves its contents to the other.
• This is how it appears:
![Page 10: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/10.jpg)
10
The structure diagram for ClickCup:
![Page 11: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/11.jpg)
11
ClickCup
ClickCupFrame
ContentPane
JFrame
+paintComponent()+setCup()
ClickCupPanel
+paintComponent()+repaint()
JPanel
-has1
1
-has1
1
-has added1
1
ClickCupCup
-has1
2
MouseHandler
-has
1
1
WindowCloser
-has
11WindowAdapter
1
-has instance variable
1
Rectangle
-has1
1
MouseAdapter
![Page 12: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/12.jpg)
12
The sequence diagram for the input side of ClickCup:
![Page 13: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/13.jpg)
13
instance of MouseHandler instance of MouseEvent myPanel myCupA cupRectangle
mouseClicked()
getRectangle()
cupRectangle
getPoint()
contains(event.getPoint())
moveCupValueTo(myCupB)
repaint()
![Page 14: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/14.jpg)
14
The sequence diagram for the output side of ClickCup:
![Page 15: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/15.jpg)
15
myPanel myCupA myCupB g2
drawCup(g2)
draw(cupRectangle)
drawString(Integer...)
update()
paintComponent()
super.paintComponent()
drawCup(g2)
drawCup(cupRectangle)
drawString(Integer...)
repaint()
![Page 16: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/16.jpg)
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);• }• }
![Page 17: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/17.jpg)
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);• }• }• }
![Page 18: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/18.jpg)
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);• }
![Page 19: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/19.jpg)
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• {• }• }• }• }
![Page 20: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/20.jpg)
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.
![Page 21: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/21.jpg)
21
![Page 22: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/22.jpg)
22
The structure diagram for ClickDot:
![Page 23: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/23.jpg)
23
ClickDot
ClickDotFrame
ContentPane
+paintComponent()+setCup()
ClickDotPanel
+paintComponent()+repaint()
JPanel
-has1
1
-has1
1
-has added1
1
SeedCup
-has
1
2
MouseHandler
-has1
1
WindowCloser
-has
11WindowAdapter
1
-has instance variable
1
Rectangle
-has
1
1
MouseAdapter
JFrame
ArrayList
-has1
1
Seed
-has1
0..4
![Page 24: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/24.jpg)
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:
![Page 25: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/25.jpg)
25
myPanel myCupA myCupB g2
drawCup(g2)
draw(cupRectangle)
update()
paintComponent()
super.paintComponent()
repeat sequence above for myCupB
repaint()
instance of Seed
*drawSeed(g2)
setColor()
fill()
setColor()
![Page 26: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/26.jpg)
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.
![Page 27: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/27.jpg)
27
• class SeedCup• {• private Rectangle cupRectangle;• private ArrayList<Seed> 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<Seed>();• for(i = 0; i < seedCountIn; i++)• {• Seed aSeed = new Seed();• seedList.add(aSeed);• }• }
![Page 28: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/28.jpg)
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;• }
![Page 29: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/29.jpg)
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);• }• }• }• }
![Page 30: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/30.jpg)
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);• }• }
![Page 31: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/31.jpg)
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.
![Page 32: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/32.jpg)
32
ClickHand
ClickHandFrame
ContentPane
+paintComponent()+setCup()
ClickHandPanel
+paintComponent()+repaint()
JPanel
-has1
1
-has1
1
-has added1
1
SeedCup
-has1
2
MouseHandler
-has
1
1
WindowCloser
-has
11WindowAdapter
1
-has instance variable
1
Rectangle
-has
1
1
MouseAdapter
JFrame
ArrayList
-has1
1
Seed
-has1
0..4
MouseMotionHandlerMouseMotionAdapter
-has1
1
![Page 33: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/33.jpg)
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());• }
![Page 34: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/34.jpg)
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());• }• }
![Page 35: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/35.jpg)
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());• }• }• }
![Page 36: 1 Object-Oriented Programming (Java), Unit 20 Kirk Scott](https://reader035.vdocument.in/reader035/viewer/2022062314/56649d835503460f94a68ba3/html5/thumbnails/36.jpg)
36
The End