[java] window scaling
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?
You can just scale your graphics object before you use it.
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 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 . . .}
both make nothing appear on my screen. do I need to change something else in my program. Im using jframe for window and jpanel.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement