Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
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  
  Where are my pixels!!???  (Read 1964 times)
0 Members and 1 Guest are viewing this topic.
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Posted 2011-03-25 14:55:55 »

Hi, I have the following code to display a game in a window (Frame) or applet mode:

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Game extends Applet {

   private boolean isApplet;

   public Game() {
      this.isApplet = true;
   }

   public void init() {
   }

   public void paint(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0, 0, 640, 480);

      g.setColor(Color.red);
      g.drawRect(0, 0, 640, 480);

      // g.setColor(Color.green);
     // g.drawRect(0, 0, 640 - 3, 480 - 33);
  }

   public void update(Graphics g) {
   }

   public void buildFrame() {
      Frame f = new Frame("Game");
      // Add an window adapter to allow user close it.
     f.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
      f.setSize(640, 480);
      f.setResizable(false);
      // Get current screen resolution to adjust the frame in the middle
     // of the user desktop.
     Dimension dimension = getToolkit().getScreenSize();
      Rectangle rectangle = f.getBounds();
      // Put the frame centered in the user desktop.
     f.setLocation((dimension.width - rectangle.width) / 2,
            (dimension.height - rectangle.height) / 2);
      // Add the applet to frame.
     f.add(this);
      // Show it!
     f.setVisible(true);
      this.isApplet = false;
      // Call applet init method.
     this.init();
   }

   public static void main(String[] args) {
      Game g = new Game();
      g.buildFrame();
   }
}


When running as application it starts in the main method that calls the buildFrame method to put the applet inside of it.

I have created a 640x480 pixels window and to make sure the size is correct I have drawn a rectangle with (0,0,640,480) but for my surprise it does not appear in the right and bottom edges.

To make the full rectangle appear in the screen I had to put the following code:

1  
g.drawRect(0, 0, 640 - 3, 480 - 33);


Why this happens? The same problem happens when running in the browser (applet mode).

