Thread: Resize frame
View Single Post
  #2 (permalink)  
Old 07-30-2007, 01:18 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The trick is to base your variable values on the current size of the component.
Code:
import java.awt.*; import javax.swing.*; public class StretchyGraphics extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = getHeight(); g2.drawRect(w/16, h/16, w*7/8, h*7/8); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new StretchyGraphics()); f.setSize(400,400); f.setVisible(true); } }
Reply With Quote