replacing thread with timer
Code :
//###########################################################################################################
/*** stat method ***/
public void start()
{
Thread thread = new Thread(this); //set up thread
thread.start(); //start thread jump inside run method
}/*** end of start method ***/
//###########################################################################################################
/*** run method ***/
public void run()
{
while(true) //main game loop
{
//move image here
x += dx;
if(x+width >= getWidth())
{
x -= dx;
}
repaint();
try
{
Thread.sleep(17);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}//end of while loop
}/*** end of run method ***/
i want to keep same structure but replacing thread with timer.
in start method i can set and start timer.
Code :
public void start()
{
Timer timer = new Timer(17,this);
timer.start();
}/*** end of start method ***/
but not sure about run method bc run method it is thread method.
Re: replacing thread with timer
Quote:
run method it is thread method.
The run() method can be used by any code. It doesn't belong only to a thread.
Re: replacing thread with timer
for thread i was implements Runnable. but with timer do i use TimerTask ?
Re: replacing thread with timer
What does the API doc say is needed for the Timer class you are using?
Re: replacing thread with timer
Re: replacing thread with timer
Here's a link to the API doc:
Java Platform SE 7
Re: replacing thread with timer
i look under javax.management.timer but i didnt see it what iam looking for. am i missing some thing?
Re: replacing thread with timer
The most commonly used timers are in the java.util and the javax.swing packages.
Re: replacing thread with timer
just want to make sure. if you use timer than u replace run() method with
public void actionPerformed(ActionEvent e)? for ex
Code :
public void actionPerformed(ActionEvent e)
{
while(true) //main game loop
{
//move image here
x += dx;
if(x+width >= getWidth())
{
x -= dx;
}
repaint();
try
{
timer.delay(17);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
Re: replacing thread with timer
Please make a small simple program that compiles, executes and shows the problem.
I'm done for tonight. Back tomorrow.
Re: replacing thread with timer
here try compiling this. as you can see it will flicker. let me know if you know how to fix this problem.
Code :
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;
public class main extends JApplet implements ActionListener
{
//player information variable
int x = 10;
int y = 50;
int width = 30;
int height = 30;
int dx = 1;
//#####################
/*** init method ***/
public void init()
{
setSize(800, 400);
}/*** end of init method ***/
//#####################
/*** stat method ***/
public void start()
{
Timer timer = new Timer(30, this);
timer.start();
}/*** end of start method ***/
//##########################
/*** game loop ***/
public void actionPerformed(ActionEvent e)
{
//move image here
x += dx;
if(x+width >= getWidth())
{
x -= dx;
}
repaint();
}/*** end of run method ***/
//#################
/*** paint method ***/
public void paint(Graphics g)
{
super.paint(g); //redraw paint method. so it doesnt draw on top of each other
g.fillRect(x, y, width, height); //player
}/** end of paint method ***/
}
i am think probem might be with my eclipe.
i have eclipse indigo and runing
name=Eclipse Platform
id=org.eclipse.platform
version=3.7.0
Re: replacing thread with timer
Try getting double buffering for the drawing by using a Swing component and the paintComponent() method.
I get a black square moving to the right when I execute this.
Not any flicker.