I got a problem I couldn't get to work after about 2 Hours of trying. I want to have a loop that do 2 Methods (Draw and update) but also listen to Mouse/Keyboard events. I have a loop that Draws and Updates, but does nothing outside of the loop ( Listening to events ) I tried alot of things but nothing worked. Help Please?
I tried using the Runnable Thread, using different orders, using wait() and notify(), I've tried alot of things. But basicly I want to know how to run a loop and still check for User Input
Also when I try to quit the program clicking the red "X", it won't quit but still work
Here's the Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class main extends Applet implements MouseListener, Runnable {
public main() {
super();
init();
}
Thread t;
Screen screen = new Screen();
String Text = "Hello";
boolean Running = true;
boolean Click = false;
int R = 0x00;
int G = 0x00;
int B = 0x00;
int xpoints[] = {25, 40, 40, 25, 25};
int ypoints[] = {40, 40, 25, 25, 25};
int npoints = 5;
public void run() {
while (Running) {
GameLoop();
}
}
public void init() {
this.addMouseListener(this);
this.setSize(400, 300); //manually set your Frame's size
t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
g.setColor(new Color(R, B, G));
g.fillPolygon(xpoints, ypoints, npoints);
Running = true;
t.run();
}
public void mousePressed(MouseEvent e) { //On Mouse Click
System.exit(0);
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
System.exit(0);
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public boolean keyDown(Event e, int key) {
return true;
}
public void GameLoop() {
if (Running) {
if (R != 0xff) {
R++;
} else {
if (G != 0xff) {
G++;
} else {
if (B != 0xff) {
B++;
} else {
System.exit(0);
}
}
}
try {
sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
paint(getGraphics());
}
}
public void sleep(int time) throws InterruptedException {
Thread.sleep(time, 0);
}
}
This tutorial should provide some insight as to how your program should be structured. And this one is helpful for the mouse listener.
Issues you should address:
1) You're doing something fishy with the paint method. Why are you calling t.run() in there? The thread t is already running and looping constantly calling the paint() method to redraw the screen. Remove this call and see what you get.
1) The destruction of your thread/applciation is poor. The first example above provides a better way for that to occur
2) You have your System.Exit(0) on mousePressed() with the comment //on mouse click but nothing in mouseClicked()... it works but its bad convention
3)Having your class named main is extremely poor convention that is both confusing and impractical. Rename your class to something like "Game" or similar.
4) Why declare Screen if you don't use it?
I see that you define a Running variable to be true upon initialization. This variable is used to determine whether or not the game should stop. I, however, don't see any place where you modify the value of this variable to false. This would explain why your game never exits.
As for the the game not working, try debugging the application in an IDE. You should then pay attention to what, if any, Exception are being thrown and the values of any variables you are questioning. Hopefully this will give you insight into the behavior of your app.
Don't forget to update us with any new info you discover so we can help you out along the way.
Related
I have been trying for hours to figure out my mistake in a simple program in java. I wish to display the change in backgroundcolor after 200 ms so I wrote this program but it's not showing correct output.
It's a long program ahead thanks in advance for reading.
public class bgColor extends Applet implements Runnable
{
Thread timer=null;
int c,flag;
public void init()
{
c=0;
setBackground(new Color(255,255,255));
}
public void start()
{
if(timer==null)
{
timer=new Thread(this);
timer.start();
}
}
public void paint(Graphics g)
{
switch(c)
{
case 0:setBackground(Color.white);
break;
case 1:setBackground(Color.blue);
break;
case 2:setBackground(Color.green);
break;
case 3:setBackground(Color.red);
break;
}
if(c==4)
{
flag=0;
}
else if(c==0)
{
flag=1;
}
if(flag==0)
{
c--;
}
else if(flag==1)
{
c++;
}
}
public void stop()
{
timer=null;
}
public void run()
{
if(timer!=null)
{
repaint();
c++;
try
{
Thread.sleep(200);
}
catch(InterruptedException e){}
}
timer=null;
}
}
I basically set up a switch for changing background Color after certain intervals but it's showing only red Color throughout.
It would also be of enormous help if someone could suggest where I can read up how the program runs step by step like I think probably init(), then start() then run() then paint() then stop() but it's purely my guess. I need an authentic source.
Have you tried printing c?
The thread runs more often than paint since even if you call repaint a million times, it only paints at most once per frame. The sleep helps, but it only reduces the risk of a race condition instead of eliminating the issue.
You also have a race condition where c can go above 4 and flag will never toggle. Try using c >= 4 and c <= 0 instead to avoid this. Based on your description, this is probably your issue.
I have been working on a basic Applet (not JApplet, I don't know why my instructor told me to use AWT...). It's a simple game that has a ball bouncing across the screen, collecting dots.
I recently added a menu, and it seems to be all working ok, but when I press "play" the game starts, and freezes on the first frame. I will paste a skeleton of the code below, cutting out what I assume to be irrelevant to the question. Please forgive my Syntax and perhaps ignorance as I have only been programming for a week or two.
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Menu;
import java.awt.event.MouseEvent;
public class StartingPoint extends Applet implements Runnable
{
private Graphics doubleG;
Ball b;
boolean gameOver = false;
Menu menu = new Menu();
private enum STATE
{
MENU,
GAME,
};
public static STATE State =STATE.MENU;
// more irrelevant variables below. will comment "blah blah" when cut has been made.
#Override
public void init()
{
setSize(800,600);
// blah blah
}
#Override
public void start()
{
b = new Ball();
// blah blah
Thread thread = new Thread(this);
thread.start();
}
#Override
public void run()
{
if(State == STATE.GAME)
{
while(true)
{
b.update(this);
// blah blah
repaint();
try
{
Thread.sleep(17);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
else if(State == STATE.MENU)
{
while(true)
{
repaint();
try
{
Thread.sleep(17);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
public void update(Graphics g)
{
if(i == null)
{
i = createImage(this.getSize().width, this.getSize().height);
doubleG = i.getGraphics();
}
doubleG.setColor(getBackground());
doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
doubleG.setColor(getForeground());
paint(doubleG);
g.drawImage(i, 0, 0, this);
}
public void paint (Graphics g)
{
if(State == STATE.GAME)
{
b.paint(g);
//blah blah
}
else if(State ==STATE.MENU)
{
menu.render(g);
}
}
#Override
public void mouseClicked(MouseEvent e)
{
if(mouseIn)
{ //mouseIn is a boolean that checks if Try Again is clicked on lose
if(b.getScore() == -1)
{
b = null;
b = new Ball();
b.setScore(0);
}
if(State == STATE.MENU)
{
if(menu.isPlayb() == true)
{ //playb=boolean to check if "play" is clicked on menu.
State = STATE.GAME;
menu.setPlayb(false);
}
}
}
}
}
It's still quite long sorry, I tried to cut as much as possible.
Could someone please give me a hand here? It would be much appreciated.
Here is some more info about the way the program behaves:
If the initial state is set to game, the applet runs just as it did without a menu.
There is no error given when the game pauses after the play button is pushed.
I have a guess that the problem lies in that after the play button sets the state to STATE.GAME, the beginning of the code sets it back to menu again. No idea how accurate of a guess this is though, as I have tried many ways of avoiding writing STATE.MENU at the beginning of the code, to no different result. (I tried booleans, and If/else).
Please help if you can!
Thanks so much.
Also, if you need any more info, I will provide :)
Thanks for trying to help everyone! After all the effort of attempting to solve the problem, the answer was simple. I just needed to run my start method again when the play button is pushed....
Thanks :)
There is a problem with the repaint() method in Java. I made a new thread that constantly repaints the screen. When I release the spacebar I want my player to fall smoothly by setting its position and then waiting for 50 milliseconds and looping that 20 times. Instead, it waits the whole amount of time in the loop, then repaints. I am wondering why it doesn't constantly repaint the changes in the players co-ordinates. Thank you.
(Edit) Thanks everyone for the help. This is my first time using stack overflow, and I am only 13 and still learning java, so I probably will go back to the tutorials again.
My 'a' class (main):
public class a {
public static void main(String[] args) {
JFrame frame = new JFrame("StickFigure Game");
frame.setSize(740, 580);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
b board = new b();
frame.add(board);
frame.addKeyListener(board);
}
}
My 'b' class (JPanel/drawing):
public class b extends JPanel implements KeyListener {
c player = new c();
public class MyRunnable implements Runnable {
public void run() {
while (true)
repaint();
}
}
MyRunnable run = new MyRunnable();
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(player.getImage(), player.getX(), player.getY(), 80, 140,
null);
}
public b() {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
public static void slow(int n) {
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while (t1 - t0 < n);
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_D) {
player.setPos(player.getX() + 6, player.getY());
}
if (e.getKeyCode() == KeyEvent.VK_A) {
player.setPos(player.getX() - 6, player.getY());
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
player.setPos(player.getX(), player.getY() - 60);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
for (int i = 0; i < 20; i++) {
slow(50);
player.setPos(player.getX(), player.getY() + 2);
}
}
}
public void keyTyped(KeyEvent e) {
}
}
my 'c' class (player):
public class c {
private ImageIcon i = new ImageIcon("guy.png");
private Image img = i.getImage();
private int x = 0;
private int y = 100;
public void wait(int what) {
try {
Thread.sleep(what);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public c() {
}
public Image getImage() {
return img;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setPos(int mx, int my) {
x = mx;
y = my;
}
}
I haven't gone through all the code here but here are some pointers:
Swing has its own concurrency mechanisms which allow you to handle UI updates. You can use a Swing Timer rather than a raw Thread. Related is the use of Thread.sleep - don't do this, it only blocks the EDT and prevents UI updates.
The Swing paint chain mechanism requires you to override paintComponent rather than paint.
Always use Key Bindings rather than KeyListeners in Swing. KeyListeners require component focus to work to interact with the KeyEvents. Key Bindings do not have this limitation.
"There is a problem with the repaint() method in java." Did you consider that perhaps the problem is with your code instead? You are blocking the event thread and giving the system no time to do the intermediate repaints. In particular, this method:
public static void slow (int n){
long t0,t1;
t0=System.currentTimeMillis();
do{
t1=System.currentTimeMillis();
}
while (t1-t0<n);
}
and this loop:
for(int i = 0;i<20;i++){
slow(50);
player.setPos(player.getX(), player.getY()+2);
}
do not relinquish control to the system so that repaints can actually happen. Rewrite those using Swing timers. Look at this tutorial for an introduction on how to use these.
Also, your thread that constantly calls repaint() in a tight loop:
public void run(){
while(true) repaint();
}
is a terrible idea. You don't need to call repaint() at full CPU speed. Once every 30 milliseconds or so is fine for animation. Again, consider using Swing utilities to do this rather than writing your own looping thread.
The repaint is only a "request" to paint as soon as possible. so when you call it it causes a call to the paint method as soon as possible.
from here
So basically you just flooding the scheduled calls of paint or update with while(true) repaint();.
Oracle's stance on painting in AWT and Swing
One way you could do it, or should I say how I would do it, is to make your c class implement KeyListener, so that when a key is pressed (and only when it is pressed) you update it's location.
So move your KeyListener methods to class c, in your class b constructor you can add the call this.addKeyListener(player) or make a method void addPlayer(c player) that adds it.
What im trying to do is pretty simple, I want to show the steps of an algorithm on the screen, hence why im trying to combine repaint() with sleep(), but I am doing it wrong, Id love it if someone knows enough about it to firstly explain whats wrong with this code, and secondly, what do i do to make it work...
thanks!
in summery, what this code was meant to do is paint 10 red vertices, then balcken em one by one in intervals of 200 milliseconds.
here's the code:
public class Tester {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ShowGUIGraph();
}
});
}
private static void ShowGUIGraph() {
JFrame f = new JFrame("something");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(BorderLayout.CENTER,new SomePanel());
f.add(p);
f.setPreferredSize(new Dimension(800,600));
f.pack();
f.setVisible(true);
}
}
public class SomePanel extends JPanel {
private static final long serialVersionUID = 1L;
LinkedList<Vertex> vertices=new LinkedList<Vertex>();
public SomePanel () {
for (int i=0;i<10;i++) {
Vertex v=new Vertex(i);
v.setLocation(20+30*i, 20+30*i);
vertices.add(v);
}
traverseVerticesRecoursive(0);
traverseVerticesNonRecoursive();
}
public void traverseVerticesRecoursive(int i) {
if (i>=vertices.size()) return;
vertices.get(i).setColor(Color.black);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
traverseVerticesRecoursive(i+1);
}
public void traverseVerticesNonRecoursive() {
for (int i=0;i<10;i++) {
vertices.get(i).setColor(Color.red);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i=0;i<vertices.size();i++) {
vertices.get(i).paintVertex(g);
}
}
}
public class Vertex {
private int x,y,tag,r=20;
private Color color=Color.red;
Vertex (int i) {
tag=i;
}
public void setLocation(int x0,int y0) {
x=x0;
y=y0;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setColor(Color c) {
color=c;
}
public boolean colorIs(Color c) {
return (color.equals(c));
}
public void paintVertex(Graphics g) {
g.setColor(color);
g.fillOval(x,y,r,r);
g.setColor(Color.BLACK);
g.drawOval(x,y,r,r);
g.drawString(""+tag, x+r/2, y+r/2+4);
}
public int getR() {
return r;
}
}
Do not sleep in the Event Dispatch Thread; this will cause the GUI to freeze. For animation, use an EDT-friendly utility class, such as javax.swing.Timer.
Just a few ideas that might make your code cleaner:
In your SomePanel class, put the traversing code in a method out of the constructor. Constructors are intended for initializing fields.
First launch your static GUI, then spawn a worker thread to do the updates via the previous method (this would be your small "engine"). In this thread is were you can call sleep.
In your traverseVerticesRecoursive method, do only the repaint on the UI thread, and the status update on your worker thread.
Tha main modification you should do is not to block the GUI thread with sleep calls, as they have told you in the first answer.
Thread.sleep is a long running task. When you a running such a task in the EDT it blocks all repaint requests from being executed. All repaint requests which are pending and which were sent during the sleep phase are queued for future processing.
As a result when the EDT comes out of the sleep phase it coalesce all such repaint request (if coalescing is enabled which is the default property) into a single repaint which gets executed. If coalescing is not enabled then all queued request are executed serially without any time gap in between. As a result it seems that the UI did not update.
To correct the situation use a timer which triggers periodically after specific intervals of time.
Guy, you could use a new Thread differ with EDT thread to make an animation. For example,
void play() {
Thread thread = new Thread() {
#Override
public void run() {
game();
}
};
thread.start();
}
void game() {
for (; ; ) {
switch (state) {
case GameData.ANIMATING:
// call some function as repaint() to update GUI
break;
case GameData.GAME_ENDED:
return;
default:
break;
}
diffTime = System.currentTimeMillis() - beforeTime;
sleepTime = delay - diffTime;
sleepTime = (sleepTime < 0) ? 0 : sleepTime;
Thread.sleep(sleepTime);
}
}
First of all here are some code snippets:
public void startThread() {
this.animationThread = new Thread(this);
this.animationThread.start();
try {
this.animationThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public void run() {
pirateMainAnimation.animate();
}
public void animate() {
for (int j = 0; j < 9; j++) {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
break;
}
PirateAnimationPanel.getInstance().setCurrent(j);
PirateAnimationPanel.getInstance().repaint();
}
}
I'm trying to animate some images. The thing is that I want the main thread to wait for the animation thread to finish and then to continue. I searched around, read a little bit and decided to use the join() method. It perfectly waits for the thread to finish but I doesn't animate correctly. The repaint() method gets called 2 times instead of nine. I think maybe the problem is because I used singletons. Here is the singleton implementation.
import java.awt.Graphics;
import java.awt.MediaTracker;
import javax.swing.JPanel;
import uk.ac.aber.dcs.piratehangman.animation.PirateMainAnimation;
import uk.ac.aber.dcs.piratehangman.utilityclasses.AnimationThread;
#SuppressWarnings("serial")
public class PirateAnimationPanel extends JPanel {
private int current;
private MediaTracker mTracker;
private PirateMainAnimation pirateMainAnimation;
private AnimationThread animationThread;
private PirateAnimationPanel() {
this.current = 0;
this.pirateMainAnimation = new PirateMainAnimation();
mTracker = new MediaTracker(this);
this.animationThread = new AnimationThread();
setMediaTracker();
repaint();
}
private void setMediaTracker() {
for (int i = 0; i < 9; i++) {
mTracker.addImage(
this.pirateMainAnimation.getImagesForAnimation()[i],
this.pirateMainAnimation.getImagesForAnimationID()[i]);
try {
mTracker.waitForID(this.pirateMainAnimation
.getImagesForAnimationID()[i]);
} catch (InterruptedException e) {
System.out.println("Error loading image: " + i);
}
}
}
public void playAnimation() {
this.animationThread.startThread();
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
System.out.println("called");
g.drawImage(this.pirateMainAnimation.getImagesForAnimation()[current],
0, 0, this);
}
private static class PirateAnimationPanelHolder {
private static final PirateAnimationPanel pirateAnimationPanel =
new PirateAnimationPanel();
};
public static PirateAnimationPanel getInstance() {
return PirateAnimationPanelHolder.pirateAnimationPanel;
}
public void setCurrent(int current) {
this.current = current;
}
public int getCurrent() {
return current;
}
}
I think you mean that the paintComponent() methods only gets called twice. Also I think you should be able to remove the call to super.paintComponents() if you fill the component to the background color.
The repaint() method only marks the component as dirty and requests a re-render on the next paint.
I would have expected the Swing thread to be able to repaint within the 250ms but I'm not sure what other work is being done/rendered. You might want to put a call to MediaTracker.waitForAll() before the animation.
While the static singleton is not adding much I don't think it is causing a problem (in this case).
Update:
So the problem is that the join() is on the Swing event Thread which is blocking the repainting of the component. I suggested a call like the following to show the "new game dialog after the last animation:
SwingUtilities.invokeLater(new Runnable() {
public void run() { showDialog(); }
})
"Note that events being posted to the EventQueue can be coalesced," which may explain the disparity. Also, be certain to build your GUI on event dispatch thread. See A More Complex Image Icon Example for a more detailed discussion.
Addendum: Improving Perceived Performance When Loading Image Icons has a nice SwingWorker example that may simplify the off-loading.