Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  What am i doing wrong in here?  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« on: 2012-01-31 22:21:06 »

Can someone help me to get this running?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
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);
   }
}

Warning: Plz, excuse my bad english...
Online theagentd

JGO Wizard
****

Posts: 1392
Medals: 88



« Reply #1 on: 2012-02-01 01:54:11 »

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

There is no god.
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« Reply #2 on: 2012-02-01 06:02:47 »

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.

Warning: Plz, excuse my bad english...
Games published by our own members! Go get 'em!
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5871
Medals: 255


Hand over your head.


« Reply #3 on: 2012-02-01 06:06:39 »

1  
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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline Luckssmith

JGO n00b
*

Posts: 2
Medals: 1



« Reply #4 on: 2012-02-01 06:48:47 »

1  
2  
3  
4  
5  
 public void init(){
      ...
      Dim.getSize();
      ...
   }

Dim is uninitialized. Change it to something like this
1  
Dim = new Dimension(getSize());
or don't call getSize() since you aren't using Dim anywhere yet.
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« Reply #5 on: 2012-02-01 06:54:16 »

1  
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.

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...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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);
   }
}

Warning: Plz, excuse my bad english...
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5871
Medals: 255


Hand over your head.


« Reply #6 on: 2012-02-01 06:55:39 »

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

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« Reply #7 on: 2012-02-01 07:24:06 »

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

Don't get me wrong, I really appreciate your help, the issue is i didnt understand very well what you said...  Huh

Warning: Plz, excuse my bad english...
Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5871
Medals: 255


Hand over your head.


« Reply #8 on: 2012-02-01 07:25:41 »

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.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Strike Force
***

Posts: 901
Medals: 55



« Reply #9 on: 2012-02-01 11:52:17 »

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.
Games published by our own members! Go get 'em!
Offline gerard_pp

JGO n00b
*

Posts: 31
Medals: 1



« Reply #10 on: 2012-02-02 11:47:41 »

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).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
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);
   }
}

Warning: Plz, excuse my bad english...
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.136 seconds with 20 queries.