What am i doing wrong in here?

Can someone help me to get this running?

import java.applet.*;
import java.awt.*;

public class Prueba extends Applet implements Runnable{
	Thread t;
	int i;
	Graphics backBufferG;
	Image backBufferI;
	Dimension Dim;
			
	public void init(){
		t = new Thread(this);
		t.start();
		i = 0;
		Dim.getSize();
		backBufferI = createImage(600, 600);
		backBufferG = backBufferI.getGraphics();
		
	}
	public void run(){
		while(true){
			i++;
			repaint();
			try{
				t.sleep(1000/30);
			}catch(Exception ex){;}	
		}
	}
	public void paint(Graphics g){
		backBufferG.drawString("i = "+i, 10, 20);
		g.drawImage(backBufferI, 0, 0, this);
	}
}

Try to move the Thread creation and starting to public void start().

Hi D

i still cant figure out why this is not working. i did what you told, but its like the t.start(); method is not doing anything.

backBufferG.drawString("i = "+i, 10, 20);

This is rather odd. You overwrite your image with text. You probably will end up with something that looks like ‘i=8’, ‘i=88’, ‘i=888’ (as time goes by)

Also, make ‘int i’ volatile, as you’re reading it from a thread that is not writing to it.

 public void init(){
      ...
      Dim.getSize();
      ...
   }

Dim is uninitialized. Change it to something like this

Dim = new Dimension(getSize());

or don’t call getSize() since you aren’t using Dim anywhere yet.

Hello Riven

it just works as i intend. i use it to make some kind of counter… I dont know if you can recomend another way…

Any ways, i applied some changes to the code and finally get it to work. But now, all the numbers on my counter keeps splattered on the screen :emo:
no matter the “repaint()” refresh the whole thing…

import java.applet.*;
import java.awt.*;

public class Prueba extends Applet implements Runnable{
   Thread t;
   int i;
   Graphics backBufferG;
   Image backBufferI;
            
   public void init(){
	  t = new Thread(this);
      t.start();
      i = 0;
      backBufferI = createImage(600, 600);
      backBufferG = backBufferI.getGraphics();
      
   }
   public void run(){
      while(true){
         i++;
         repaint();
         try{
            t.sleep(1000/29);
         }catch(Exception ex){;}   
      }
   }
   public void paint(Graphics g){
	  backBufferG.drawString("i = "+i, 10, 20);
      g.drawImage(backBufferI, 0, 0, this);
   }
   public void update(Graphics g){
	   paint(g);
   }
}

Reread my post very carefully. It explains why you see what you see :expressionless:

Don’t get me wrong, I really appreciate your help, the issue is i didnt understand very well what you said… ???

repaint() clears your component, not your own image.

You are rendering text in your own image, time and time again, never clearning it, so any text will be drawn on top of the existing text.

Your thread pattern is unusual, and I really don’t think it’s a good idea to turn an Applet into a Runnable that becomes the implementation of a new thread. Really, stay sane and separate that Runnable a different class, or drop the threading altogether and get it working without threads first.

Im back, just to post the code with some minor changes, and this time, working as i wanted.

I still have in consideration to put my applet extension class apart from the runnable ones. (thanks for the tips).


import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class Prueba extends Applet implements Runnable{
	//Declaracion de variables
	int i;
	BufferedImage backBuffer;
	Graphics2D painter;
	Thread t;
	int movilidadX;
	int movilidadY;
	
	//Metodos de clase
	public void init(){
		t = new Thread(this);
		backBuffer = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB);
		painter = backBuffer.createGraphics();
		i = 0;
		movilidadX = 0;
		movilidadY = 0;
		t.start();
	}
	public void run(){
		while(true){
			i++;
			movilidadX++;
			movilidadY++;
			repaint();
			try{
				t.sleep(1000/30);
			}catch(InterruptedException ex){
				ex.printStackTrace();
			}
		}
	}
	public void paint(Graphics g){
		painter.setColor(Color.GRAY);
		painter.fillRect(0, 0, 600, 400);
		painter.setColor(Color.BLACK);
		painter.fillRect(movilidadX, movilidadY, 10, 10);
		painter.drawString("El valor de i es: " + i, 20, 20);
		g.drawImage(backBuffer, 0, 0, this);
		paint(g);
	}
}