473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Carpet Fishing: Accessing Class Variable Help

I'm working on a game called carpet fishing...long story...anyways I am having trouble working with the fish that I've created. I need to be able to access their Fish Class method move() so I can keep the fish placement random and changing. Also I want to be able to change the timer delay randomly...but both of these have to be triggered by a listener that's listening to a timer object. I'm beyond stumped and I would greatly appreciate a nudge in the right direction.

------------------------------------
CARPET FISHING Class
------------------------------------

Expand|Select|Wrap|Line Numbers
  1. //GUI IMPORTS                                                                                 
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5. import java.math.*;
  6.  
  7. /**
  8.  * Write a description of class carpetFishing here.
  9.  * 
  10.  * @author (your name) 
  11.  * @version (a version number or a date)
  12.  */
  13. public class CarpetFishing extends JPanel implements ActionListener
  14. {
  15.     private String bait;
  16.     private int score, time;
  17.     private Point playerXY;
  18.     JComboBox baitList;
  19.     GridSquare old;
  20.     public void setBait(String incBait)
  21.     {
  22.         bait = incBait;
  23.     } // setBait()
  24.     public String getBait()
  25.     {
  26.         return bait;
  27.     } // getBait()
  28.     public void setScore(int incScore)
  29.     {
  30.         score = incScore;
  31.     } // setScore()
  32.     public int getScore()
  33.     {
  34.         return score;
  35.     } // getScore()
  36.     public void setBait(Point loc)
  37.     {
  38.         playerXY = loc;
  39.     }
  40.  
  41.  
  42.     //GUI
  43.     public static void main(String[] args)
  44.     {
  45.         Fish shark = new Fish("Shark", "Chum", 30, null);
  46.         Fish marlin = new Fish("Marlin", "Tuna", 20, null);
  47.         Fish dolphin = new Fish("Dolphin", "Mackerel", 25, null);
  48.         JFrame frame = new JFrame();
  49.         frame.setContentPane(new CarpetFishing());
  50.         frame.setTitle("Carpet Fishing");
  51.         frame.setPreferredSize(new Dimension(600,595));
  52.         frame.setResizable(true);
  53.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  54.         frame.setVisible(true);
  55.         frame.pack();
  56.  
  57.     }
  58.  
  59.     public class TimerListener implements ActionListener
  60.     {
  61.         public void actionPerformed(ActionEvent e)
  62.         {
  63.             JOptionPane.showMessageDialog(null,"testing");
  64.             shark.move();
  65.             dolphin.move();
  66.             marlin.move();
  67.             timer.setDelay((int)(50000 * Math.random));
  68.  
  69.  
  70.         }
  71.     }
  72.  
  73.  
  74.     public CarpetFishing()
  75.     {
  76.  
  77.         //Make & Start Timer
  78.         Timer timer = new Timer(30000, new TimerListener());
  79.         timer.start();
  80.  
  81.         this.setLayout(new BorderLayout());
  82.         // make LayoutManagers
  83.         GridLayout gl = new GridLayout(3,3);
  84.  
  85.         // Pond is a JPanel
  86.         JPanel pond = new JPanel(gl);
  87.         JLabel scorelbl = new JLabel("Score: ");
  88.         //drop down list
  89.         String[] baitStrings = { "Chum", "Tuna", "Mackerel"};
  90.         baitList = new JComboBox(baitStrings);
  91.         baitList.setSelectedIndex(2);
  92.         baitList.addActionListener(this);
  93.  
  94.         // Makes user select bait right away
  95.         String s = (String)JOptionPane.showInputDialog(null,"Choose Bait:\n","Bait Setting",JOptionPane.PLAIN_MESSAGE,new ImageIcon(""), baitStrings,"Chum");
  96.         baitList.setSelectedItem(s);
  97.  
  98.  
  99.         add(pond, BorderLayout.CENTER);
  100.         add(scorelbl, BorderLayout.PAGE_START);
  101.         add(baitList, BorderLayout.PAGE_END);
  102.  
  103.  
  104.  
  105.         GridSquare section1 = new GridSquare(0,0);
  106.         GridSquare section2 = new GridSquare(0,1);
  107.         GridSquare section3 = new GridSquare(0,2);
  108.         GridSquare section4 = new GridSquare(1,0);
  109.         GridSquare section5 = new GridSquare(1,1);
  110.         GridSquare section6 = new GridSquare(1,2);
  111.         GridSquare section7 = new GridSquare(2,0);
  112.         GridSquare section8 = new GridSquare(2,1);
  113.         GridSquare section9 = new GridSquare(2,2);
  114.  
  115.         pond.add(section1,0,0);
  116.         pond.add(section2,0,1);
  117.         pond.add(section3,0,2);
  118.         pond.add(section4,1,0);
  119.         pond.add(section5,1,1);
  120.         pond.add(section6,1,2);
  121.         pond.add(section7,2,0);
  122.         pond.add(section8,2,1);
  123.         pond.add(section9,2,2);
  124.  
  125.         section1.addActionListener(this);
  126.         section2.addActionListener(this);
  127.         section3.addActionListener(this);
  128.         section4.addActionListener(this);
  129.         section5.addActionListener(this);
  130.         section6.addActionListener(this);
  131.         section7.addActionListener(this);
  132.         section8.addActionListener(this);
  133.         section9.addActionListener(this);
  134.  
  135.         /*
  136.          *      Set Border Up
  137.          */
  138.         pond.setBorder(BorderFactory.createMatteBorder(3, 3, 3, 3, Color.blue));
  139.  
  140.     }
  141.  
  142.  
  143.  
  144.  
  145.     public void actionPerformed(ActionEvent e)
  146.     {
  147.         if(e.getSource() == baitList)
  148.         {
  149.             String selected = (String)baitList.getSelectedItem();
  150.             bait = selected;
  151.             JOptionPane.showMessageDialog(null,"Changed bait to: " + selected);
  152.         }
  153.         else
  154.         {
  155.             //removes bait from old section of pond
  156.             if(old != null)
  157.                 old.setText("");
  158.  
  159.             // creates a temporary square as a pointer
  160.             GridSquare tmp = (GridSquare)e.getSource();
  161.             Point newLoc = new Point(tmp.getSQX(),tmp.getSQY());
  162.             // changes official location
  163.             setBait(newLoc);
  164.             // changes text and overrides 'old' location
  165.             tmp.setText("Bait Location");
  166.             old = tmp;
  167.  
  168.         }
  169.     } // actionPerformed()
  170.  
  171. } // carpetFishing
  172.  
  173.  
