360 likes | 447 Views
Object-Oriented Programming (Java), Unit 20. Kirk Scott. Clickable Objects. 20.1 Clickable Objects 20.2 Graphical Objects within Objects 20.3 Changing the Mouse Pointer 20.4 Implementation Choices. Toothcomb. Ring-tailed lemur’s toothcomb. Sublingua of a slow loris.
E N D
Object-Oriented Programming (Java), Unit 20 Kirk Scott
Clickable Objects • 20.1 Clickable Objects • 20.2 Graphical Objects within Objects • 20.3 Changing the Mouse Pointer • 20.4 Implementation Choices
ClickCup: • This program has 2 cups in the panel. • Clicking one cup moves its contents to the other. • This is how it appears:
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); • } • }
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); • } • } • }
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); • }
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 • { • } • } • } • }
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.
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:
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.
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); • } • }
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; • }
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); • } • } • } • }
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); • } • }
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.
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()); • }
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()); • } • }
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()); • } • } • }