Click here to Skip to main content
15,899,026 members
Home / Discussions / Java
   

Java

 
GeneralRe: Eclipse (Java?) makes try/catch mandatory? Pin
Helfdane28-Jun-11 1:58
Helfdane28-Jun-11 1:58 
GeneralRe: Eclipse (Java?) makes try/catch mandatory? Pin
TorstenH.28-Jun-11 2:04
TorstenH.28-Jun-11 2:04 
AnswerRe: Eclipse (Java?) makes try/catch mandatory? Pin
Gerben Jongerius28-Jun-11 6:34
Gerben Jongerius28-Jun-11 6:34 
GeneralRe: Eclipse (Java?) makes try/catch mandatory? Pin
Helfdane28-Jun-11 20:47
Helfdane28-Jun-11 20:47 
GeneralRe: Eclipse (Java?) makes try/catch mandatory? Pin
TorstenH.29-Jun-11 2:32
TorstenH.29-Jun-11 2:32 
GeneralRe: Eclipse (Java?) makes try/catch mandatory? Pin
Helfdane29-Jun-11 3:25
Helfdane29-Jun-11 3:25 
AnswerRe: Eclipse (Java?) makes try/catch mandatory? Pin
Nagy Vilmos29-Jun-11 3:20
professionalNagy Vilmos29-Jun-11 3:20 
QuestionJava canvas problems [modified] (Solved) Pin
Davíð Þór27-Jun-11 3:13
Davíð Þór27-Jun-11 3:13 
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 drawing
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;

     // Make variable for double buffering
     private int bwidth;
     private int bheight;
     private Image bimage;
     private Graphics bgraphics;
	
     public TileCanvas() {
          super();

          // Create LEDs and add them to the ledlist.
          //Set the color black so no led is drawn on the screen.
          ledlist = new ArrayList<TileLeds>();
          ledlist.add(new TileLeds(29,18,10,10,Color.BLACK)); // LED 0
	  ledlist.add(new TileLeds(47,18,10,10,Color.BLACK)); // LED 1
          ledlist.add(new TileLeds(60,30,10,10,Color.BLACK)); // LED 2
          ledlist.add(new TileLeds(60,48,10,10,Color.BLACK)); // LED 3
          ledlist.add(new TileLeds(47,60,10,10,Color.BLACK)); // LED 4	
          ledlist.add(new TileLeds(29,60,10,10,Color.BLACK)); // LED 5
          ledlist.add(new TileLeds(16,48,10,10,Color.BLACK)); // LED 6
          ledlist.add(new TileLeds(16,30,10,10,Color.BLACK)); // LED 7		
		
          // Media tracker used to load an image file
          MediaTracker media = new MediaTracker(this);
          image = Toolkit.getDefaultToolkit().getImage(imageFile);
          media.addImage(image, 0);
          try {
               media.waitForID(0);
          }
          catch (Exception e){
          }
     }

     // LED count down. Commands: run, turnOff, changeColor
     public void ledcountdown1(int speed, final Color color, String command){
     // ************** Count down **************
          if(command == "run"){
               if(isrunning == false){
                    myspeed = speed;
                    //counter = 8;
                    counter = 9;
                    isrunning = true;
                    ledcountdowntimer = new Timer();
                    ledcountdowntimer.schedule(new TimerTask(){
                    public void run(){
                         counter--;
                         // Making sure all LEDs are off
                         for(int g=0; g<8; g++){
                              ledlist.get(g).setcolor(Color.BLACK);
                              repaint();	
                         }
                         // Cancel the timer task and turn all the LEDs off
                         if(counter==0){	
                              //System.out.println("CANCELING THE TIMER");
                              for(int g=0; g<8; g++){
                                   ledlist.get(g).setcolor(Color.BLACK);
                                   repaint();
                              }
                              isrunning = false;
                              ledcountdowntimer.cancel();
                         }
                         // Turn LEDs off one by one from 8
                         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){
          //System.out.println("paint");

          // Check the buffersize, it should be the size of the current panel (size of the tile). 
          // Also check if the bufferImage and bufferGraphics. Reset the buffer if anything returns true.                        if(bwidth!=getSize().width||bheight!=getSize().height||bimage==null||bgraphics==null)
          resetBuffer();

          // If graphics exists
          if(bgraphics!=null){
          //  Clear the offscreen image
               bgraphics.clearRect(0,0,bwidth,bheight);
               // Call the paintbuffer with the off screen graphics
               paintBuffer(bgraphics);
               // Paint the off screen image on the screen
               g.drawImage(bimage,0,0,this);
          }
     }
		      
     private void resetBuffer(){

          // Check the image size
          bwidth=getSize().width;
          bheight=getSize().height;
		
          // Dispose of the old graphics and image
          if(bgraphics!=null){
               bgraphics.dispose();
               bgraphics=null;
          }
          if(bimage!=null){
               bimage.flush();
               bimage=null;
          }
		
          // Run the garbage collector
          System.gc();
		
          // Create a buffer image with the size of the panel
          bimage = createImage(bwidth, bheight);
          bgraphics = bimage.getGraphics();
     }
     
     public void paintBuffer(Graphics g){
          //System.out.println("paintBuffer");
		
          // Drawing the tile
          g.drawImage(image, 0, 0 , this);
	    
          // Loop through the LEDs
          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

AnswerRe: Java canvas problems Pin
TorstenH.28-Jun-11 2:02
TorstenH.28-Jun-11 2:02 
GeneralRe: Java canvas problems [modified] Pin
Davíð Þór28-Jun-11 5:20
Davíð Þór28-Jun-11 5:20 
GeneralRe: Java canvas problems Pin
TorstenH.28-Jun-11 20:43
TorstenH.28-Jun-11 20:43 
GeneralRe: Java canvas problems Pin
Davíð Þór29-Jun-11 0:39
Davíð Þór29-Jun-11 0:39 
GeneralRe: Java canvas problems Pin
TorstenH.29-Jun-11 2:31
TorstenH.29-Jun-11 2:31 
AnswerRe: Java canvas problems Pin
David Skelly28-Jun-11 6:17
David Skelly28-Jun-11 6:17 
GeneralRe: Java canvas problems Pin
Davíð Þór28-Jun-11 7:05
Davíð Þór28-Jun-11 7:05 
GeneralRe: Java canvas problems Pin
Davíð Þór29-Jun-11 0:36
Davíð Þór29-Jun-11 0:36 
Questionproject Pin
Member 803844226-Jun-11 23:52
Member 803844226-Jun-11 23:52 
AnswerRe: project Pin
Richard MacCutchan27-Jun-11 0:12
mveRichard MacCutchan27-Jun-11 0:12 
GeneralRe: project Pin
Member 803844227-Jun-11 3:13
Member 803844227-Jun-11 3:13 
GeneralRe: project Pin
Nagy Vilmos27-Jun-11 4:07
professionalNagy Vilmos27-Jun-11 4:07 
GeneralRe: project Pin
Pete O'Hanlon27-Jun-11 4:31
mvePete O'Hanlon27-Jun-11 4:31 
GeneralRe: project Pin
Member 803844227-Jun-11 4:46
Member 803844227-Jun-11 4:46 
GeneralRe: project Pin
Pete O'Hanlon27-Jun-11 5:59
mvePete O'Hanlon27-Jun-11 5:59 
GeneralRe: project Pin
Member 803844227-Jun-11 6:39
Member 803844227-Jun-11 6:39 
GeneralRe: project PinPopular
Pete O'Hanlon27-Jun-11 6:53
mvePete O'Hanlon27-Jun-11 6:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.