Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
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  
  Problem Centering Canvas on JFrame  (Read 2731 times)
0 Members and 1 Guest are viewing this topic.
Offline Lucas_F98

Senior Newbie





« Posted 2012-07-03 05:39:24 »

Hello Guys,

Hope you can help me out on this one...

My code is here... http://pastebin.java-gaming.org/54a6b1d441c

The problem is that the canvas or JPanel (not sure which) isnt centered in the frame. It seems to start at the top left (0,0) but ends 10 - 20 pixels before the end of the frame. Is there any way i can center it or make it fill the whole frame?

Cheers, Lucas
Offline SkyAphid
« Reply #1 - Posted 2012-07-03 06:02:21 »

Hello Guys,

Hop you can help me out on this one...

My code is here... http://pastebin.java-gaming.org/54a6b1d441c

The problem is that the canvas or JPanel (not sure which) isnt centered in the frame. It seems to start at the top left (0,0) but ends 10 - 20 pixels before the end of the frame. Is there any way i can center it or make it fill the whole frame?

Cheers, Lucas


Not sure if this is a good fix, and I never really used JPanel, but when I did, I'd pull the actual frame back, but leave the canvas as the same size. Like:
1  
2  
JPanel.Display(800x600)
Canvas.Size(810x610)


Something along those lines.

“Life is pretty simple: You do some stuff. Most fails. Some works. You do more of what works. If it works big, others quickly copy it. Then you do something else. The trick is the doing something else.” ~Leonardo da Vinci
Offline StumpyStrust
« Reply #2 - Posted 2012-07-03 06:11:16 »

Its is working fine

You are only rendering a 100x100 square so it looks all wrong but if you were to up the size you will see it.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Lucas_F98

Senior Newbie





« Reply #3 - Posted 2012-07-03 06:22:49 »

i dont mean the square... but if you run that code and move the square to the bottom right you see what i mean

Cheers
Offline 65K
« Reply #4 - Posted 2012-07-03 06:39:35 »

You explicitly remove the panel's layout manager.
But with ignored repaint the built in layout stuff probably doesn't work anyway.

Offline Lucas_F98

Senior Newbie





« Reply #5 - Posted 2012-07-03 07:55:13 »

You explicitly remove the panel's layout manager.
But with ignored repaint the built in layout stuff probably doesn't work anyway.

I am unsure as to what you mean could you please give me an example
Offline 65K
« Reply #6 - Posted 2012-07-03 08:05:08 »

This
1  
panel.setLayout(null);

removes the layout manager from panel. Thus, child components are not automatically positioned.

So, this
1  
panel.add(canvas, BorderLayout.CENTER);

has no effect.

But even if you keep the layout manager, calling
1  
canvas.setIgnoreRepaint(true);

prevents the layout system to be invoked at all (as far as I remember).

The conclusion is, there is no implicit layout managing without further manual steps if active rendering is used.

Offline Lucas_F98

Senior Newbie





« Reply #7 - Posted 2012-07-03 08:15:29 »

So, how can this be done, all i want is to be able to paint everywhere and such. Have you run that code and seen what i mean?

it just isnt drawing to the bottom right corner...

Screenshots:

Draws to top left fine:


But not to bottom right:



Help appreciated,

Lucas
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #8 - Posted 2012-07-03 08:31:48 »

Set the same values for the size, preferred size, minimum size, and maximum size on the Canvas, then call JFrame.pack()

Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2012-07-03 08:33:40 »

Set the same values for the size, preferred size, minimum size, and maximum size on the Canvas, then call JFrame.pack()
There, FTFY. Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Lucas_F98

Senior Newbie





« Reply #10 - Posted 2012-07-03 08:40:02 »

Set the same values for the size, preferred size, minimum size, and maximum size on the Canvas, then call JFrame.pack()

I did that... (updated method below) but still same problem. Any ideas.


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  
 public Game(){      
     Dimension size = new Dimension(WIDTH, HEIGHT);

      frame = new JFrame(NAME);
      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);
      panel.setFocusable(true);
      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);    
      panel.add(canvas, BorderLayout.CENTER);
     
      canvas.setSize(size);
      canvas.setPreferredSize(size);
      canvas.setMinimumSize(size);
      canvas.setMaximumSize(size);
     
     
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);      
     
     
      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();    
      canvas.requestFocus();

      canvas.addKeyListener(new KeyListener() {               //KEYLISTENER
          public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_UP){
               Game.up = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
               Game.down = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
               Game.left = true;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
               Game.right = true;
            }
           }

           public void keyReleased(KeyEvent e) {
              if(e.getKeyCode() == KeyEvent.VK_UP){
               Game.up = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_DOWN){
               Game.down = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_LEFT){
               Game.left = false;
            }
            if(e.getKeyCode() == KeyEvent.VK_RIGHT){
               Game.right = false;
            }
             
           }
         public void keyTyped(KeyEvent arg0) {
         }
         });
   
}

Lucas
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2012-07-03 08:40:25 »

Set the same values for the size, preferred size, minimum size, and maximum size on the Canvas, then call JFrame.pack()
There, FTFY. Smiley
Actually I read from another thread that on some occasions if you don't set the preferred size and the size, it won't work :S

Offline 65K
« Reply #12 - Posted 2012-07-03 08:56:29 »

Try this:
1  
frame.setUndecorated(true);


You need to consider the decoration inset stuff and adjust draw positions.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2012-07-03 08:59:52 »

Actually I read from another thread that on some occasions if you don't set the preferred size and the size, it won't work :S
Don't believe everything you read.

Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

You can either write your own LayoutManager, or calculate the coordinates of the component yourself:
1  
2  
int x = (panel.getWidth() - canvas.getWidth()) / 2;
int y = (panel.getHeight() - canvas.getHeight()) / 2;

You must update these coordinates everytime the parent (panel) is resized.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Lucas_F98

Senior Newbie





« Reply #14 - Posted 2012-07-03 09:02:38 »

Try this:
1  
frame.setUndecorated(true);


You need to consider the decoration inset stuff and adjust draw positions.

What if i still want the title bar?


Actually I read from another thread that on some occasions if you don't set the preferred size and the size, it won't work :S
Don't believe everything you read.

Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

You can either write your own LayoutManager, or calculate the coordinates of the component yourself:
1  
2  
int x = (panel.getWidth() - canvas.getWidth()) / 2;
int y = (panel.getHeight() - canvas.getHeight()) / 2;

You must update these coordinates everytime the parent (panel) is resized.

What would i do with the x, y coords?
Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2012-07-03 09:08:30 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
panel.setLayout(new LayoutManager() {
   public void layoutContainer(Container parent) {
      Component child = parent.getComponents()[0];
      Dimension prefSize = child.getPreferredSize();

      int x = (parent.getWidth() - prefSize.width) / 2;
      int y = (parent.getHeight() - prefSize.height) / 2;
      child.setBounds(x, y, prefSize.width, prefSize.height);
   }

   public Dimension minimumLayoutSize(Container parent) {
      return this.preferredLayoutSize(parent);
   }

   public Dimension preferredLayoutSize(Container parent) {
      return parent.getPreferredSize();
   }

   // AFAIK you can leave the add/remove methods of LayoutManager empty, in this specific case.

   public void addLayoutComponent(String name, Component comp) {}
   public void removeLayoutComponent(Component comp) {}
});
panel.setPreferredSize(new Dimension(w, h));

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Lucas_F98

Senior Newbie





« Reply #16 - Posted 2012-07-03 09:13:48 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
panel.setLayout(new LayoutManager() {
   public void layoutContainer(Container parent) {
      Component child = parent.getComponents()[0];
      Dimension prefSize = child.getPreferredSize();

      int x = (parent.getWidth() - prefSize.width) / 2;
      int y = (parent.getHeight() - prefSize.height) / 2;
      child.setBounds(x, y, prefSize.width, prefSize.height);
   }

   // AFAIK you can leave the other methods of LayoutManager empty, in this specific case.
});


Thank you very much Riven and 65K, ra4king, SkyAphid, StumpyStrust

Works!!!
Offline nsigma
« Reply #17 - Posted 2012-07-03 09:37:59 »

Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

hmm .. I'm using

1  
2  
frame.setLayout(new GridBagLayout());
frame.add(canvas);


Does that not do what the OP is looking for?

Incidentally, that does fail to work in FSEM on Windows with the D3D pipeline enabled - no idea why, and as I'm either using software rendering or LWJGL I switched D3D off.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Online Riven
« League of Dukes »

JGO Overlord


Medals: 796
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #18 - Posted 2012-07-03 09:44:49 »

I've always considered GridBagLayout a crime against usability, so I've been avoiding it. Apparently it solves this case in a one-liner.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Lucas_F98

Senior Newbie





« Reply #19 - Posted 2012-07-03 09:47:54 »

Anyway, there are no LayoutManagers to do the vertical alignment you want. FlowLayout can do the horizontal alignment, but obviously that's not enough.

hmm .. I'm using

1  
2  
frame.setLayout(new GridBagLayout());
frame.add(canvas);


Does that not do what the OP is looking for?

Incidentally, that does fail to work in FSEM on Windows with the D3D pipeline enabled - no idea why, and as I'm either using software rendering or LWJGL I switched D3D off.

Yeah that works i think ill go with that... Saves space Tongue

Thanks
Offline nsigma
« Reply #20 - Posted 2012-07-03 09:52:09 »

I've always considered GridBagLayout a crime against usability, so I've been avoiding it. Apparently it solves this case in a one-liner.

 Grin +1.  This may be its one and only use!

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
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.

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

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

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

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (38 views)
2014-09-04 23:08:59
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!