求助!java 画一个矩形,怎么样让这个白颜色的矩形跟随鼠标移动呢?(背景是非白色)
这是我用的笨方法,监听鼠标移动并重绘新的图形并覆盖旧图形颜色。另一种方法怎么弄?packageauther.abbott;importjava.awt.BorderLay...
这是我用的笨方法,监听鼠标移动并重绘新的图形并覆盖旧图形颜色。
另一种方法怎么弄?
package auther.abbott;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame {
/**
*
*/
private static final long serialVersionUID = -7926796655555593301L;
Container con;
Shape rec;
DrawPanel drawP;
int x=0,y=0;
int ox=0,oy=0;
Boolean flag=false;
Game(String title){
super(title);
this.setBounds(500, 300, 240, 320);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = this.getContentPane();
drawP = new DrawPanel();
Icon icon;
Cursor cursor = new Cursor(icon.paintIcon(this,, 30, 30));
this.setCursor(cursor);
this.setBackground(Color.pink);
con.add(drawP,BorderLayout.CENTER);
drawP.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO 自动生成的方法存根
if(flag==true){
ox=x;
oy=y;
}
x=arg0.getX();
y=arg0.getY();
drawP.repaint();
}
});
}
class DrawPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 6821779692499741237L;
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
rec = new Rectangle2D.Double(x, y, 50, 50);
g2.fill(rec);
if(flag==true){
g2.setColor(Color.pink);
g2.fill(new Rectangle2D.Double(ox, oy, 50, 50));
}
flag=true;
g2.setColor(Color.white);
g2.fill(rec);
}
}
} 展开
另一种方法怎么弄?
package auther.abbott;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame {
/**
*
*/
private static final long serialVersionUID = -7926796655555593301L;
Container con;
Shape rec;
DrawPanel drawP;
int x=0,y=0;
int ox=0,oy=0;
Boolean flag=false;
Game(String title){
super(title);
this.setBounds(500, 300, 240, 320);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con = this.getContentPane();
drawP = new DrawPanel();
Icon icon;
Cursor cursor = new Cursor(icon.paintIcon(this,, 30, 30));
this.setCursor(cursor);
this.setBackground(Color.pink);
con.add(drawP,BorderLayout.CENTER);
drawP.addMouseMotionListener(new MouseMotionListener(){
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO 自动生成的方法存根
if(flag==true){
ox=x;
oy=y;
}
x=arg0.getX();
y=arg0.getY();
drawP.repaint();
}
});
}
class DrawPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 6821779692499741237L;
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
rec = new Rectangle2D.Double(x, y, 50, 50);
g2.fill(rec);
if(flag==true){
g2.setColor(Color.pink);
g2.fill(new Rectangle2D.Double(ox, oy, 50, 50));
}
flag=true;
g2.setColor(Color.white);
g2.fill(rec);
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询