SRL Archive - Java Help and Tutorials

need java help


JAD

2008-01-27 17:44:18

Hmm.. So I'm making a java applet mini game (I'm a java noob :p) and I'm having problems with this:


public void mouseClicked(MouseEvent me)
{
while(x1 > 21 && x1 < 54)
{
blah = true;
repaint();
}
}

So, what that's supposed to do is this. In my paint method, I have just a circle moving back and forth across the screen, buy subtracting and adding to the x1 variable. When the circle (x1) is between 21 and 54 x coordinate, and the user clicks the mouse, I want the script to do what I put between the brackets. I tried it, and it doesn't work. I had the script do something if blah was true and it never did it because I probably did something wrong.

Any help would be appreciated.

I'm a little confused when working with applets because there's no "main method" with them. I really don't think I get when MouseClicked is called. Will it just do what was put in that method when the mouse is clicked or do I have to call for MouseClicked in the paint or run methods?

Thanks.

Town

2008-01-27 19:23:38

Huh? Did you assign an action listener to the mouse button you want or did you just make a function called mouseClicked?

Buckleyindahouse

2008-01-27 20:14:50

well, i think java is the same as C# which i learned quite a bit now.

in order to delcare booleans i think u have to double this (=).

so….

blah == true;


if that worked and if u need more help add my msn.

[email]bigbwoi2001@hotmail.com[/email]

Dan Cardin

2008-01-27 21:06:29

blah = true sets blah equal to true

blah == true see's whether blah equals true or not

JAD

2008-01-28 18:01:54

From: Town
Huh? Did you assign an action listener to the mouse button you want or did you just make a function called mouseClicked?


It's in some include that I included at the top of my script. Can't think of the name of it right now though, and I'm not on that computer. But there's a few of these inside of it. MouseClicked, MouseEntered, MouseExited…etc.

and no, you don't use "==" in java when changing the value of something. Only for if(integer == 1); like that.

Trifonius

2008-01-28 19:27:10

Ehm could you please post the rest of your code? Its kinda impossible to find the problem now…

mpd

2008-01-29 04:35:57

    public void mouseClicked(MouseEvent me)
{
while(x1 > 21 && x1 < 54)
{
blah = true;
repaint();
[B]changeMyValue(x1);[/B]
}
}

when you run that loop, it doesnt increase & decrease x1
you should assing a value to x1 in order to be able to do the loop

try adding a function like changeMyValue that changes the value of x1
and make sure x1 > && x1 < 54 when you call it.

cant think of anything else

JAD

2008-01-29 17:39:41

From: mpd
    public void mouseClicked(MouseEvent me)
{
while(x1 > 21 && x1 < 54)
{
blah = true;
repaint();
[B]changeMyValue(x1);[/B]
}
}

when you run that loop, it doesnt increase & decrease x1
you should assing a value to x1 in order to be able to do the loop

try adding a function like changeMyValue that changes the value of x1
and make sure x1 > && x1 < 54 when you call it.

cant think of anything else


I'll have to post the code when I get on my comp that has it… But I have in the run method adding and decreasing x already.

JAD

2008-02-01 13:29:26

Here's the code:



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyGame extends JApplet implements Runnable
{
private int x1;
private int x2;
private boolean blah;
Thread animator;

public void init()
{
JRootPane rootPane = this.getRootPane();
rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
}

public void start()
{
x1 = 250;
x2 = 40;
blah = false;
animator = new Thread(this);
animator.start();
}

public void stop()
{

}

public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.drawRect(25, 100, 56, 300);
g.setColor(Color.black);
g.drawRect(700, 100, 60, 300);
g.fillOval(x1, 200, x2, 40);
if(blah == true)
{
g.fillOval(50,50,80,80);
}
}

public void run()
{
while(Thread.currentThread() == animator)
{
x1 = x1 - 5;
repaint();

if(x1 < 5)
{
while(x1 < 780)
{
x1 = x1 + 5;
repaint();
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
break;
}
}
}
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
break;
}
}
}
public void mouseClicked(MouseEvent me)
{
while(x1 > 21 && x1 < 54)
{
blah = true;
repaint();
}
}
}

The part I'm having trouble with is the mouseClicked thing… I want it so that when the mouse is clicked when the x1 variable is between 21 and 54, it does whatever I put between the brackets. Any help is appreciated!

