JComponentBounds: Unterschied zwischen den Versionen
Aus Byte-Welt Wiki
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 4: | Zeile 4: | ||
* JComponentBounds.java | * JComponentBounds.java | ||
* | * | ||
* This class aims at making | * This class aims at making any JComponent resizable | ||
* simply by putting it inside a JInternalFrame. | |||
* The wrapped JComponent is made moveable | |||
* by means of a MouseInputAdapter. | |||
* The class includes a "snap to grid" option. | |||
* | * | ||
* author: Andre Uhres | * author: Andre Uhres | ||
* | * update April 15, 2010 at 6:00 | ||
* update December 21, 2010 at 15:00 | |||
*/ | */ | ||
| Zeile 18: | Zeile 23: | ||
public class JComponentBounds extends JInternalFrame { | public class JComponentBounds extends JInternalFrame { | ||
public static final int FOCUS_GAINED = 97531; | |||
private JComponent componentsContainer; | private JComponent componentsContainer; | ||
private boolean focused; | private boolean focused; | ||
private JComponent comp; | private JComponent comp; | ||
private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f); | private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f); | ||
| Zeile 42: | Zeile 46: | ||
} | } | ||
public JComponentBounds(final JComponent comp, final int x, final int y, final boolean focused) { | public JComponentBounds(final JComponent comp, final int x, final int y, | ||
final boolean focused) { | |||
super(); | super(); | ||
this.comp = comp; | this.comp = comp; | ||
this.focused = focused; | this.focused = focused; | ||
setBounds(x, y, comp.getPreferredSize().width, comp.getPreferredSize().height); | setBounds(x, y, | ||
comp.getPreferredSize().width, comp.getPreferredSize().height); | |||
setResizable(true); | setResizable(true); | ||
setVisible(true); | setVisible(true); | ||
| Zeile 52: | Zeile 58: | ||
setBackground(TRANSPARENT); | setBackground(TRANSPARENT); | ||
((BasicInternalFrameUI) getUI()).setNorthPane(null); | ((BasicInternalFrameUI) getUI()).setNorthPane(null); | ||
setBorder(BorderFactory.createEmptyBorder(MARGIN, MARGIN, MARGIN, MARGIN)); | setBorder(BorderFactory.createEmptyBorder( | ||
MARGIN, MARGIN, MARGIN, MARGIN)); | |||
comp.setFocusable(true); | comp.setFocusable(true); | ||
add(comp); | add(comp); | ||
ml = new MouseInputAdapter() { | ml = new MouseInputAdapter() { | ||
private Point p1; | |||
@Override | @Override | ||
public void mousePressed(final MouseEvent e) { | public void mousePressed(final MouseEvent e) { | ||
p1 = e.getPoint(); | |||
componentsContainer = (JComponent) getParent(); | componentsContainer = (JComponent) getParent(); | ||
e.setSource(JComponentBounds.this); | e.setSource(JComponentBounds.this); | ||
//componentsContainer might use the event | //componentsContainer might use the mousePressed event | ||
//to take hidden components to the top: | //to take hidden components to the top: | ||
componentsContainer.dispatchEvent(e); | componentsContainer.dispatchEvent(e); | ||
| Zeile 72: | Zeile 80: | ||
componentsContainer = (JComponent) getParent(); | componentsContainer = (JComponent) getParent(); | ||
e.setSource(JComponentBounds.this); | e.setSource(JComponentBounds.this); | ||
//componentsContainer might use the event | //componentsContainer might use the mouseReleased event | ||
//to take hidden components to the top: | //to take hidden components to the top: | ||
componentsContainer.dispatchEvent(e); | componentsContainer.dispatchEvent(e); | ||
| Zeile 82: | Zeile 90: | ||
return; | return; | ||
} | } | ||
//drag operation: | |||
Point p2 = e.getPoint(); | |||
Point loc = getLocation(); | |||
loc.translate(p2.x - p1.x, p2.y - p1.y); | |||
setLocation(loc); | |||
componentsContainer = (JComponent) getParent(); | componentsContainer = (JComponent) getParent(); | ||
componentsContainer. | //auto scroll: | ||
componentsContainer.scrollRectToVisible( | |||
new Rectangle(loc.x, loc.y, getWidth(), getHeight())); | |||
componentsContainer. | //componentsContainer might use the focusGained event | ||
//to keep the dragged component in front: | |||
componentsContainer.getFocusListeners()[0].focusGained( | |||
new FocusEvent(JComponentBounds.this, JComponentBounds.FOCUS_GAINED)); | |||
} | } | ||
}; | }; | ||
| Zeile 95: | Zeile 110: | ||
public void keyPressed(final KeyEvent e) { | public void keyPressed(final KeyEvent e) { | ||
componentsContainer = (JComponent) getParent(); | componentsContainer = (JComponent) getParent(); | ||
//componentsContainer might use the event | //componentsContainer might use the keyPressed event | ||
//to delete the component: | //to delete the component: | ||
componentsContainer.dispatchEvent(e); | componentsContainer.dispatchEvent(e); | ||
} | } | ||
}; | }; | ||
| Zeile 105: | Zeile 119: | ||
@Override | @Override | ||
public void componentResized(final ComponentEvent e) { | public void componentResized(final ComponentEvent e) { | ||
((JComponent) e.getSource()).revalidate(); | |||
componentsContainer = (JComponent) | componentsContainer = (JComponent) getParent(); | ||
componentsContainer. | //componentsContainer might use the componentResized event | ||
//to transfer focus to the resizing component: | |||
componentsContainer.dispatchEvent(e); | |||
} | } | ||
@Override | @Override | ||
public void componentMoved(final ComponentEvent e) { | public void componentMoved(final ComponentEvent e) { | ||
((JComponent) e.getSource()).revalidate(); | |||
} | } | ||
}; | }; | ||
| Zeile 119: | Zeile 134: | ||
} | } | ||
/* Move and resize this component. | |||
* This method has been overridden to implement the "snap to grid" option. | |||
*/ | |||
@Override | @Override | ||
public void reshape(final int x, final int y, final int width, final int height) { | public void reshape(final int x, final int y, | ||
final int width, final int height) { | |||
if (snapToGrid) { | if (snapToGrid) { | ||
int | //convert all parameters to multiples of MARGIN: | ||
int | int gridx = x / MARGIN * MARGIN; | ||
int | int gridy = y / MARGIN * MARGIN; | ||
int | int gridwidth = width / MARGIN * MARGIN; | ||
if ( | int gridheight = height / MARGIN * MARGIN; | ||
//if not horizontally on the grid, round up the grid width: | |||
if (gridx != x && gridwidth != width) { | |||
gridwidth += MARGIN; | |||
} | } | ||
if ( | //if not vertically on the grid, round up the grid height: | ||
if (gridy != y && gridheight != height) { | |||
gridheight += MARGIN; | |||
} | } | ||
super.reshape( | //snap to the grid: | ||
}else{ | super.reshape(gridx, gridy, gridwidth, gridheight); | ||
} else { | |||
super.reshape(x, y, width, height); | super.reshape(x, y, width, height); | ||
} | } | ||
| Zeile 144: | Zeile 167: | ||
public void setSnapToGrid(final boolean snapToGrid) { | public void setSnapToGrid(final boolean snapToGrid) { | ||
this.snapToGrid = snapToGrid; | this.snapToGrid = snapToGrid; | ||
} | } | ||
| Zeile 200: | Zeile 211: | ||
rect.height -= (insets.top + insets.bottom); | rect.height -= (insets.top + insets.bottom); | ||
return rect; | return rect; | ||
} | } | ||
} | } | ||
Version vom 21. Dezember 2010, 14:16 Uhr
Die Klasse "JComponentBounds" erlaubt es, JComponents zu verschieben und in der Grösse zu verändern. Die Klasse "PictureDemo" weiter unten zeigt ein Anwendungsbeispiel. <code=java> /*
* JComponentBounds.java * * This class aims at making any JComponent resizable * simply by putting it inside a JInternalFrame. * The wrapped JComponent is made moveable * by means of a MouseInputAdapter. * The class includes a "snap to grid" option. * * author: Andre Uhres * update April 15, 2010 at 6:00 * update December 21, 2010 at 15:00 */
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.plaf.basic.*;
public class JComponentBounds extends JInternalFrame {
public static final int FOCUS_GAINED = 97531; private JComponent componentsContainer; private boolean focused; private JComponent comp; private static final Color TRANSPARENT = new Color(0.0f, 0.0f, 0.0f, 0.0f); public static final int MARGIN = 5; private KeyAdapter kl; private MouseInputAdapter ml; private ComponentAdapter cl; private boolean snapToGrid;
public JComponentBounds() {
this(new JLabel("jComponentBounds"));
}
public JComponentBounds(final JComponent comp) {
this(comp, 0, 0, false);
}
public JComponentBounds(final JComponent comp, final boolean focused) {
this(comp, 0, 0, focused);
}
public JComponentBounds(final JComponent comp, final int x, final int y,
final boolean focused) {
super();
this.comp = comp;
this.focused = focused;
setBounds(x, y,
comp.getPreferredSize().width, comp.getPreferredSize().height);
setResizable(true);
setVisible(true);
setOpaque(false);
setBackground(TRANSPARENT);
((BasicInternalFrameUI) getUI()).setNorthPane(null);
setBorder(BorderFactory.createEmptyBorder(
MARGIN, MARGIN, MARGIN, MARGIN));
comp.setFocusable(true);
add(comp);
ml = new MouseInputAdapter() {
private Point p1;
@Override
public void mousePressed(final MouseEvent e) {
p1 = e.getPoint();
componentsContainer = (JComponent) getParent();
e.setSource(JComponentBounds.this);
//componentsContainer might use the mousePressed event
//to take hidden components to the top:
componentsContainer.dispatchEvent(e);
}
@Override
public void mouseReleased(final MouseEvent e) {
componentsContainer = (JComponent) getParent();
e.setSource(JComponentBounds.this);
//componentsContainer might use the mouseReleased event
//to take hidden components to the top:
componentsContainer.dispatchEvent(e);
}
@Override
public void mouseDragged(final MouseEvent e) {
if (!JComponentBounds.this.isFocused()) {
return;
}
//drag operation:
Point p2 = e.getPoint();
Point loc = getLocation();
loc.translate(p2.x - p1.x, p2.y - p1.y);
setLocation(loc);
componentsContainer = (JComponent) getParent();
//auto scroll:
componentsContainer.scrollRectToVisible(
new Rectangle(loc.x, loc.y, getWidth(), getHeight()));
//componentsContainer might use the focusGained event
//to keep the dragged component in front:
componentsContainer.getFocusListeners()[0].focusGained(
new FocusEvent(JComponentBounds.this, JComponentBounds.FOCUS_GAINED));
}
};
kl = new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
componentsContainer = (JComponent) getParent();
//componentsContainer might use the keyPressed event
//to delete the component:
componentsContainer.dispatchEvent(e);
}
};
cl = new ComponentAdapter() {
@Override
public void componentResized(final ComponentEvent e) {
((JComponent) e.getSource()).revalidate();
componentsContainer = (JComponent) getParent();
//componentsContainer might use the componentResized event
//to transfer focus to the resizing component:
componentsContainer.dispatchEvent(e);
}
@Override
public void componentMoved(final ComponentEvent e) {
((JComponent) e.getSource()).revalidate();
}
};
addAllListeners();
}
/* Move and resize this component.
* This method has been overridden to implement the "snap to grid" option.
*/
@Override
public void reshape(final int x, final int y,
final int width, final int height) {
if (snapToGrid) {
//convert all parameters to multiples of MARGIN:
int gridx = x / MARGIN * MARGIN;
int gridy = y / MARGIN * MARGIN;
int gridwidth = width / MARGIN * MARGIN;
int gridheight = height / MARGIN * MARGIN;
//if not horizontally on the grid, round up the grid width:
if (gridx != x && gridwidth != width) {
gridwidth += MARGIN;
}
//if not vertically on the grid, round up the grid height:
if (gridy != y && gridheight != height) {
gridheight += MARGIN;
}
//snap to the grid:
super.reshape(gridx, gridy, gridwidth, gridheight);
} else {
super.reshape(x, y, width, height);
}
}
public boolean isSnapToGrid() {
return snapToGrid;
}
public void setSnapToGrid(final boolean snapToGrid) {
this.snapToGrid = snapToGrid;
}
private void addAllListeners() {
comp.addMouseListener(ml);
comp.addMouseMotionListener(ml);
comp.addKeyListener(kl);
addComponentListener(cl);
}
public void removeAllListeners() {
comp.removeMouseListener(ml);
comp.removeMouseMotionListener(ml);
comp.removeKeyListener(kl);
removeComponentListener(cl);
}
public void setComponent(final JComponent comp) {
//remove old component:
removeAllListeners();
remove(this.comp);
//add new component:
this.comp = comp;
comp.setFocusable(true);
add(comp);
addAllListeners();
}
public void setFocused(final boolean focused) {
this.focused = focused;
repaint();
}
public boolean isFocused() {
return focused;
}
public Rectangle getComponentRect() {
Insets insets = getInsets();
Rectangle rect = getBounds();
rect.x += insets.left;
rect.y += insets.top;
rect.width -= (insets.right + insets.left);
rect.height -= (insets.top + insets.bottom);
return rect;
}
} </code=java>
