JComponentBounds

Aus Byte-Welt Wiki
Zur Navigation springenZur Suche springen

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>