import javax.swing.*; import java.awt.*; /** Square frames */ public class MyFrame extends JFrame { private int previousMinDim= -1; //Min dim before frame gets squared //Initialize to -1 to indicate no prev dim /** Make this frame a square */ public void makeSquare() { this.previousMinDim= Math.min(this.getWidth(), this.getHeight()); int dim= Math.max(this.getHeight(), this.getWidth()); this.setSize(dim,dim); } /** get the minimum dimension of the frame before it got squared */ public int getPreviousMinDim() { return this.previousMinDim; } /** Draw a square in the frame */ public void paint(Graphics g) { g.setColor(Color.blue); //Move the origin to left-top cell below title bar g.translate(this.getInsets().left, this.getInsets().top); //Draw square in center of this frame g.drawRect((this.getWidth()-100)/2, (this.getHeight()-100)/2, 100, 100); //What happens if you specify a negative coordinate? g.drawRect(-10, (this.getHeight()-100)/2, 100, 100); } }