1 / 36

Object-Oriented Programming (Java), Unit 20

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.

amory
Download Presentation

Object-Oriented Programming (Java), Unit 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  3. Toothcomb

  4. Ring-tailed lemur’s toothcomb

  5. Sublingua of a slow loris

  6. Philippine flying lemur (colugo) mother with baby

  7. Colugo (Singapore)

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

  9. The structure diagram for ClickCup:

  10. The sequence diagram for the input side of ClickCup:

  11. The sequence diagram for the output side of ClickCup:

  12. 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); • } • }

  13. 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); • } • } • }

  14. 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); • }

  15. 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 • { • } • } • } • }

  16. 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.

  17. The structure diagram for ClickDot:

  18. 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:

  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. 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()); • }

  26. 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()); • } • }

  27. 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()); • } • } • }

  28. The End

More Related