Thanks.

Yakman

2008-02-01 15:21:18

change

public class MyGame extends JApplet implements Runnable

to


public class MyGame extends JApplet implements Runnable, MouseListener

and add the following (you have to add these to make it compile, even if you wont use them)


public void mousePressed(MouseEvent me) { }
public void mouseReleased(MouseEvent me) { }
public void mouseEntered(MouseEvent me) { }
public void mouseExited(MouseEvent me) { }

you might also find it helpful to know that to find the location of the mouse click, you do me.getX() and me.getY()

JAD

2008-02-01 16:13:13

Gah… That one line (the program name) did it! Thanks a lot! :D

How do you do IntToStr in java? I want to draw a string that's an integer..

Yakman

2008-02-01 17:50:57

From: JAD
How do you do IntToStr in java? I want to draw a string that's an integer


you just join up strings with +, the java compiler sorts it all out


String myStr = "hello";
int k = 5;
String newStr = myStr + " world" + k;

and it works.

but under the hood, the compiler turns it into this

String myStr = "hello";
int k = 5;
String newStr = new StringBuilder().append(myStr).append(" world").append(k).toString();

theres a bit of information you'll probably never need :P

JAD

2008-02-01 18:32:31

K, ty. Now that I figured that out though… I'm still advancing my game and I got a new problem (the game works now btw! But I'm trying to make it cooler, because it's pretty lame right now lol).



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class MyGame extends JApplet implements Runnable, MouseListener
{
private int x1;
private int x2;
Thread animator;
private int clicks;
private String clickNum;
private boolean clicked;
private boolean clicked2;
private boolean red;
private Random r;

public void init()
{
JRootPane rootPane = this.getRootPane();
rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
addMouseListener(this);
}

public void start()
{
x1 = 250;
x2 = 40;
clicks = 0;
red = false;
animator = new Thread(this);
animator.start();
}

public void stop()
{

}

public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
g.drawRect(15, 100, 66, 300);
g.setColor(Color.black);
g.drawRect(350, 100, 7, 300);
if(x1 > 5 && x1 < 67)
{
g.setColor(Color.blue);
}
/* if(r.nextInt(69) == 3)
{
g.setColor(Color.red);
red = true;
} */
g.fillOval(x1, 200, x2, 40);
g.setColor(Color.black);
clickNum = Integer.toString(clicks);
g.drawString(clickNum,100,100);
}

public void run()
{
while(Thread.currentThread() == animator)
{
x1 = x1 - 4;
repaint();

if(x1 < 5)
{
while(x1 < 320)
{
x1 = x1 + 4;
repaint();
try
{
Thread.sleep(25);
}
catch (InterruptedException e)
{
break;
}
}
}
try
{
Thread.sleep(25);
}
catch (InterruptedException e)
{
break;
}
}
}
public void mouseClicked(MouseEvent me)
{

}

public void mousePressed(MouseEvent me)
{
if(red == true)
{
clicks = clicks - 50;
}
else
if(x1 > 5 && x1 < 67)
{
clicks = clicks + 1;
}
else
{
clicks = clicks - 1;
}
}

public void mouseReleased(MouseEvent me)
{

}

public void mouseEntered(MouseEvent me)
{

}

public void mouseExited(MouseEvent me)
{

}
}

Right now, the game works. If the user clicks the mouse button when the oval is in the rectangle, it will add 1 to the score, and minus one if they click and it's not in the rectangle. But I want the script to randomly make the oval red and then it will minus 50 from your points if you click when the oval is read (I may change this later).

Now, it works, but when I take the comments out on that commented chunk in the paint method, the oval disappears and the score disappears. Basically, it wouln't draw anything after that commented chunk so the game doesn't work. I'm really confused as to why that's messing it up.

Thanks again for the help. You'd probably be able to help me better if you tried running it in BlueJ or something in applet viewer so you see what I mean/how it works :)

eurostylz

2008-02-02 16:36:01

Im not that familiar with java applets more of the programs but as others mentioned in java "=" sets a variable and "==" checks to see if there equal, if your trying to compara an array i believe you have to call a method called ".compare" or it was ".compareto", i cant remember which one, and instead of making the private variables private, make them protected, it might work, and when your bumping a counter to save space you could have done "clicks++", or "clicks–"