posted 22 years ago
Below is part of my program,and it is expected to view the special subimage with mouse dragging.
public boolean mouseDrag(MouseEvent e,int x,int y) {
//to veirfy that the mouse is on the image;
if(x>0 && y>height+20 && x<img.getWidth(this) && y<img.getHeight(this)+height+20)
{
if(ini)
ini=false;
centerX=x;
centerY=y-height-20;
if(centerX<width/2)
centerX=width/2;
if(centerX>(size().width-width/2))
centerX=size().width-width/2;
if(centerY<height/2)
centerY=height/2;
if(centerY<(size().height-height/2))
centerY=size().height-height/2;
//show the rectangle subimage centered by (centerX,centerY)
temp=this.createImage(new FilteredImageSource(img.getSource(), new CropImageFilter(centerX-width/2, centerY-height/2, width, height)));
screen=temp;
this.update(this.getGraphics());
}
return true;
}
public void paint(Graphics g) {
g.drawImage(img,0,height+20,this);
g.drawImage(screen,0,0,this);
g.setColor(Color.red);
g.drawRect(0,20+height,img.getWidth(this),size().height-20-height);
g.setColor(Color.black);
g.drawRect(0,0,width,height);
}
For help!