ok now what? in the ball class i created variables that define the x, y, width and height for the ball and created a new ball in the public void paint(Graphics g) method and then drew the ball to the variables, but no ball is being drawn
Show differencesHistory of post edits
#Actualburnt_casadilla
Posted 11 August 2012 - 10:10 AM
[source lang="java"] class AnimationThread implements Runnable { JApplet c; public AnimationThread(game game) { this.c = c; } public void run() { c.repaint(); } } class Ball extends Ellipse2D.Float { public Ball() { int x = 150; int y = 150; int width = 20; int height = 20; } } public void init() { this.setSize(WIDTH, HEIGHT); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3); executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS); } public void start() { Thread th = new Thread(this); th.start();//start main game } public void stop() { } public void destroy() { } public boolean keyDown (Event e, int key) { if(key == Event.LEFT) { xspeed = -5; yspeed = 0; } if(key == Event.RIGHT) { xspeed = 5; yspeed = 0; } if(key == Event.UP) { yspeed = -5; xspeed = 0; } if(key == Event.DOWN) { yspeed = 5; xspeed = 0; } return true; } public void run() { while(true) { repaint(); if (xpos < 1) { xpos = 399; } if (xpos > 399) { xpos = 1; } if (ypos < 1) { ypos = 399; } if (ypos > 399) { ypos = 1; } ypos += yspeed; xpos += xspeed; try { Thread.sleep(20); } catch(InterruptedException ex) { } } } public void paint(Graphics g) { Ball ball = new Ball(); g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); g.drawOval((int)ball.x, (int)ball.y, (int)ball.width, (int)ball.height); }}[/source]
ok now what? in the ball class i created variables that define the x, y, width and height for the ball and created a new ball in the public void paint(Graphics g) method and then drew the ball to the variables, but no ball is being drawn
ok now what? in the ball class i created variables that define the x, y, width and height for the ball and created a new ball in the public void paint(Graphics g) method and then drew the ball to the variables, but no ball is being drawn
#1burnt_casadilla
Posted 11 August 2012 - 10:09 AM
[source lang="java"] class AnimationThread implements Runnable { JApplet c; public AnimationThread(game game) { this.c = c; } public void run() { c.repaint(); } } class Ball extends Ellipse2D.Float { public Ball() { int x = 150; int y = 150; int width = 20; int height = 20; } } public void init() { this.setSize(WIDTH, HEIGHT); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3); executor.scheduleAtFixedRate(new AnimationThread(this), 0L, 20L, TimeUnit.MILLISECONDS); } public void start() { Thread th = new Thread(this); th.start();//start main game } public void stop() { } public void destroy() { } public boolean keyDown (Event e, int key) { if(key == Event.LEFT) { xspeed = -5; yspeed = 0; } if(key == Event.RIGHT) { xspeed = 5; yspeed = 0; } if(key == Event.UP) { yspeed = -5; xspeed = 0; } if(key == Event.DOWN) { yspeed = 5; xspeed = 0; } return true; } public void run() { while(true) { repaint(); if (xpos < 1) { xpos = 399; } if (xpos > 399) { xpos = 1; } if (ypos < 1) { ypos = 399; } if (ypos > 399) { ypos = 1; } ypos += yspeed; xpos += xspeed; try { Thread.sleep(20); } catch(InterruptedException ex) { } } } public void paint(Graphics g) { Ball ball = new Ball(); g.setColor(Color.black); g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius); g.drawOval((int)ball.x, (int)ball.y, (int)ball.width, (int)ball.height); }}[/source]