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
------------------------------------ -
//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
----------------------------- -
-
-
/**
-
* 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
--------------------------------- -
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()
-
}
-
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mike Oliszewski |
last post by:
Given the following c# code:
namespace Company2
{
public class SomeFunctions
{
public void FunctionA()
{
// Do Something.
}
|
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...
|
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.
|
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:
|
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 ?
| |
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;
}
|
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
|
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/...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |