资源描述
代码:
DragDao.java
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
public class DragDao extends JPanel implements MouseMotionListener,
MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
/** 矩形的起点 左上角* */
private Point rectStart = null;
/** 矩形的终点 右下角* */
private Point rectStop = null;
/** 是否绘制虚线矩形* */
private boolean dottedTag = true;
public DragDao() {
this.setBackground(Color.WHITE);
// this.setOpaque(false);
this.setSize(1000, 1000);
rectStart = new Point(0, 0);
rectStop = new Point(0, 0);
//this.addMouseListener(this);
//this.addMouseMotionListener(this);
this.addMouseListener(ma_imgpanel);
this.addMouseMotionListener(mma);
}
MouseMotionListener mma = new MouseMotionListener()
{
@Override
public void mouseMoved(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e)
{
rectStop = e.getPoint();
repaint();
}
};
MouseListener ma_imgpanel = new MouseListener()
{
@Override
public void mouseReleased(MouseEvent e)
{
dottedTag = true;
repaint();
}
@Override
public void mousePressed(MouseEvent e)
{
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = e.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
}
};
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
/** 矩形的宽度* */
int w = Math.abs(rectStop.x - rectStart.x);
/** 矩形的高度* */
int h = Math.abs(rectStop.y - rectStart.y);
/** 起点终点的最小值作为起点* */
Point min = new Point(0, 0);
min.x = Math.min(rectStop.x, rectStart.x);
min.y = Math.min(rectStop.y, rectStart.y);
/** 起点终点的最小值作为终点* */
Point max = new Point(0, 0);
max.x = Math.max(rectStop.x, rectStart.x);
max.y = Math.max(rectStop.y, rectStart.y);
/** 如果是绘制虚线矩形* */
if (dottedTag) {
/** new float[] { 5, 5, } 数组可以改变虚线的密度* */
Stroke dash = new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND, 0.5f, new float[] { 5, 5, }, 0f);
g2.setStroke(dash);
g2.setColor(Color.BLUE);
g2.drawRect(min.x, min.y, w, h);
System.out.println("min.x"+min.x+"min.y"+min.y);
} else {
/*
g2.setStroke(new BasicStroke());
g2.setColor(new Color(198, 214, 239));
g.fillRect(min.x, min.y, w, h);
*/
//g2.draw(new RoundRectangle2D.Double(5, 7, 200, 200,
// 10, 10));
}
}
/** *鼠标拖动 */
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
/** 随着鼠标的拖动 改变终点* */
rectStop = arg0.getPoint();
this.repaint();
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上单击(按下并释放)时调用* */
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标按键在组件上按下时调用 */
public void mousePressed(MouseEvent arg0) {
/** 设置可以进行绘制* */
dottedTag = true;
/** 记录起始点* */
rectStart = arg0.getPoint();
/** 记录起终点* */
rectStop = rectStart;
}
/** 鼠标按钮在组件上释放时调用* */
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
dottedTag = true;
this.repaint();
}
/** 鼠标进入到组件上时调用* */
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/** 鼠标离开组件时调用* */
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
测试代码:
Dragdemo.java
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class DragDemo extends JFrame{
private static final long serialVersionUID = 1L;
public DragDao drag = new DragDao();
public DragDemo() {
super();
drag = new DragDao();
this.add(drag,BorderLayout.CENTER);
Initialization("XuXian JuXing");
}
private void Initialization(String title) {
Dimension screen = getToolkit().getScreenSize();
this.setSize(screen.width*4/5,screen.height*4/5);
this.setTitle(title);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public static void main(String[] args) {
new DragDemo();
}
}
测试截图
展开阅读全文