constrained resize
March 7, 2008 on 7:45 pm | In Swing | 1 CommentOK, I have a JFrame whose dimensions had to be constrained on resize. Basically, it has to grow equally in width and height (think Checkerboard). In order to do that, I had to override the JFrame’s setBounds. However, I had no idea how to apply this same behavior when teh Frame is maximized. When maximized, the Frame would simply blow up and fit the entire screen. Well, it turns out that the same way setSize has been deprecated, setMaximiumSize - although not deprecated, it’s ignored for Frames. So use setMaximizedBounds.
Rectangle bounds = getGraphicsConfiguration().getBounds();
int minOfSizes = (int) Math.min(bounds.getWidth(), bounds.getHeight());
setMaximizedBounds(new Rectangle(new Dimension(minOfSizes, minOfSizes))); // viola!
Tic Tac Toe
December 22, 2005 on 12:42 am | In Swing | No CommentsI’ve been working on a simple Tic Tac Toe game. Most things going well, but I’ve been working on the painting of the X’s and O’s within the insets of a given JButton. In the end, the calculation that actually centered it drove me crazy: I had to use 2 * the width of paint stroke.
Anyway, I’m looking for a cleaner way to draw the X and O Icon within a JButton. Here’s the current image; it currently resizes well, but there’s gotta be another way. Any suggestions? Thanks in advance. Here’s the paint code:
public void paintIcon(Component c, Graphics g, int x, int y) {
if (!CLEAR_SQUARE.equals(type)) {
int strokeWidth = calcStrokeWidth(getWidth(), getHeight());
int strokeWidthX2 = strokeWidth * 2;
Insets insets = getInsets();
int width = getWidth() - strokeWidthX2 - insets.left - insets.right;
int height = getHeight() - strokeWidthX2 - insets.top - insets.bottom;
int left = insets.left + strokeWidth;
int right = left + width;
int top = insets.top + strokeWidth;
int bottom = top + height;
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(getStroke(strokeWidth));
g2.setColor(getForeground());
g2.setBackground(getBackground());
if (X_SQUARE.equals(type)) {
g2.drawLine(left, top, right, bottom);
g2.drawLine(left, bottom, right, top);
} else if (O_SQUARE.equals(type)) {
g2.drawOval(left, top, width, height);
}
}
}
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^

