我想创建一个可以使用鼠标在程序中移动图片的程序这是我的代码。我的代码是用 tempX 和 tempY 设置形状的位置,我将它设置为在拖动鼠标时获取鼠标位置,图片将沿鼠标移动。
* 我不能在拖动鼠标时重新绘制这张图片我错过了什么?* 添加 chagnePositionVectors()
public class SiaemsiMouse extends JPanel implements MouseListener,MouseMotionListener {
int tempX = 330;
int tempY = 95;
// position of Siaemsi box
int posX[] = {tempX-30,tempX-30,tempX-30};
int posY[] = {tempY+305,tempY+80,tempY+55};
// position of Siaemsi stick
int posXS1[] = {tempX,tempX,tempX-3,tempX+8,tempX+20,tempX+17};
int posXS2[] = {tempX+20,tempX+20,tempX+17,tempX+28,tempX+40,tempX+37};
int posXS3[] = {tempX+40,tempX+40,tempX+37,tempX+48,tempX+60,tempX+57};
int posXS4[] = {tempX+60,tempX+60,tempX+57,tempX+68,tempX+80,tempX+77};
int posXS5[] = {tempX+80,tempX+80,tempX+77,tempX+88,tempX+100,tempX+97};
int posXS6[] = {tempX+100,tempX+100,tempX+97,tempX+108,tempX+120,tempX+117};
int posXS7[] = {tempX+120,tempX+120,tempX+117,tempX+128,tempX+140,tempX+137};
// All of y for stick are equals
int posYS1[] = {tempY,tempY,tempY-10,tempY-15,tempY-10,tempY};
public SiaemsiMouse(){
addMouseMotionListener(this);
addMouseListener(this);
}
public static void main(String[] args) {
SiaemsiMouse siaemsimouse = new SiaemsiMouse();
JFrame frame = new JFrame();
frame.add(siaemsimouse);
frame.setSize(800,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics graphics){
super.paintComponent(graphics);
Graphics2D g2 = (Graphics2D) graphics;
Random random = new Random();
// Draw Siaemsi
Ellipse2D.Double floor = new Ellipse2D.Double(posX[0],posY[0],200,50);
Rectangle2D.Double body = new Rectangle2D.Double(posX[1],posY[1],200,250);
Ellipse2D.Double top = new Ellipse2D.Double(posX[2],posY[2],200,50);
g2.setPaint(Color.RED);
g2.fill(floor);
g2.fill(body);
g2.setPaint(Color.BLACK);
g2.fill(top);
// Draw siaemsi's stick
Rectangle2D.Double stick1 = new Rectangle2D.Double(posXS1[0],posYS1[0],18,100);
GeneralPath path1 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS1.length);
path1.moveTo(posXS1[1],posYS1[1]);
for (int i = 2; i < posXS1.length; i++) {
path1.lineTo(posXS1[i],posYS1[i]);
}
path1.closePath();
Rectangle2D.Double stick2 = new Rectangle2D.Double(posXS2[0],posYS1[0],18,100);
GeneralPath path2 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS2.length);
path2.moveTo(posXS2[1],posYS1[1]);
for (int i = 2; i < posXS2.length; i++) {
path2.lineTo(posXS2[i],posYS1[i]);
}
path2.closePath();
Rectangle2D.Double stick3 = new Rectangle2D.Double(posXS3[0],posYS1[0],18,100);
GeneralPath path3 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS3.length);
path3.moveTo(posXS3[1],posYS1[1]);
for (int i = 2; i < posXS3.length; i++) {
path3.lineTo(posXS3[i],posYS1[i]);
}
path3.closePath();
Rectangle2D.Double stick4 = new Rectangle2D.Double(posXS4[0],posYS1[0],18,100);
GeneralPath path4 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS4.length);
path4.moveTo(posXS4[1],posYS1[1]);
for (int i = 2; i < posXS4.length; i++) {
path4.lineTo(posXS4[i],posYS1[i]);
}
path4.closePath();
Rectangle2D.Double stick5 = new Rectangle2D.Double(posXS5[0],posYS1[0],18,100);
GeneralPath path5 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS5.length);
path5.moveTo(posXS5[1],posYS1[1]);
for (int i = 2; i < posXS5.length; i++) {
path5.lineTo(posXS5[i],posYS1[i]);
}
path5.closePath();
Rectangle2D.Double stick6 = new Rectangle2D.Double(posXS6[0],posYS1[0],18,100);
GeneralPath path6 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS6.length);
path6.moveTo(posXS6[1],posYS1[1]);
for (int i = 2; i < posXS6.length; i++) {
path6.lineTo(posXS6[i],posYS1[i]);
}
path6.closePath();
Rectangle2D.Double stick7 = new Rectangle2D.Double(posXS7[0],posYS1[0],18,100);
GeneralPath path7 = new GeneralPath(GeneralPath.WIND_EVEN_ODD,posXS7.length);
path7.moveTo(posXS7[1],posYS1[1]);
for (int i = 2; i < posXS7.length; i++) {
path7.lineTo(posXS7[i],posYS1[i]);
}
path7.closePath();
g2.setPaint(new Color(153,153,0));
g2.fill(stick1);
g2.fill(path1);
g2.setPaint(Color.BLACK);
g2.draw(stick1);
g2.draw(path1);
g2.setPaint(new Color(153, 76, 0));
g2.fill(stick2);
g2.fill(path2);
g2.setPaint(Color.BLACK);
g2.draw(stick2);
g2.draw(path2);
g2.setPaint(new Color(255, 255, 0));
g2.fill(stick3);
g2.fill(path3);
g2.setPaint(Color.BLACK);
g2.draw(stick3);
g2.draw(path3);
g2.setPaint(new Color(0, 255, 128));
g2.fill(stick4);
g2.fill(path4);
g2.setPaint(Color.BLACK);
g2.draw(stick4);
g2.draw(path4);
g2.setPaint(new Color(102, 0, 56));
g2.fill(stick5);
g2.fill(path5);
g2.setPaint(Color.BLACK);
g2.draw(stick5);
g2.draw(path5);
g2.setPaint(new Color(255, 0, 127));
g2.fill(stick6);
g2.fill(path6);
g2.setPaint(Color.BLACK);
g2.draw(stick6);
g2.draw(path6);
g2.setPaint(new Color(224, 224, 56));
g2.fill(stick7);
g2.fill(path7);
g2.setPaint(Color.BLACK);
g2.draw(stick7);
g2.draw(path7);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
changePositionVectors(e.getX(),e.getY());
}
@Override
public void mouseMoved(MouseEvent e) {
}
public void changePositionVectors(int xPos, int yPos){
tempX = xPos;
tempY = yPos;
repaint();
}
}