Téléchargement
et affichage d'images
sous java 2D
Images.class
Fichier source
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class Images extends Applet implements MouseListener,MouseMotionListener {
private int initX = 0 ;
private int initY = 0 ;
private int px = 100 ;
private int py = 20 ;
private int dx = 0 ;
private int dy = 0 ;
private boolean f = true ;
private Image f1 = null ;
private Image f2 = null ;
private Image m1 = null ;
private Image m2 = null ;
public void init() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
URL url = this.getDocumentBase();
try {
String adresse = "http://"+url.getHost()+"/IG/Java2D/Images/Bois.jpg" ;
URL u1 = new URL(adresse);
f1 = getImage(u1) ; }
catch (MalformedURLException e) { }
try {
String adresse = "http://"+url.getHost()+"/IG/Java2D/Images/Parquet.gif" ;
URL u1 = new URL(adresse);
f2 = getImage(u1) ; }
catch (MalformedURLException e) { }
try {
String adresse = "http://"+url.getHost()+"/IG/Java2D/Images/Brick.gif" ;
URL u1 = new URL(adresse);
m1 = getImage(u1) ; }
catch (MalformedURLException e) { }
try {
String adresse = "http://"+url.getHost()+"/IG/Java2D/Images/Checker.gif" ;
URL u1 = new URL(adresse);
m2 = getImage(u1) ; }
catch (MalformedURLException e) { }
}
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
g2.drawImage(( f ) ? f1 : f2,0,0,this);
g2.drawImage(( f ) ? m1 : m2,px+dx,py+dy,this);
}
public String getAppletInfo() {
return "Téléchargement et affichage d'images 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) {
if ( (e.getModifiers()&MouseEvent.BUTTON1_MASK) != 0 ) {
px = px+dx;
py = py+dy;
dx = 0;
dy = 0; }
else {
f = !f ; }
repaint();
}
public void update(Graphics g) {
setBackground(Color.white);
paint(g) ;
}
}