Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Scaling Swing components for the size of the screen?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Benner




PostPosted: Wed Apr 17, 2013 11:00 pm   Post subject: Scaling Swing components for the size of the screen?

Hi. this is probably a easily solved question because I'd imagine every gui based program has to do this, however I have no idea how to do it.

I'm making this program ( http://youtu.be/XcuBvj0pw-E?t=1m5s ). Lets say I want to have 1600 cells/squares on the screen. What I've been doing is creating a 2d array of JLabels and placing them on the contentpane with a GridLayout(rows, colums, Horizontal space, vertical space) so that they form a grid. I use label.setIcon to change the picture of the label to either the on or off state.

my problem is that the way I am doing it, I need to set the horizontal space and vertical space by trial and error so that it creates a grid like in this picture ( http://oi48.tinypic.com/1zyhehh.jpg ). If I dont have the horizontal gap and vertical gap specifically a number ideal for the screen size that my program is being run on, I get things like ( http://oi45.tinypic.com/1e83u0.jpg ) or ([url] http://oi45.tinypic.com/241ouif.jpg[/url]).

So i guess what I need to do is somehow scale the size of the window and the spacing between each label for the screen's size. How would I do this?

The size of each cell/square is 10x10 pixels. What I've been doing is using ScreenSize.getWidth and .getHeight to get the dimensions of the screen, and then dividing that by a specific number, which changes the spacing between the labels and changes the number of labels.

for example

code:

String height = (int)(screenSize.getHeight()/6.5);
String width = (int)(screenSize.getWidth()/15);

...

JLabel cell = new JLabel[height][width];
contentPane.setLayout(new GridLayout(height, width);
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Apr 18, 2013 2:10 pm   Post subject: RE:Scaling Swing components for the size of the screen?

You would be better off just using a JPanel to draw the cells, rather than 1600 separate UI elements.

Then you can just use the .getGraphics() method to get a Graphics2D object, which you can use to draw lines and coloured blocks much easier than dealing with a multitude of JLabels and GridLayout and so forth. It's also a lot easier to scale them correctly.

To detect where clicks happen, in your implementation of MouseListener (mousePressed(), mouseClicked(), etc), look at the MouseEvent's .getX() and .getY() to determine which cell was clicked on.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: