Pardon v componentoch nic ako flush neexistuje, ten sa nahcadza v image ... to asi keby si pouzival doublebuffering (to ta teraz nemusi zauimat). Ale k veci, v tom painte volas v tvojom kode super.paint(Graphics g) teda tu pretazenu metodu? Lebo ta najskor vymaze cely obsah vykresli okno (komponentu, teda JPanel) a az za tym kreslis co chces ty. Presne ako to mam v kode co som ti poslal, konkretne class JCanvas. Za tym super.paint(g) ide hoc co co ches nakreslit. Tak este raz:
import java.awt.Graphics;
import javax.swing.JPanel;
public class JCanvas extends JPanel {
/**
*
*/
private static final long serialVersionUID = 3060757030831662850L;
private Drawable drawable;
// rovnako mozes Overridnut paintComponent(Graphics g)
@Override
public void paint(final Graphics g) {
super.paint(g); // treba volat aj toto super.paint(g)
// tu napis co chces kreslit ja som
// tu dal ze sa ma nakreslit object drawable
this.drawable.draw(g);
}
// toto je mnou definovana metoda dolezite je len to co sadeje v paint(g)
public void setDrawable(final Drawable d) {
this.drawable = d;
}
}
A tu je to s pohybujucim sa obrazkom:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Square implements Drawable {
public int width;
public int x, y;
private final BufferedImage img;
public Square(final int initX, final int initY, final int width) {
this.x = initX;
this.y = initY;
this.width = width;
// tuto cast si nevsimaj len sa snazim nacpat vacsi obrazok do mensieho
this.img = new BufferedImage(width, width, BufferedImage.TYPE_INT_ARGB);
try {
final BufferedImage im = ImageIO.read(new File(
"/cesta/k/obrazku/icons/page-add-icon.png"));
final Graphics g = this.img.getGraphics();
g.drawImage(im, 0, 0, this.img.getWidth(), this.img.getHeight(), 0,
0, im.getWidth(), im.getHeight(), null);
g.dispose();
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void draw(final Graphics g) {
// TODO Auto-generated method stub
g.drawImage(this.img, this.x, this.y, null);
}
public void moveBottom(final int step) {
this.y += step;
}
public void moveLeft(final int step) {
this.x += step;
}
public void moveRight(final int step) {
this.x -= step;
}
public void moveTop(final int step) {
this.y -= step;
}
}