------------------------------------
CARPET FISHING Class
------------------------------------
Expand|Select|Wrap|Line Numbers
- //GUI IMPORTS
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.math.*;
- /**
- * Write a description of class carpetFishing here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class CarpetFishing extends JPanel implements ActionListener
- {
- private String bait;
- private int score, time;
- private Point playerXY;
- JComboBox baitList;
- GridSquare old;
- public void setBait(String incBait)
- {
- bait = incBait;
- } // setBait()
- public String getBait()
- {
- return bait;
- } // getBait()
- public void setScore(int incScore)
- {
- score = incScore;
- } // setScore()
- public int getScore()
- {
- return score;
- } // getScore()
- public void setBait(Point loc)
- {
- playerXY = loc;
- }
- //GUI
- public static void main(String[] args)
- {
- Fish shark = new Fish("Shark", "Chum", 30, null);
- Fish marlin = new Fish("Marlin", "Tuna", 20, null);
- Fish dolphin = new Fish("Dolphin", "Mackerel", 25, null);
- JFrame frame = new JFrame();
- frame.setContentPane(new CarpetFishing());
- frame.setTitle("Carpet Fishing");
- frame.setPreferredSize(new Dimension(600,595));
- frame.setResizable(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setVisible(true);
- frame.pack();
- }
- public class TimerListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- JOptionPane.showMessageDialog(null,"testing");
- shark.move();
- dolphin.move();
- marlin.move();
- timer.setDelay((int)(50000 * Math.random));
- }
- }
- public CarpetFishing()
- {
- //Make & Start Timer
- Timer timer = new Timer(30000, new TimerListener());
- timer.start();
- this.setLayout(new BorderLayout());
- // make LayoutManagers
- GridLayout gl = new GridLayout(3,3);
- // Pond is a JPanel
- JPanel pond = new JPanel(gl);
- JLabel scorelbl = new JLabel("Score: ");
- //drop down list
- String[] baitStrings = { "Chum", "Tuna", "Mackerel"};
- baitList = new JComboBox(baitStrings);
- baitList.setSelectedIndex(2);
- baitList.addActionListener(this);
- // Makes user select bait right away
- String s = (String)JOptionPane.showInputDialog(null,"Choose Bait:\n","Bait Setting",JOptionPane.PLAIN_MESSAGE,new ImageIcon(""), baitStrings,"Chum");
- baitList.setSelectedItem(s);
- add(pond, BorderLayout.CENTER);
- add(scorelbl, BorderLayout.PAGE_START);
- add(baitList, BorderLayout.PAGE_END);
- GridSquare section1 = new GridSquare(0,0);
- GridSquare section2 = new GridSquare(0,1);
- GridSquare section3 = new GridSquare(0,2);
- GridSquare section4 = new GridSquare(1,0);
- GridSquare section5 = new GridSquare(1,1);
- GridSquare section6 = new GridSquare(1,2);
- GridSquare section7 = new GridSquare(2,0);
- GridSquare section8 = new GridSquare(2,1);
- GridSquare section9 = new GridSquare(2,2);
- pond.add(section1,0,0);
- pond.add(section2,0,1);
- pond.add(section3,0,2);
- pond.add(section4,1,0);
- pond.add(section5,1,1);
- pond.add(section6,1,2);
- pond.add(section7,2,0);
- pond.add(section8,2,1);
- pond.add(section9,2,2);
- section1.addActionListener(this);
- section2.addActionListener(this);
- section3.addActionListener(this);
- section4.addActionListener(this);
- section5.addActionListener(this);
- section6.addActionListener(this);
- section7.addActionListener(this);
- section8.addActionListener(this);
- section9.addActionListener(this);
- /*
- * Set Border Up
- */
- pond.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.blue));
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() == baitList)
- {
- String selected = (String)baitList.getSelectedItem();
- bait = selected;
- JOptionPane.showMessageDialog(null,"Changed bait to: " + selected);
- }
- else
- {
- //removes bait from old section of pond
- if(old != null)
- old.setText("");
- // creates a temporary square as a pointer
- GridSquare tmp = (GridSquare)e.getSource();
- Point newLoc = new Point(tmp.getSQX(),tmp.getSQY());
- // changes official location
- setBait(newLoc);
- // changes text and overrides 'old' location
- tmp.setText("Bait Location");
- old = tmp;
- }
- } // actionPerformed()
- } // carpetFishing
FISH Class
-----------------------------
Expand|Select|Wrap|Line Numbers
- /**
- * Fish: customizable class to create and manipulate fish
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Fish
- {
- private String name, bait;
- private int points;
- private java.awt.Point location;
- public Fish(String incName,String incBait,int incPoints,java.awt.Point incLocation)
- {
- name = incName;
- bait = incBait;
- points = incPoints;
- location = incLocation;
- } // Fish
- public void move()
- {
- location.x = (int)Math.random() * 3;
- location.y = (int)Math.random() * 3;
- } // move()
- public String getName()
- {
- return name;
- } // getName()
- public String getBait()
- {
- return bait;
- } // getBait()
- public int getPoints()
- {
- return points;
- } // getPoints()
- public java.awt.Point getLocation()
- {
- return location;
- } // getLocation()
- }
GRIDSQUARE Class
---------------------------------
Expand|Select|Wrap|Line Numbers
- import javax.swing.*;
- import java.awt.*;
- public class GridSquare extends JButton
- {
- private int sqx,sqy;
- public GridSquare(int iX, int iY)
- {
- super();
- sqx = iX;
- sqy = iY;
- } // gridSquare()
- public int getSQX()
- {
- return sqx;
- } // getSQX()
- public int getSQY()
- {
- return sqy;
- } // getSQY()
- }