当前位置: 技术问答>java相关
对面的牛人看过来,看过来,这里一个paint小事件,小时间
来源: 互联网 发布时间:2015-09-14
本文导语: /*为什么我的gp出的图形不能正确的repaint ??*/ import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.*; import java.awt.color.*; public class test extends JPanel{ int last_x,last_y; boolean firstTime...
/*为什么我的gp出的图形不能正确的repaint ??*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.color.*;
public class test extends JPanel{
int last_x,last_y;
boolean firstTime=true;
BufferedImage bImg;
Graphics2D g2;
Rectangle2D rect=new Rectangle2D.Float();
GeneralPath gp=new GeneralPath(GeneralPath.WIND_NON_ZERO,3);
boolean flag=true;
boolean press=false;
int[] pointX={100,180,140};
int[] pointY={100,100,40};
public test(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
last_x=(int)rect.getX()-e.getX();
last_y=(int)rect.getY()-e.getY();
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse press in area call update methods");
press=true;
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
press=true;
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e){
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse released in area call update methods");
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
updateLocation(e);
}
press=false;
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(press){
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse drag in area call update methods");
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
updateLocation(e);
}
}
}
});
}
public void updateLocation(MouseEvent e){
if(rect.contains(e.getX(),e.getY())){
rect.setRect(last_x+e.getX(),last_y+e.getY(),40,40);
}else if(gp.contains(e.getX(),e.getY())){
pointX[0]=last_x+e.getX();pointY[0]=last_y+e.getY()+60;
pointX[1]=last_x+e.getX()+80;pointY[1]=last_y+e.getY()+60;
pointX[2]=last_x+e.getX()+40;pointY[2]=last_y+e.getY();
gp.moveTo(pointX[0],pointY[0]);
for(int i=1;i
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.color.*;
public class test extends JPanel{
int last_x,last_y;
boolean firstTime=true;
BufferedImage bImg;
Graphics2D g2;
Rectangle2D rect=new Rectangle2D.Float();
GeneralPath gp=new GeneralPath(GeneralPath.WIND_NON_ZERO,3);
boolean flag=true;
boolean press=false;
int[] pointX={100,180,140};
int[] pointY={100,100,40};
public test(){
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
last_x=(int)rect.getX()-e.getX();
last_y=(int)rect.getY()-e.getY();
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse press in area call update methods");
press=true;
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
press=true;
updateLocation(e);
}
}
public void mouseReleased(MouseEvent e){
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse released in area call update methods");
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
updateLocation(e);
}
press=false;
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
if(press){
if(rect.contains(e.getX(),e.getY())){
System.out.println("mouse drag in area call update methods");
updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
updateLocation(e);
}
}
}
});
}
public void updateLocation(MouseEvent e){
if(rect.contains(e.getX(),e.getY())){
rect.setRect(last_x+e.getX(),last_y+e.getY(),40,40);
}else if(gp.contains(e.getX(),e.getY())){
pointX[0]=last_x+e.getX();pointY[0]=last_y+e.getY()+60;
pointX[1]=last_x+e.getX()+80;pointY[1]=last_y+e.getY()+60;
pointX[2]=last_x+e.getX()+40;pointY[2]=last_y+e.getY();
gp.moveTo(pointX[0],pointY[0]);
for(int i=1;i