|
Replies:
1
-
Last Post:
Jun 29, 2007 12:16 PM
by: rms7326
|
|
|
|
|
|
|
Layouting components with a ratio
Posted:
Jun 29, 2007 3:07 AM
|
|
|
Hello. I have been looking over forums and every places i knew for the answer to my current problem. I have a JPanel, using GroupLayout to handle the content. Amongst the components, i have one that ought to be layed out with a fixed ratio (width dependent on height) Actually, i tried 'playing' with preferredSize/miniSize/maxSize, then each and every trick i could gather/discover.
I can not _unfortunatly_ apply aspect ratio to the content of my ratioed(?!) component, as the content must take all place to be visible.
Can someone help?
|
|
|
|
|
|
|
Re: Layouting components with a ratio
Posted:
Jun 29, 2007 12:16 PM
in response to: pepe
|
|
|
Pepe,
I solved it by overriding the sizes. See example source code below (sorry for the loss in code formatting, I don't know how to keep fixed font in these forums) for a card. Not sure if this is the best way to handle it but it works.
Michael
import java.awt.Dimension; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;
import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel;
/** * @author Michael Saunders */ public class CardExample extends JComponent { /** */ public CardExample() { selected = false; widthToHeightRatio = 2.25f/3.5f; addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { synchronized (CardExample.this) { selected = !selected; CardExample.this.repaint(); } } }); }
/** */ public Dimension getClampedSize() { Dimension result = new Dimension(super.getWidth(), super.getHeight()); Dimension min = getMinimumSize(); Dimension max = getMaximumSize(); // clamp the width if (result.width < min.width) result.width = min.width; else if (result.width > max.width) result.width = max.width; // clamp the height if (result.height < min.height) result.height = min.height; else if (result.height > max.height) result.height = max.height; return result; } /** */ public int getWidth() { return calcWidth(getClampedSize()); }
/** */ public int getHeight() { return calcHeight(getClampedSize()); }
/** */ public Dimension getMinimumSize() { return new Dimension(100, calcHeight(100)); }
/** */ public Dimension getMaximumSize() { return new Dimension(200, calcHeight(200)); }
/** */ public Dimension getPreferredSize() { return new Dimension(150, calcHeight(150)); }
/** */ public void paintComponent(Graphics g) { super.paintComponent(g); g.setClip(null); // clear all clipping g.setColor(java.awt.Color.RED);//getBackground()); int regionWidth = super.getWidth(); int regionHeight = super.getHeight(); g.fillRect(0, 0, regionWidth, regionHeight);
if (isSelected()) g.setColor(java.awt.Color.BLUE); else g.setColor(java.awt.Color.CYAN);
int width = getWidth(); int height = getHeight(); int hGap = (regionWidth - width); int vGap = (regionHeight - height); int left = (int)(hGap * getAlignmentX()); int top = (int)(vGap * getAlignmentY()); g.fillRect(left, top, width, height); g.setColor(java.awt.Color.BLACK); g.drawLine(left, top, left+width, top+height); g.drawLine(left, top+height, left+width, top); }
/** */ private boolean isSelected() { return selected; }
/** */ private int calcWidth(int height) { return (int)(height * widthToHeightRatio); }
/** */ private int calcWidth(Dimension size) { int result; if (size.width / widthToHeightRatio > size.height) result = calcWidth(size.height); else result = size.width; return result; }
/** */ private int calcHeight(int width) { return (int)(width / widthToHeightRatio); }
/** */ private int calcHeight(Dimension size) { int result; if (size.height * widthToHeightRatio > size.width) result = calcHeight(size.width); else result = size.height; return result; }
private boolean selected; private float widthToHeightRatio; }
|
|
|
|
|