Swing 自定义控件学习笔记

来源:互联网 发布:thinkphp sqlserver 编辑:程序博客网 时间:2024/04/28 14:29

做一个用Swing开发的小工具,需要用到一个可拉伸的矩形控件,学习了下极客学院的自定义控件的视频教程记录一下~^_^

自定义控件主要实现Jcomponent中的4个方法来绘制控件:

  • paintComponent(Graphics)  绘制组件自身
  • paintBorder(Graphics)  绘制组件border边框
  • paintChildren(Graphics) 绘制该控件内部的子控件
  • paint(Graphics) 依次调用上面的函数来进行绘制
调用的次序依次是paintComponent、paintBorder、paintChildren方法,其中Graphics对象进行具体的绘制,可以强转为Graphics2D获得更多的绘图API,如消除锯齿等。

实现可拉伸的矩形框参考 http://blog.csdn.net/fancy888/article/details/7740958

主要实现:

  • 绘制矩形并绘制8个用来调整大小的手柄
  • 拖动手柄可以调整矩形框的大小
  • 在矩形框内部可以拖动整个矩形框的位置

一共两个类

PointRectangle:扩展了AWT的Rectangle类,按左上角和右下角两个坐标点来定义矩形(AWT默认是按左上角坐标和长宽来定义矩形的)

import java.awt.Rectangle;public class PointRectangle extends Rectangle {private static final long serialVersionUID = -2465363144347743255L;int x1, x2, y1, y2;public PointRectangle(int x1, int y1, int x2, int y2) {this.x1 = x = x1;this.x2 = x2;this.y1 = y = y1;this.y2 = y2;width = x2 - x1;height = y2 - y1;if (x2 < x1) {x = x2;width = x1 - x2;}if (y2 < y1) {y = y2;height = y1 - y2;}}public PointRectangle(Rectangle rectangle) {super(rectangle.x, rectangle.y, rectangle.width, rectangle.height);this.x1 = rectangle.x;this.y1 = rectangle.y;this.x2 = rectangle.x + rectangle.width;this.y2 = rectangle.y + rectangle.height;}}

RectBound :自定义的矩形边框控件

import java.awt.AlphaComposite;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Cursor;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Rectangle;import java.awt.event.MouseEvent;import javax.swing.JPanel;import javax.swing.event.MouseInputListener;public class RectBoundS extends JPanel implements MouseInputListener {private static final long serialVersionUID = -8362626432551491341L;boolean selecting;int x, y; // 鼠标点击坐标int rbx, rby; // jpanel原点坐标PointRectangle rectBound = null;// 当前绘制的矩形框Rectangle[] handles = new Rectangle[8];// 8个Resize手柄int activeHandle = -1; // 激活的Resize手柄public RectBoundS() {// 添加鼠标事件监听addMouseListener(this);addMouseMotionListener(this);}public RectBoundS(Rectangle rec) {rectBound = new PointRectangle(rec);// 添加鼠标事件监听addMouseListener(this);addMouseMotionListener(this);}// 返回当前有效的矩形public Rectangle getSelection() {return rectBound;}// 清除选择的矩形public void clearSelection() {rectBound = null;selecting = false;activeHandle = -1;for (int i = 0; i < handles.length; i++) {handles[i] = null;}}@Overrideprotected void paintComponent(Graphics g) {
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Monaco;"><span style="color: #931a68"><span style="white-space:pre"></span>  super</span>.paintComponent(<span style="color: #7e504f">g</span>); //必须先执行下父类的该方法</p>if (rectBound == null)rectBound = new PointRectangle(new Rectangle(getX(), getY(),getWidth() / 2, getHeight() / 2));Graphics2D graphics = (Graphics2D) g;if (rectBound.isEmpty()) {clearSelection();return;}graphics.setStroke(new BasicStroke(1));// 设置选择框底色笔触graphics.setPaint(Color.BLUE);// 设置边框底色颜色graphics.draw(rectBound);// 绘制选择边框底色graphics.setPaint(Color.WHITE);// 设置边框颜色graphics.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND, 1f, new float[] { 5 }, 0));// 设置边框笔触graphics.draw(rectBound);// 绘制选择边框// 定义手柄handles[0] = new Rectangle(rectBound.x - 2, rectBound.y - 2, 4, 4);handles[1] = new Rectangle(rectBound.x + rectBound.width / 2 - 2,rectBound.y - 2, 4, 4);handles[2] = new Rectangle(rectBound.x + rectBound.width - 2,rectBound.y - 2, 4, 4);handles[3] = new Rectangle(rectBound.x + rectBound.width - 2,rectBound.y + rectBound.height / 2 - 2, 4, 4);handles[4] = new Rectangle(rectBound.x + rectBound.width - 2,rectBound.y + rectBound.height - 2, 4, 4);handles[5] = new Rectangle(rectBound.x + rectBound.width / 2 - 2,rectBound.y + rectBound.height - 2, 4, 4);handles[6] = new Rectangle(rectBound.x - 2, rectBound.y+ rectBound.height - 2, 4, 4);handles[7] = new Rectangle(rectBound.x - 2, rectBound.y+ rectBound.height / 2 - 2, 4, 4);graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));// 设置填充不透明graphics.setPaint(Color.YELLOW);// 设置手柄填充颜色// 填充手柄for (Rectangle handle : handles)graphics.fill(handle);graphics.setPaint(Color.BLACK);// 设置手柄边框颜色graphics.setStroke(new BasicStroke(1f));// 设置实线笔触// 绘制手柄边框for (Rectangle handle : handles)graphics.draw(handle);}@Overridepublic void mousePressed(MouseEvent e) {x = e.getX();y = e.getY();rbx = (int) rectBound.getX();rby = (int) rectBound.getY();}@Overridepublic void mouseReleased(MouseEvent e) {if (selecting || activeHandle >= 0) {repaint();selecting = false;activeHandle = -1;setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));System.out.println("左上角坐标(" + rectBound.x1 + "," + rectBound.y1+ "), 右下角坐标(" + rectBound.x2 + "," + rectBound.y2 + ")");}}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}@Overridepublic void mouseDragged(MouseEvent e) {if (activeHandle >= 0) {switch (activeHandle) {case 0:rectBound = new PointRectangle(e.getX(), e.getY(),rectBound.x2, rectBound.y2);break;case 1:rectBound = new PointRectangle(rectBound.x1, e.getY(),rectBound.x2, rectBound.y2);break;case 2:rectBound = new PointRectangle(rectBound.x1, e.getY(),e.getX(), rectBound.y2);break;case 3:rectBound = new PointRectangle(rectBound.x1, rectBound.y1,e.getX(), rectBound.y2);break;case 4:rectBound = new PointRectangle(rectBound.x1, rectBound.y1,e.getX(), e.getY());break;case 5:rectBound = new PointRectangle(rectBound.x1, rectBound.y1,rectBound.x2, e.getY());break;case 6:rectBound = new PointRectangle(e.getX(), rectBound.y1,rectBound.x2, e.getY());break;case 7:rectBound = new PointRectangle(e.getX(), rectBound.y1,rectBound.x2, rectBound.y2);break;}repaint();} else if (null != rectBound && rectBound.contains(e.getX(), e.getY())) {rectBound.setLocation(rbx + e.getX() - x, rby + e.getY() - y);rectBound.x1 = (int) rectBound.getX();rectBound.y1 = (int) rectBound.getY();rectBound.x2 = (int) (rectBound.getX() + rectBound.getWidth());rectBound.y2 = (int) (rectBound.getY() + rectBound.getHeight());System.out.println("左上角坐标(" + rectBound.getX() + ","+ rectBound.getY() + "), 右下角坐标("+ (rectBound.getX() + rectBound.getWidth()) + ","+ (rectBound.getY() + rectBound.getHeight()) + ")");repaint();setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));} else {//}}@Overridepublic void mouseMoved(MouseEvent e) {for (int i = 0; i < handles.length; i++) {if (handles[i] != null && handles[i].contains(e.getX(), e.getY())) {activeHandle = i;switch (i) {case 0:case 4:setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));break;case 1:case 5:setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));break;case 2:case 6:setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));break;case 3:case 7:setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));break;}return;}}if (rectBound.contains(e.getX(), e.getY())) {activeHandle = -1;setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));} else {activeHandle = -1;setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));}}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}}



0 0
原创粉丝点击