寫一個具有縮放功能的邊框,由於 border class 不是 component,因此沒辦法加入 listener。
考慮將其 extends listener,然後加入 parent 的 listener 之中。
package package1;
import javax.swing.border.*;
import javax.swing.event.MouseInputListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class ResizeBorder extends AbstractBorder implements MouseInputListener {
private int gap;
private Color borderColor;
private int rectWidth, rectHeight;
private boolean selected;
public ResizeBorder(Color color, int gap, int w, int h) {
borderColor = color;
this.gap = gap;
rectWidth = w;
rectHeight = h;
/*
* ResizeObjectListener listener = new ResizeObjectListener();
* this.addMouseListener(listener);
* this.addMouseMotionListener(listener);
*/
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
super.paintBorder(c, g, x, y, width, height);
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
if (selected || usedLarge)
g2d.setColor(Color.BLUE);
else
g2d.setColor(borderColor);
// left-up corner.
g2d.fillRect(x, y, rectWidth, rectHeight);
g2d.fillRect(x, y, rectHeight, rectWidth);
// right-up corner.
g2d.fillRect(width - rectWidth, y, rectWidth, rectHeight);
g2d.fillRect(width - rectHeight, y, rectHeight, rectWidth);
// left-bottom corner.
g2d.fillRect(x, height - rectHeight, rectWidth, rectHeight);
g2d.fillRect(x, height - rectWidth, rectHeight, rectWidth);
// right-bottom corner.
g2d.fillRect(width - rectWidth, height - rectHeight, rectWidth,
rectHeight);
g2d.fillRect(width - rectHeight, height - rectWidth, rectHeight,
rectWidth);
}
}
@Override
public Insets getBorderInsets(Component c) {
return getBorderInsets(c, new Insets(gap, gap, gap, gap));
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = insets.bottom = insets.top = gap;
return insets;
}
@Override
public boolean isBorderOpaque() {
return true;
}
/*------------------------------------*/
private Point p = new Point(0, 0);
private boolean usedLarge;
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
// System.out.println("dragged");
if (SwingUtilities.isLeftMouseButton(e)) {
if (usedLarge) {
Component jl = (Component) e.getSource();
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.setBounds(jl.getX(), jl.getY(), jl.getWidth()
+ (newP.x - p.x), jl.getHeight() + (newP.y - p.y));
p = newP;
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
if (usedLarge)
usedLarge = false;
Component jl = (Component) e.getSource();
jl.repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
Component jl = (Component) e.getSource();
jl.repaint();
selected = false;
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component source = (Component) e.getSource();
selected = true;
System.out.println("selected");
return;
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
selected = true;
Component jl = (Component) e.getSource();
p = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.repaint();
} else {
if (selected == true) {
usedLarge = true;
}
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
}
package package1;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
/**
* more explain by Chinese language : http://javapub.iteye.com/blog/739146 &
* http://hi.baidu.com/wwwpeng584/item/668f6ef7289020db6225d24a
*
* @author owner
*
*/
public class DragObjectListener implements MouseInputListener {
Point p = new Point(0, 0);
public DragObjectListener() {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
if(pt.x < 30 || pt.x > jl.getBounds().getWidth()-30)
return;
if(pt.y < 30 || pt.y > jl.getBounds().getHeight()-30)
return;
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.setLocation(jl.getX() + (newP.x - p.x), jl.getY()
+ (newP.y - p.y));
p = newP;
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
Component source = (Component) e.getSource();
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component source = (Component) e.getSource();
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
if(pt.x < 30 || pt.x > jl.getBounds().getWidth()-30)
return;
if(pt.y < 30 || pt.y > jl.getBounds().getHeight()-30)
return;
p = SwingUtilities.convertPoint(jl, e.getPoint(), jl.getParent());
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
}
package package1;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
public class TestPanel extends JPanel {
TestPanel() {
this.setBounds(50, 50, 100, 100);
LineBorder blackline = new LineBorder(Color.BLACK);
this.add(new JButton("OKKKKKKKKK"));
ResizeBorder rborder = new ResizeBorder(Color.RED, 5, 10, 25);
this.setBorder(rborder);
DragObjectListener listener = new DragObjectListener();
this.addMouseListener(rborder);
this.addMouseMotionListener(rborder);
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.setBackground(Color.ORANGE);
}
}
package package1;
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static int WIDTH = 500, HEIGHT = 300;
MainFrame() {
System.out.println("START");
this.setTitle("ResizeObject");
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
JPanel left, right;
left = new JPanel();
right = new JPanel();
//left.setLayout(new BorderLayout());
//left.add(new JButton("Button"), BorderLayout.NORTH);
left.add(new TestPanel());
right.add(new JButton("Foo"));
this.setLayout(new BorderLayout());
this.add(left, BorderLayout.CENTER);
this.add(right, BorderLayout.WEST);
//this.setLayout(null);
//this.add(new MovePanel());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
文章定位: