Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What am i doing wrong in here?  (Read 1182 times)
0 Members and 1 Guest are viewing this topic.
Offline gerard_pp

Junior Member


Medals: 1



« Posted 2012-02-01 04: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...
Offline theagentd
« Reply #1 - Posted 2012-02-01 07:54:11 »

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

Myomyomyo.
Offline gerard_pp

Junior Member


Medals: 1



« Reply #2 - Posted 2012-02-01 12: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-02-01 12: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

Junior Newbie


Medals: 1



« Reply #4 - Posted 2012-02-01 12: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

Junior Member


Medals: 1



« Reply #5 - Posted 2012-02-01 12: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 Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2012-02-01 12: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

Junior Member


Medals: 1



« Reply #7 - Posted 2012-02-01 13: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 Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-02-01 13: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 Kernel


Medals: 202



« Reply #9 - Posted 2012-02-01 17: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! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gerard_pp

Junior Member


Medals: 1



« Reply #10 - Posted 2012-02-02 17: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]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!