PDA

View Full Version : Nooby java applet problem



JAD
02-02-2008, 10:19 PM
Well, here's the code:



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

public class MyFightingGame extends JApplet implements Runnable, MouseListener, MouseMotionListener
{
private int x1;
private int x2;
Thread animator;
private String clickNum;
private boolean first;
private boolean clicked;
// private Random r;

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

public void start()
{
animator = new Thread(this);
animator.start();
first = true;
clicked = false;
}

public void stop()
{
animator = null;
}

public void mainMenu(Graphics g)
{
if(first == true)
{
g.setColor(Color.blue);
Font myFont = new Font("Times New Roman", Font.BOLD, 30);
g.setFont(myFont);
g.drawString("Click anywhere to play!", 100, 200);
while(clicked == false && Thread.currentThread() == animator)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
break;
}
}
first = false;
g.drawString("hi there",17,17);
}

}

public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
mainMenu(g);
}

public void run()
{
while(Thread.currentThread() == animator)
{
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
break;
}
repaint();
}

}

public void mouseDragged(MouseEvent me)
{
}

public void mouseMoved(MouseEvent me)
{
}

public void mouseEntered(MouseEvent me)
{
}

public void mouseExited(MouseEvent me)
{
}

public void waitMainMenu()
{
clicked = true;
}

public void mousePressed(MouseEvent me)
{
waitMainMenu();
}

public void mouseReleased(MouseEvent me)
{
}

public void mouseClicked(MouseEvent me)
{
}
}


It's nothing really yet, I just started writing something and already I'm having problems. When I run the applet, it's supposed to do the mainmenu method once, and what the mainmenu method is supposed to do is draw the string "Click anywhere to play!" and wait until clicked = true (clicked is supposed to equal true when the mouse is clicked anywhere in the applet), then draw the string "hi there". But what it does is draws both strings because for some reason it's not waiting for the mouse to be clicked :confused:

In theory this should work, but I don't know what the problem is.

Thanks in advance. Any help/ideas/suggestions are greatly appreciated!

~JAD

Yakman
02-03-2008, 12:35 PM
you are calling mainMenu() from paint()

theres a few things you should know about paint()

you know when there is something drawn on an applet? when the applet is hidden, this drawing is lost, when the applet is shown again, it needs to draw itself again.
the operating system calls paint() when the applet needs to draw itself, that could be when the applet is first started, or when a user drags a window over the applet, or when the applet is minimised then restored.

it is impossible to predict when paint() will be invoked, in the same way its impossible to predict when mouseMoved() or whatever.

i see you're starting another thread to call repaint() every so often, i dont think its a very good idea, just call repaint() when something actually changes, like



//..something changes here
//oneVariable = 5;
repaint();


theres a few other stuff that arnt right in your applet, like you have a time-consuming loop in your event-dispatch thread, which would freeze the applet until the loop is finished.

heres a small applet i quickly wrote for you, you might learn from it, it uses things you use in your applet (storing the applets state, responding the mouse events)


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

/**
* Applet which waits for user to click mouse and repaints itself
* can be run as applet and application
* @author yakman
*/
public class StateMachineApplet extends Applet implements MouseListener {

public static void main(String[] args) {

final Applet app = new StateMachineApplet();

Frame frame = new Frame("StateMachineApplet");
frame.setSize(400, 400);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
app.destroy();
System.exit(0);
}
});
frame.setLayout(new BorderLayout());
frame.add(app, BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
app.init();
}

private String message;
private int lastX;
private int lastY;
private Color color;

@Override
public void init() {
lastX = getWidth() / 2;
lastY = getHeight() / 2;
color = Color.getHSBColor((float)Math.random(), 0.7F, 0.7F);
message = "First message";
addMouseListener(this);
}

@Override
public void destroy() {
removeMouseListener(this);
}

@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());

FontMetrics fm = g.getFontMetrics();
g.setColor(color);
g.drawString(message, lastX - fm.stringWidth(message)/2, lastY);
}

public void mousePressed(MouseEvent e) {
lastX = e.getX();
lastY = e.getY();
message = "You clicked " + lastX + ", " + lastY + "!";
color = Color.getHSBColor((float)Math.random(), 0.7F, 0.7F);
repaint();
}

public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}

JAD
02-03-2008, 05:02 PM
Thanks sooo much! :) I finally fixed it, and now I get the paint method, unlike before.

Thanks again yakman! :)

Yakman
02-03-2008, 06:16 PM
you dont have to quote everything i say, it makes the post really long and annoying, just so you can add two lines of your own text at the bottom.
iv edited out the times you did it, its easier on the eyes i think.

red eyes 20
02-13-2008, 08:15 AM
wow yak, your 1337
i've just been loytering here cause i want to learn java, so i couldn't resist saying im impressed.