java 鼠标形状_java-如何在线描程序中拖动鼠标时使用户看到形状?

//DrawPanel

import java.awt.Graphics;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JPanel;

import javax.swing.JLabel;

import javax.swing.JFrame;

public class DrawLine extends JPanel

{

private LineClass lines[];

private int lineCount;

private LineClass currentLine;

public JLabel statusLabel;

private int currShapeX1,currShapeY1;

public DrawLine()

{

statusLabel = new JLabel("(0,0)");

lines = new LineClass[100];

lineCount = 0;

currentLine = null;

MouseHandler handler = new MouseHandler();

addMouseListener(handler);

addMouseMotionListener(handler);

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

for(int count = 0; count < lineCount; ++count)

{

lines[count].draw(g);

}

}

public static void main(String args[])

{

JFrame frame = new JFrame();

DrawLine panel = new DrawLine();

frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

frame.add(panel);

frame.setSize(400,400);

frame.setVisible(true);

}

private class MouseHandler extends MouseAdapter implements MouseMotionListener

{

public void mousePressed(MouseEvent event)

{

//it assigns currentShape a new shape and initializes both points to the mouse position.

currShapeX1 = event.getX();

currShapeY1 = event.getY();

}

public void mouseReleased(MouseEvent event)

{

//finish drawing the current shape and place it in the array

//Set the second point of currentShape to the current mouse position

currentLine = new LineClass(currShapeX1,currShapeY1,event.getX(),event.getY());

// and add currentShape to the array.

//Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.

lines[lineCount] = currentLine;

lineCount++;

currentLine = null;

repaint();

}

public void mouseDragged(MouseEvent event)

{

//currently not working

/*What is desired:

* As you drag the mouse across the panel,the shape should be showing

* The shape should change in size each time you drag the mouse

* Only one shape should be shown as the mouse is being dragged

* The shape that should be displayed finally is that which was being displayed at the moment the mouse was released

* */

//it sets the second point of the currentShape to the current mouse position and calls method repaint

//finish drawing the current shape and place it in the array

//Set the second point of currentShape to the current mouse position

currentLine = new LineClass(currShapeX1,event.getY());

// and add currentShape to the array.

//Instance variable shapeCount determines the insertion index. Set currentShape to null and call method repaint to update the drawing with the new shape.

lines[lineCount] = currentLine;

currentLine = null;

repaint();

statusLabel.setText(String.format("(%d,%d)",event.getY()));

}

public void mouseMoved(MouseEvent event)

{

//to set the text of the statusLabel so that it displays the mouse coordinates—this will update the label with the coordinates every time the user moves

//(but does not drag) the mouse within the DrawPanel

statusLabel.setText(String.format("(%d,event.getY()));

}

}

}

//LineClass

class LineClass

{

private int x1;

private int y1;

private int x2;

private int y2;

public LineClass(int x1,int y1,int x2,int y2)

{

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

}

public void draw(Graphics g)

{

g.drawLine(x1,y1,x2,y2);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值