Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] window scaling

Started by joeyg2477 Mar 24, 2006 at 3:54 AM 1 replies 700+ views
Original Post
joeyg2477
joeyg2477
I am creating a application with a bunch of cubes that im drawing on the screen. When I make my window bigger I want the cubes to scale up. Not only fit the window (which I figured out), but also the cubes become bigger too. I have no idea how to do this. Can someone push me in the right direction?
CaptainJester
CaptainJester
You can just scale your graphics object before you use it.
public void paintComponent(Graphics g) {  Graphics2D g2 = (Graphics2D)g;  //if you want your drawing at normal size to be 200 x 200  g2.scale(getWidth() / 200, getHeight() / 200);  //you have to watch the aspect ratio with this  //unless you constrain your JPanel to a square, this can look stretched  //drawing code goes here    .    .    .}

You can guaruntee a square aspect ratio with this
public void paintComponent(Graphics g) {  Graphics2D g2 = (Graphics2D)g;  //if you want your drawing at normal size to be 200 x 200  double scale1 = getWidth() / 200;  double scale2 = getHeight() / 200;  //this one will scale within the bounds of the JPanel, but  //will only fill it in one direction  double scale = (scale1 > scale2) ? scale2 : scale1;  //this one will fill the bounds of the JPanel in one direction, but  //will draw off the other edge  double scale = (scale1 > scale2) ? scale1 : scale2;  g2.scale(scale, scale);  //you have to watch the aspect ratio with this  //unless you constrain your JPanel to a square, this can look stretched  //drawing code goes here    .    .    .}
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.