Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Refreshing gaphics in java

I want to be able to refresh the graphics in java but when i try to refresh it it leaves the old image behind and paints the new one as well so it shows both of hem when i want only one.
The code is:
//refaris() is used to refresh.

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import sun.audio.*;
import java.util.*;
import java.net.*;

import java.applet.*;
public class test2 extends JFrame implements KeyListener
{
int right = 225;
int up = 225;
ImageIcon snake;
Container f2;
String test;
ImageIcon bull;
ImageIcon f;
int bullup;
int bullright;
int q = 1;
int fright = 50;
int fup = 50;
int score = 0;


public test2() {
super("FSnake");
f2 = getContentPane();
f2.setLayout(new FlowLayout());

f = new ImageIcon("f.JPG");


snake = new ImageIcon("tn_ea03.jpg");
bull = new ImageIcon("buller.JPG");
setVisible(true);
setSize(500,500);
addKeyListener(this);
}


public void keyReleased(KeyEvent event){
System.out.println( "Key pressed: %s" +
event.getKeyText( event.getKeyCode() ) );


}

public void keyTyped(KeyEvent event){
System.out.println( "Key pressed: %s" +
event.getKeyText( event.getKeyCode() ) );
}



public void repaint() {
paint(getGraphics()) ;
}

public void keyPressed(KeyEvent event) {
System.out.println( "" +
event.getKeyText( event.getKeyCode() ) ); // output pressed key
String f;
System.out.println( event.getKeyCode() ); // output pressed key;
//handels if key pressed is up
if (event.getKeyCode() == 38) {


up();

}
//handels if key pressed is down
if (event.getKeyCode() == 40) {


down();

}

//handels if space pressed
if (event.getKeyCode() == 32) {




}

//handels if right is pressed
if (event.getKeyCode() == 39) {
right();



}

//handels if left is pressed
if (event.getKeyCode() == 37) {
left();



}



}

public void up() {
up = up - 50;
eat();

refaris();


}

public void down() {
up = up + 50;
eat();
refaris();
}
public void right (){ //39
right = right + 50;
eat();
refaris();
}

public void left(){ //39
right = right - 50;
eat();
refaris();
}

public void eat() {
if ( right == fright && up == fup) {
score = score + 1;

fright = (int)(Math.random()*500);
fup = (int)(Math.random()*500);
refaris();
System.out.println("dsdsdsvdsvdsvsd" + score);

}
}


public void paint (Graphics g) {


g.drawImage( snake.getImage(), right, up, null );
g.drawImage( bull.getImage(), bullright, bullup, null );
g.drawImage( f.getImage(), fright,fup,null);

}

public void refaris() { //REFRESH
repaint();


}

public void update(Graphics g) {
paint(g) ;
}


public static void main(String[] args){

test2 application = new test2();

}
}


if i put setVisible(false); then setVisible(true); it would work but ofcourse thats just bad coding and it would make it blink everytime i wanted to refresh.
[4179 byte] By [dojo] at [2007-11-11 9:52:29]
# 1 Re: Refreshing gaphics in java
i fixed it b calling thread.sleep before i repaint
dojo at 2007-11-11 22:32:17 >