473,769 Members | 1,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Carpet Fishing: Accessing Class Variable Help

1 New Member
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 3191
Nepomuk
3,112 Recognized Expert Specialist
...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
2464
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
5
5461
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) containing the protected variable so that the modification(s) can be done. However, when I try this, I cannot see the public method from the second class (Class B) unless it is declared static. If the method is static, then using...
5
2765
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" runat="server"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> I have the gridview inside of a master page- content hierarchy.
5
2404
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 correct value. Regardless what I set the variable to, the value that is returned for a long is always the same value. What's going on...can anyone help me? A short version of the code follows:
2
2357
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 CCommandMeshToSrf, eg: BOOL m_bHaveAnswer; I want to set this variable from within a dialog class which is defined in separate .h and .cpp files of course. How would one access this varibale from a method defined in my CTestModelessDialog class ?
4
8504
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
1671
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 new to PHP. thanks in advance Dev
0
1206
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 Cleaning, Fabric Lounge Suite Cleaning, Leather Lounge Suite Cleaning, Tile & Grout Cleaning, Mattress Cleaning, Wet Carpet / Water Damage Restoration for: offices, homes, restaurants, clubs and hotels http://carpetcleanersorangecounty.blogspot.com/...
0
932
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 Cleaning, Dry Cleaning, Fabric Lounge Suite Cleaning, Leather Lounge Suite Cleaning, Tile & Grout Cleaning, Mattress Cleaning, Wet Carpet / Water Damage Restoration for: offices, homes, restaurants, clubs and hotels...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.