java - Better way to make a thread sleep -
i've been doing tutorial on 2d graphics in java 8, when netbeans gave me hint doing thread.sleep
affect performance. however, though i've been able find several better ways, haven't been able find way include them without messing code.
package platformer; import java.awt.*; import javax.swing.*; import java.util.scanner; @suppresswarnings("serial") public class platformer extends jpanel { int x = 0;//sets starting coords. of ball int y = 0; private void moveball() {//how ball moves x = x+1; y = y+1; } @override public void paint(graphics g) {//essentially al graphics functions super.paint(g); graphics2d g2d = (graphics2d) g; g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2d.filloval(x, y, 30, 30); } public static void main(string[] args) throws interruptedexception { scanner reader = new scanner(system.in); scanner reader1 = new scanner(system.in); system.out.print("enter x-value window (whole numbers only): "); int setx = reader.nextint(); system.out.print("enter y-value window (whole numbers only): "); int sety = reader.nextint(); jframe gameframe = new jframe("sample frame");//makes window variable w/ name in quotations. platformer game = new platformer();//'copies' graphics functions above variable gameframe.add(game);//adds above variable th window gameframe.setsize(setx,sety);//sets resolution/size of window(x,y) gameframe.setvisible(true);//makse window visible gameframe.setdefaultcloseoperation(windowconstants.exit_on_close);//makes window close when close button hit while (true){ game.moveball(); game.repaint(); thread.sleep(10); } } }
i'm wondering how can include better way make thread sleep in loop, or otherwise make netbeans loop.
instead of using thread.sleep(10)
in while(true)
, can use scheduledexecutorservice
invoke moveball()
, paint()
every 10 seconds shown below gives cleaner way shown below:
public class platformer extends jpanel implements runnable { int x = 0;//sets starting coords. of ball int y = 0; public void add() { jframe gameframe = new jframe("sample frame");//makes window variable w/ name in quotations. platformer game = new platformer();//'copies' graphics functions above variable gameframe.add(game);//adds above variable th window gameframe.setsize(setx,sety);//sets resolution/size of window(x,y) gameframe.setvisible(true);//makse window visible gameframe.setdefaultcloseoperation(windowconstants.exit_on_close);//makes window close when close button hit } private void moveball() {//how ball moves x = x+1; y = y+1; } @override public void paint(graphics g) {//essentially al graphics functions super.paint(g); graphics2d g2d = (graphics2d) g; g2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2d.filloval(x, y, 30, 30); } @override public void run() { //this code executed every 10 seconds moveball(); paint(); } public static void main(string[] args) throws interruptedexception { scanner reader = new scanner(system.in); scanner reader1 = new scanner(system.in); system.out.print("enter x-value window (whole numbers only): "); int setx = reader.nextint(); system.out.print("enter y-value window (whole numbers only): "); int sety = reader.nextint(); jframe gameframe = new jframe("sample frame");//makes window variable w/ name in quotations. platformer game = new platformer();//'copies' graphics functions above variable gameframe.add(game);//adds above variable th window gameframe.setsize(setx,sety);//sets resolution/size of window(x,y) gameframe.setvisible(true);//makse window visible gameframe.setdefaultcloseoperation(windowconstants.exit_on_close);//makes window close when close button hit //schedule run every 10 seconds //this calls run() method above scheduledexecutorservice scheduler = executors.newscheduledthreadpool(1); scheduler.scheduleatfixedrate(game, 0, 10, timeunit.seconds); } }
Comments
Post a Comment