I also noticed that the result is different when running in Windows (I'm using Linux). I had to put (-5) in the right corner to make it appear.

The question is: is there a way to make the code above display a real 640x480 display for both applet/application and plataform independent?

Thanks!
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #1 - Posted 2011-03-25 15:07:11 »

The problem is that you make the actual frame (including the borders and the title bar) to be 640x480. This makes the content pane (the area where you can draw stuff) a lot smaller.

Instead do this:
1  
2  
3  
4  
5  
applet.setPreferredSize(new Dimension(640, 480));
f.add(applet);
f.pack();

f.setVisible(true);


If you want to center the frame, all you have to do is this:
1  
f.setLocationRelativeTo(null);


Hope that helps  Wink
Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #2 - Posted 2011-03-25 17:53:45 »

Thanks Captain, but even using the code you provided I still have the same problem.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2011-03-25 18:06:09 »

There shouldn't be a problem in Applet mode, since you've got the entire viewing area to paint.

However, in application mode, the top bar (with the close, maximize, and minimize buttons) and the thin border around the window are part of the viewing area. When you add the applet to the Frame, it is not using the full 640x480 because of the border.
What exactly happens when you use Captain Awesome's code?
1  
2  
3  
this.setPreferredSize(new Dimension(640,480));
f.add(this);
f.pack();

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #4 - Posted 2011-03-25 18:16:21 »

the problem is that my red rectangle right and bottom lines does not appear, as my original code in the first post.

The best I could do was:

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Game extends Applet {

   private boolean isApplet;

   private Image image;

   public Game() {
      this.isApplet = true;
      this.setPreferredSize(new Dimension(640, 480));
   }

   public void init() {
      this.setPreferredSize(new Dimension(640, 480));
      this.setSize(new Dimension(640, 480));
   }

   public void paint(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0, 0, 640, 480);

      g.setColor(Color.red);
      g.drawRect(0, 0, 640, 480);

      g.drawImage(image, 0, 0, null);
      g.setColor(Color.green);
      g.drawLine(0, 0, 640, 480);
   }

   public void update(Graphics g) {
   }

   public void buildFrame() {
      Frame f = new Frame("Game");
      f.add(this);
      f.pack();
      f.setVisible(true);

      f.setLocationRelativeTo(null);

      System.out.println(f.getInsets().bottom);
      System.out.println(f.getInsets().top);
      System.out.println(f.getInsets().left);
      System.out.println(f.getInsets().right);
      System.out.println(f.getWidth());

      // 640+11
     // 480+31
     // f.setSize(640 + 11, 480 + 31);
     f.setSize(640 + f.getInsets().left + f.getInsets().right + 1, 480
            + f.getInsets().top + f.getInsets().bottom + 1);

      try {
         image = ImageIO.read(new File("image.png"));
      } catch (IOException e1) {
         // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      this.isApplet = false;
      // Call applet init method.
     this.init();
   }

   public static void main(String[] args) {
      Game g = new Game();
      g.buildFrame();
   }
}


Using those insets() worked a little bit better but I still had to add a magic number (+1) there. Now running application mode in both Linux and Windows get almost the same result.

Quote
There shouldn't be a problem in Applet mode, since you've got the entire viewing area to paint

Have you tried my code and run as applet? Even running as applet in both Windows and Linux I'm not able to see right and bottom lines of the red rectangle.
1  
<applet code=Game width="640" height="480"></applet>


This is really weird, in fact, I haven't stopped to check this before... but it'd be nice to have one solution for both applet/application modes... any other tips?

thanks

Offline Abuse

JGO Knight


Medals: 12


falling into the abyss of reality


« Reply #5 - Posted 2011-03-25 18:32:37 »

You need to read the specification for drawRect(...); the extent of the touched pixels is not the same as fillRect(...).

fillRect(0,0,10,2); will paint a 10x2 rectangle of pixels.
drawRect(0,0,9,1); will paint the same 10x2 rectangle of pixels.

i.e. drawRect touches the pixels at width and height, fillRect touches the pixels upto width and height.

Ofcourse, if you ever work on embedded Java you'll find all manner of broken implementations that fail to follow this simple specification.
Samsung, SonyEricsson and Nokia have in the past all managed to produce phones with J2ME implementations that failed in this regard; Sun certified my ass.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #6 - Posted 2011-03-25 19:47:36 »

the problem is that my red rectangle right and bottom lines does not appear, as my original code in the first post.

The best I could do was:

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Game extends Applet {

   private boolean isApplet;

   private Image image;

   public Game() {
      this.isApplet = true;
      this.setPreferredSize(new Dimension(640, 480));
   }

   public void init() {
      this.setPreferredSize(new Dimension(640, 480));
      this.setSize(new Dimension(640, 480));
   }

   public void paint(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0, 0, 640, 480);

      g.setColor(Color.red);
      g.drawRect(0, 0, 640, 480);

      g.drawImage(image, 0, 0, null);
      g.setColor(Color.green);
      g.drawLine(0, 0, 640, 480);
   }

   public void update(Graphics g) {
   }

   public void buildFrame() {
      Frame f = new Frame("Game");
      f.add(this);
      f.pack();
      f.setVisible(true);

      f.setLocationRelativeTo(null);

      System.out.println(f.getInsets().bottom);
      System.out.println(f.getInsets().top);
      System.out.println(f.getInsets().left);
      System.out.println(f.getInsets().right);
      System.out.println(f.getWidth());

      // 640+11
     // 480+31
     // f.setSize(640 + 11, 480 + 31);
     f.setSize(640 + f.getInsets().left + f.getInsets().right + 1, 480
            + f.getInsets().top + f.getInsets().bottom + 1);

      try {
         image = ImageIO.read(new File("image.png"));
      } catch (IOException e1) {
         // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      this.isApplet = false;
      // Call applet init method.
     this.init();
   }

   public static void main(String[] args) {
      Game g = new Game();
      g.buildFrame();
   }
}


Using those insets() worked a little bit better but I still had to add a magic number (+1) there. Now running application mode in both Linux and Windows get almost the same result.

Quote
There shouldn't be a problem in Applet mode, since you've got the entire viewing area to paint

Have you tried my code and run as applet? Even running as applet in both Windows and Linux I'm not able to see right and bottom lines of the red rectangle.
1  
<applet code=Game width="640" height="480"></applet>


This is really weird, in fact, I haven't stopped to check this before... but it'd be nice to have one solution for both applet/application modes... any other tips?

thanks



When you tried my code, did you remove the f.setSize... part? Since f.pack() automatically calculates (and sets) the size of the frame needed to properly display all components. Also try what Abuse just said.
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2011-03-25 22:40:40 »

Yeah I just did some playing around and Abuse is right. It seems as if you go 1 extra pixel when you do drawRect. Weird....

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #8 - Posted 2011-03-26 01:05:29 »

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  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;

public class Game extends Applet {

   private boolean isApplet;

   private Image image;

   public Game() {
      this.isApplet = true;
      this.setPreferredSize(new Dimension(640, 480));
      this.setMaximumSize(new Dimension(640, 480));
      this.setSize(new Dimension(640, 480));
   }

   public void init() {
      this.setPreferredSize(new Dimension(640, 480));
      this.setMaximumSize(new Dimension(640, 480));
      this.setSize(new Dimension(640, 480));
   }

   public void paint(Graphics g) {
      g.setColor(Color.black);
      g.fillRect(0, 0, 640, 480);

      g.setColor(Color.red);
      g.drawRect(0, 0, 640, 480);

      g.drawImage(image, 0, 0, null);
      g.setColor(Color.green);
      g.drawLine(0, 0, 640, 480);
   }

   public void update(Graphics g) {
   }

   public void buildFrame() {
      Frame f = new Frame("Game");
      f.add(this);
      f.pack();
      f.setVisible(true);
      f.setResizable(false);

      f.setLocationRelativeTo(null);

      System.out.println(f.getInsets().bottom);
      System.out.println(f.getInsets().top);
      System.out.println(f.getInsets().left);
      System.out.println(f.getInsets().right);
      System.out.println(f.getWidth());
      System.out.println(f.getHeight());

      // 640+11
     // 480+31
     // f.setSize(640 + 11, 480 + 31);
     f.setSize(640 + f.getInsets().left + f.getInsets().right + 1, 480
            + f.getInsets().top + f.getInsets().bottom + 1);

      this.isApplet = false;
      // Call applet init method.
     this.init();
   }

   public static void main(String[] args) {
      Game g = new Game();
      g.buildFrame();
   }
}


Under linux it's really really weird. See the output of the code above:

How it's possible to run exactly the same code multiple times and get different values for the inset() values???!? Huh

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  
asses$ java Game 
5
27
1
1
650
510
^Cluisoft@luisoft-Inspiron-N5010:/luisoft/development/games/BoulderDash4k/build/asses$ java Game
5
27
1
1
642
512
^Cluisoft@luisoft-Inspiron-N5010:/luisoft/development/games/BoulderDash4k/build/asses$ java Game
5
27
1
1
642
512
^Cluisoft@luisoft-Inspiron-N5010:/luisoft/development/games/BoulderDash4k/build/asses$ java Game
5
27
1
1
640
480


Under Windows I ran multiple times the code above and the result was always consistent:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
Z:\development\games\BoulderDash4k\build\classes>java Game
3
29
3
3
648
514

Z:\development\games\BoulderDash4k\build\classes>java Game
3
29
3
3
648
514

Z:\development\games\BoulderDash4k\build\classes>java Game
3
29
3
3
648
514


Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #9 - Posted 2011-03-26 01:41:37 »

I had to put the following code just after the setVisible:

1  
Toolkit.getDefaultToolkit().sync();


And now it's working on Linux in a consistent manner.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2011-03-26 01:47:08 »

what does the sync() method do?

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #11 - Posted 2011-03-26 03:10:50 »

from the API:

Synchronizes this toolkit's graphics state. Some window systems may do buffering of graphics events.

This method ensures that the display is up-to-date. It is useful for animation.
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-03-26 03:11:55 »

Yes I looked at the API but.....I don't understand what it says Tongue
What does it synchronize the graphics with??

Offline ReBirth
« Reply #13 - Posted 2011-03-26 03:46:12 »

when I was back on the java2d I always put that method on repaint() method but still don't know what it is about. The reason I used it because it makes everything goes fine Cheesy

Offline Addictman

Senior Member


Medals: 3
Projects: 1


Java games rock!


« Reply #14 - Posted 2011-03-26 12:34:43 »

Yes I looked at the API but.....I don't understand what it says Tongue
What does it synchronize the graphics with??

Basically it synchronizes your renderer with your screen's refresh rate. If your renderer wants to update your screen faster than the screen can refresh itself, you might experience one screen-refresh displaying content from more than one frame. This is known as http://en.wikipedia.org/wiki/Screen_tearing
Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2011-03-26 20:38:32 »

Oh so that's for v-sync? Where should I put it in my code, before or after repaint()?

Offline ReBirth
« Reply #16 - Posted 2011-03-27 04:23:22 »

Based on my example, I put it on paint(g) method, at the end.

Offline ra4king

JGO Kernel


Medals: 346
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2011-03-27 06:28:16 »

1  
2  
3  
4  
5  
6  
while(true) {
    //game code
   
    paint(g);
    Toolkit.getDefaultToolkit().sync();
}

Grin

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.

radar3301 (11 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (30 views)
2014-09-19 03:14:18

Dwinin (47 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!