Hi I am having some trouble drawing on a canvas. The code below is a class that makes a small rect of 87x90 pixels and puts it on a panel. The purpose is to display a "tile" that has 8 "lights" that count down. I make several instances of this class that all run at the same time.
When input is received it contains the x and y position and it talks to an instance of this class according to the x and y position.
I instance the class like this:
tile = new TileMaker[9][9];
and:
tile[i][j] = new TileMaker(Integer.toString(tp.topo[i][j]),i,j);
i and j represent x and y.
When running the application I get input from devices with coordinates x and y, which call the ledcountdown1 in the instance of tile[x][y].
My problem is when the animation is running on several instances at the same time. Lets say the instance tile[0][0] is half way counting down and the instance tile[1][1] is called to start its count down, the first instance often resets and follows the count down of the new instance.
Sometimes a previously finished countdown shows 8 lights when a new instance is asked to count down.
I tried to make use of double buffering to fix this bug and at first it seemed to work fine but now I'm getting those annoying effects as before, though it is not as frequent as before.
I hope I make any sense to you guys.. And I apologize for the length of the message..
Any suggestions/comments are greatly appreciated.
Thank you in advance.
public class TileMaker extends JPanel{
private static final long serialVersionUID = 1L;
public static JPanel panel;
public static JLabel label;
public TileCanvas Drawingarea;
public TileMaker(String name, int xpos, int ypos){
}
public JPanel TileCreator(String name){
panel = new JPanel(new BorderLayout());
panel.setName(name);
panel.setPreferredSize(new Dimension(87, 90));
panel.setVisible(true);
Drawingarea = new TileCanvas();
panel.add(Drawingarea,BorderLayout.CENTER);
return panel;
}
}
class TileCanvas extends Canvas {
private static final long serialVersionUID = 345057978247188915L;
public static ArrayList<TileLeds> ledlist;
static String imageFile = "cleartile.jpg";
Image image;
public Timer ledcountdowntimer;
private Timer ledcountdowntimer1;
public int counter = 0;
public boolean isrunning = false;
public boolean gameover = false;
public int myspeed;
private int bwidth;
private int bheight;
private Image bimage;
private Graphics bgraphics;
public TileCanvas() {
super();
ledlist = new ArrayList<TileLeds>();
ledlist.add(new TileLeds(29,18,10,10,Color.BLACK));
ledlist.add(new TileLeds(47,18,10,10,Color.BLACK));
ledlist.add(new TileLeds(60,30,10,10,Color.BLACK));
ledlist.add(new TileLeds(60,48,10,10,Color.BLACK));
ledlist.add(new TileLeds(47,60,10,10,Color.BLACK));
ledlist.add(new TileLeds(29,60,10,10,Color.BLACK));
ledlist.add(new TileLeds(16,48,10,10,Color.BLACK));
ledlist.add(new TileLeds(16,30,10,10,Color.BLACK));
MediaTracker media = new MediaTracker(this);
image = Toolkit.getDefaultToolkit().getImage(imageFile);
media.addImage(image, 0);
try {
media.waitForID(0);
}
catch (Exception e){
}
}
public void ledcountdown1(int speed, final Color color, String command){
if(command == "run"){
if(isrunning == false){
myspeed = speed;
counter = 9;
isrunning = true;
ledcountdowntimer = new Timer();
ledcountdowntimer.schedule(new TimerTask(){
public void run(){
counter--;
for(int g=0; g<8; g++){
ledlist.get(g).setcolor(Color.BLACK);
repaint();
}
if(counter==0){
for(int g=0; g<8; g++){
ledlist.get(g).setcolor(Color.BLACK);
repaint();
}
isrunning = false;
ledcountdowntimer.cancel();
}
else{
for(int n=0;n<counter;n++){
ledlist.get(n).setcolor(color);
repaint();
}
}
}
},0,speed*10);
}
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
resetBuffer();
if(bgraphics!=null){
bgraphics.clearRect(0,0,bwidth,bheight);
paintBuffer(bgraphics);
g.drawImage(bimage,0,0,this);
}
}
private void resetBuffer(){
bwidth=getSize().width;
bheight=getSize().height;
if(bgraphics!=null){
bgraphics.dispose();
bgraphics=null;
}
if(bimage!=null){
bimage.flush();
bimage=null;
}
System.gc();
bimage = createImage(bwidth, bheight);
bgraphics = bimage.getGraphics();
}
public void paintBuffer(Graphics g){
g.drawImage(image, 0, 0 , this);
for(int i=0; i<8; i++){
if(ledlist.get(i).getcolor()!=Color.BLACK){
g.setColor(ledlist.get(i).getcolor());
g.fillOval(ledlist.get(i).getx1(), ledlist.get(i).gety1(), ledlist.get(i).getx2(), ledlist.get(i).gety2());
}
}
}
}
modified on Wednesday, June 29, 2011 6:43 AM
|