unit20PowerPointolderversion

Download Report

Transcript unit20PowerPointolderversion

Object-Oriented Programming
(Java), Unit 20
Kirk Scott
1
Clickable Objects
•
•
•
•
20.1
20.2
20.3
20.4
Clickable Objects
Graphical Objects within Objects
Changing the Mouse Pointer
Implementation Choices
2
ClickCup:
• This program has 2 cups in the panel.
• Clicking one cup moves its contents to the other.
• This is how it appears:
3
The structure diagram for
ClickCup:
4
ClickCup
1
-has
1
WindowAdapter
WindowCloser
1
1
ClickCupFrame
JFrame
-has
1
1
-has
1
ContentPane
1
-has added
1
-has instance variable
JPanel
ClickCupPanel
-has
1
+paintComponent()
+repaint()
+paintComponent()
+setCup()
1
1
2
1
ClickCupCup
1
-has
MouseHandler
MouseAdapter
-has
1
Rectangle
5
The sequence diagram for the
input side of ClickCup:
6
instance of MouseHandler
instance of MouseEvent
myPanel
myCupA
cupRectangle
mouseClicked()
getRectangle()
cupRectangle
getPoint()
contains(event.getPoint())
moveCupValueTo(myCupB)
repaint()
7
The sequence diagram for the
output side of ClickCup:
8
myPanel
myCupA
myCupB
g2
repaint()
update()
paintComponent()
super.paintComponent()
drawCup(g2)
draw(cupRectangle)
drawString(Integer...)
drawCup(g2)
drawCup(cupRectangle)
drawString(Integer...)
9
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);
}
}
10
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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);
}
}
}
11
• 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);
•
}
12
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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
{
}
}
}
}
13
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.
14
15
The structure diagram for
ClickDot:
16
ClickDot
1
-has
1
WindowAdapter
1
WindowCloser
1
ClickDotFrame
JFrame
-has
1
1
-has
1
ContentPane
1
1
-has added
-has instance variable
JPanel
ClickDotPanel
11
+paintComponent()
+repaint()
+paintComponent()
+setCup()
-has
1
-has
1
2
SeedCup
MouseHandler
1
-has
1
1
Rectangle
-has
1
ArrayList
1
MouseAdapter
-has
0..4
Seed
17
• 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:
18
myPanel
myCupA
myCupB
g2
instance of Seed
repaint()
update()
paintComponent()
super.paintComponent()
drawCup(g2)
draw(cupRectangle)
*drawSeed(g2)
setColor()
fill()
setColor()
repeat sequence above for myCupB
19
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.
20
• 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);
•
}
• }
21
•
•
•
•
•
•
•
•
•
•
•
•
•
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;
}
22
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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);
}
}
}
}
23
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
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);
}
}
24
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.
25
ClickHand
1
-has
1
WindowAdapter
WindowCloser
1
1
ClickHandFrame
JFrame
-has
1
1
-has
1
ContentPane
1
1
-has added
-has instance variable
JPanel
ClickHandPanel
1
+paintComponent()
+repaint()
+paintComponent()
+setCup()
1
-has
1
MouseMotionAdapter
-has
1
1
-has
1
2
SeedCup
MouseMotionHandler
MouseHandler
MouseAdapter
1
-has
1
1
Rectangle
-has
1
ArrayList
1
-has
0..4
Seed
26
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());
•
}
27
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());
}
}
28
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());
}
}
}
29
The End
30