1 object-oriented programming (java), unit 20 kirk scott
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
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
12
The sequence diagram for the input side of ClickCup:
13
instance of MouseHandler instance of MouseEvent myPanel myCupA cupRectangle
mouseClicked()
getRectangle()
cupRectangle
getPoint()
contains(event.getPoint())
moveCupValueTo(myCupB)
repaint()
14
The sequence diagram for the output side of ClickCup:
15
myPanel myCupA myCupB g2
drawCup(g2)
draw(cupRectangle)
drawString(Integer...)
update()
paintComponent()
super.paintComponent()
drawCup(g2)
drawCup(cupRectangle)
drawString(Integer...)
repaint()
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
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
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
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()
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<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);• }• }
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
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
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