Le clipping sous Java 2D
Clipping.class
Fichier source
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.util.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
public class Clipping extends Applet implements MouseListener,MouseMotionListener {
private int initX = 0 ;
private int initY = 0 ;
private int px = 255 ;
private int py = 20 ;
private int dx = 0 ;
private int dy = 0 ;
public void init() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
g2.setColor(Color.red) ;
g2.fill(new Rectangle(25,40,190,110)) ;
Shape oldClip = g2.getClip() ;
g2.setClip(new Ellipse2D.Double(px+dx,py+dy,150,150)) ;
g2.fill(new Rectangle(235,40,190,110)) ;
g2.setClip(oldClip) ;
g2.setColor(Color.black) ;
g2.draw(new Ellipse2D.Double(45,20,150,150)) ;
g2.draw(new Ellipse2D.Double(px+dx,py+dy,150,150)) ;
}
public String getAppletInfo() {
return "Gestion du clipping sous Java 2D.";
}
public void mouseDragged(MouseEvent e) {
int x =e.getX();
int y =e.getY();
dx = x - initX;
dy = y - initY;
repaint();
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
initX = e.getX() ;
initY = e.getY() ;
dx = 0;
dy = 0;
}
public void mouseReleased(MouseEvent e) {
px = px+dx;
py = py+dy;
dx = 0;
dy = 0;
repaint();
}
}