-----------------------------
FISH Class
-----------------------------
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. /**
  4.  * Fish: customizable class to create and manipulate fish
  5.  * 
  6.  * @author (your name) 
  7.  * @version (a version number or a date)
  8.  */
  9. public class Fish
  10. {
  11.     private String name, bait;
  12.     private int points;
  13.     private java.awt.Point location;
  14.  
  15.     public Fish(String incName,String incBait,int incPoints,java.awt.Point incLocation)
  16.     {
  17.        name = incName;
  18.        bait = incBait;
  19.        points = incPoints;
  20.        location = incLocation;
  21.     } // Fish
  22.     public void move()
  23.     {
  24.         location.x = (int)Math.random() * 3;
  25.         location.y = (int)Math.random() * 3;
  26.     } // move()
  27.     public String getName()
  28.     {
  29.         return name;
  30.     } // getName()
  31.     public String getBait()
  32.     {
  33.         return bait;
  34.     } // getBait()
  35.     public int getPoints()
  36.     {
  37.         return points;
  38.     } // getPoints()
  39.     public java.awt.Point getLocation()
  40.     {
  41.         return location;
  42.     } // getLocation()
  43. }
  44.  
  45.  
  46.  
---------------------------------
GRIDSQUARE Class
---------------------------------

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class GridSquare extends JButton
  5. {
  6.     private int sqx,sqy;
  7.  
  8.     public GridSquare(int iX, int iY)
  9.     {
  10.         super();
  11.         sqx = iX;
  12.         sqy = iY;
  13.     } // gridSquare()
  14.     public int getSQX()
  15.     {
  16.         return sqx;
  17.     } // getSQX()
  18.     public int getSQY()
  19.     {
  20.         return sqy;
  21.     } // getSQY()
  22. }
  23.  
  24.  
Sep 11 '08 #1
1 3157
Nepomuk
3,112 Expert 2GB
...but both of these have to be triggered by a listener that's listening to a timer object. I'm beyond stumped and I would greatly appreciate a nudge in the right direction...
Where exactly are you stuck? It's a bit much code to just go through, if you don't even know what exactly you're looking for. Please give us relative code instead of all of it, so that we can help you.

Greetings,
Nepomuk
Sep 12 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
5
by: Ben | last post by:
Hello I have a protected variable in a class (Class A) that I need to call from another class (Class B) to modify. I thought what I had to do was create a public method in the class (Class A)...
5
by: Siva | last post by:
Hello I have a dropdownlist inside the gridview as a template column defined as follows: <asp:TemplateField HeaderText="Choose Location"> <ItemTemplate> <asp:DropDownList ID="ddlChooseLoc"...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
2
by: Jurek Dabrowski | last post by:
hi all, I have a question in reference to accessing variables in another class maybe someone has dealt with before. I have some public variables declared in my main plug-in class...
4
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
21
by: Dev | last post by:
Dear All, I have created a website. in the contact us page i want to send all information (Entered by visitor in text boxex) directly to my e-mail address. to do this i have no idea because i am...
0
by: Now You Know | last post by:
Carpet Cleaners Los Angeles Home Carpet Rug Upholstery Cleaning Phone 1 310 925 1720 OR 1-818-386-1022 Local Call California Wide We offer carpet cleaning services such as; Steam Cleaning, Dry...
0
by: Now You Know | last post by:
Carpet Cleaners Los Angeles Home Carpet Rug Upholstery Cleaning Home Office Phone 1 310 925 1720 OR 1-818-386-1022 Local Call California Wide We offer carpet cleaning services such as; Steam...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.