import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.color.*;public class testTest 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 testTest(){

addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){

if(rect.contains(e.getX(),e.getY())){
last_x=(int)rect.getX()-e.getX();
last_y=(int)rect.getY()-e.getY();
System.out.println("mouse press in area call update methods");
press=true;
//updateLocation(e);
}else if(gp.contains(e.getX(),e.getY())){
last_x=(int)gp.getBounds().x-e.getX();
last_y=(int)gp.getBounds().y -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.reset();
gp.moveTo(pointX[0],pointY[0]);
for(int i=1;i<pointX.length;i++){
gp.lineTo(pointX[i],pointY[i]);
}
gp.closePath();
}
repaint();
}
public void paint(Graphics g){
Dimension dim=getSize();
int width=0,height=0;
if(firstTime){
width=(int)dim.getWidth();
height=(int)dim.getHeight();
bImg=(BufferedImage)createImage(width,height);
g2=bImg.createGraphics();
g2.setColor(Color.red);
rect.setRect(20,20,40,40);
gp.moveTo(pointX[0],pointY[0]);
for(int i=1;i<pointX.length;i++){
gp.lineTo(pointX[i],pointY[i]);
}
gp.closePath();
firstTime=false;
}
if(g2==null){
g2 = (Graphics2D)g;
System.out.println("return");return;
}

/*if((width!=(int)dim.getWidth())||(height!=(int)dim.getHeight())){
width=(int)dim.getWidth();
height=(int)dim.getHeight();
bImg=(BufferedImage)createImage(width,height);
g2=bImg.createGraphics(); //System.out.println("new create bufferimage's width is "+width+" 's height is "+height);
}*/
width=(int)dim.getWidth();
height=(int)dim.getHeight();
        g2.setColor(Color.black);
        g2.fillRect(0,0,width,height);//System.out.println("width is "+width+" height is "+height);
        /*draw dynamic shape*/
        /*if(flag){
        g2.setColor(Color.red);
rect.setRect(20,20,40,40);
}*/
g2.setColor(Color.red);
        g2.fill(rect);
        g2.fill(gp);//System.out.println("paint");
g.drawImage(bImg,0,0,this);
} public static void main(String args[]){
testTest p=new testTest();
JFrame jf=new JFrame("test");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(p);
jf.setSize(300,300);
jf.setVisible(true);
}}