📄 mymouselistener.java
字号:
package com.moveShape;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyMouseListener implements MouseListener,MouseMotionListener
{
private DrawPanel myPanel;
private ArrayList<Shape> shapes;
public MyMouseListener(JPanel Panel)
{
this.myPanel=(DrawPanel)Panel;
this.shapes=myPanel.getShapes();
}
public void mouseClicked(MouseEvent arg0)
{
// TODO Auto-generated method stub
Point clickPoint=arg0.getPoint();
int beClickedNum=0;
for(Shape shape: shapes)
{
if(shape.isInside(clickPoint))
{
beClickedNum++;
}
}
//change the style
if(beClickedNum>0)
{
//get the color
String colorName=JOptionPane.showInputDialog(null,"Enter the number : \n 1 for black \n 2 for yellow \n","change color",
JOptionPane.QUESTION_MESSAGE);
for(Shape shape: shapes)
{
if(colorName!=null)
{
if((colorName.trim()).equals("1"))
{
shape.changeColor(Color.black,"black");
}
else if((colorName.trim()).equals("2"))
{
shape.changeColor(Color.yellow,"yellow");
}
}
}
myPanel.repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0)
{
// TODO Auto-generated method stub
Point source=arg0.getPoint();
for(Shape shape: shapes)
{
shape.isInside(source);
}
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
Point destination=arg0.getPoint();
for(Shape shape: shapes)
{
shape.move(destination);
myPanel.repaint();
}
}
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -