Illustration des
principales
fonctionnalités de Java 2D
Java2DExemple001.class
Fichiers source
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.* ;
public class Java2DExemple001 extends Applet
implements Runnable {
public Java2DExemple001Canvas canvas;
static public Image i = null ;
static public Image a[] = new Image[36] ;
private Thread app = null ;
private boolean bRun = false ;
public void init() {
URL u ;
try {
u = new URL("http://raphaello.univ-fcomte.fr/IG/Java2D/Images/r1.gif");
i = getImage(u) ; }
catch (MalformedURLException e) { }
for ( int ind = 0 ; ind < 36 ; ind++ ) {
try {
u = new URL("http://raphaello.univ-fcomte.fr/IG/Java2D/Images/Animation/Image"+ind+".gif");
a[ind] = getImage(u) ; }
catch (MalformedURLException e) { } }
setBackground(Color.white);
setLayout(new BorderLayout());
canvas = new Java2DExemple001Canvas();
add("Center",canvas);
}
public void initApplication() {
i = Toolkit.getDefaultToolkit().getImage("r1.gif");
for ( int ind = 0 ; ind < 36 ; ind++ ) {
a[ind] = Toolkit.getDefaultToolkit().
getImage("Animation/Image"+ind+".gif"); }
setLayout(new BorderLayout());
canvas = new Java2DExemple001Canvas();
add("Center",canvas);
}
public void destroy() {
remove(canvas);
}
public void start() {
bRun = true ;
if ( app == null ) {
app = new Thread(this);
app.start(); }
}
public void stop() {
if ( app != null ) {
bRun = false;
app = null; }
}
public void run() {
while ( bRun ) {
try {
canvas.repaint() ;
Thread.sleep(100) ; }
catch(Exception e) { } ;
}
}
public void processEvent(AWTEvent e) {
if (e.getID() == Event.WINDOW_DESTROY) {
System.exit(0);
}
}
public static void main(String args[]) {
Frame f = new Frame("Exemple 001 pour Java 2D");
Java2DExemple001 j001 = new Java2DExemple001();
j001.initApplication();
j001.start();
f.add("Center",j001);
f.setSize(500,400);
f.show();
}
public String getAppletInfo() {
return "Exemple 001 de Java 2D.";
}
}
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Novembre 2001 */
import java.awt.*;
import java.awt.font.*;
public class Java2DExemple001Canvas extends Canvas {
public int im = 0 ;
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
g2.setColor(new Color(0,0,255));
g2.drawString("Chaîne de caractères simple",50,30);
FontRenderContext frc = g2.getFontRenderContext();
Font f = new Font("Comic Sans MS",Font.BOLD,22);
String s = new String("Font : 22 Comic Sans MS Bold");
TextLayout tl = new TextLayout(s,f,frc);
g2.setColor(new Color(0,255,255));
tl.draw(g2,20,80);
g2.setColor(new Color(255,0,0));
g2.draw3DRect(100,100,200,60,true);
BasicStroke bs1 = new BasicStroke(8,
BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND);
g2.setStroke(bs1);
g2.setColor(new Color(200,200,100));
Polygon p1 = new Polygon();
p1.addPoint(20,200);
p1.addPoint(90,250);
p1.addPoint(150,220);
p1.addPoint(180,230);
p1.addPoint(220,180);
g2.drawPolygon(p1);
BasicStroke bs2 = new BasicStroke(1,
BasicStroke.CAP_SQUARE,BasicStroke.JOIN_ROUND);
g2.setStroke(bs2);
g2.setColor(new Color(0,0,0));
GradientPaint gp = new GradientPaint(100,310,
new Color(30,90,150),
200,320,
new Color(230,170,70),
true);
g2.setPaint(gp);
Polygon p2 = new Polygon();
p2.addPoint(10,300);
p2.addPoint(100,350);
p2.addPoint(130,320);
p2.addPoint(190,330);
p2.addPoint(210,280);
g2.fillPolygon(p2);
if ( Java2DExemple001.i != null )
g2.drawImage(Java2DExemple001.i,370,10,this) ;
if ( Java2DExemple001.a[im] != null ) {
g2.drawImage(Java2DExemple001.a[im],280,180,this) ;
im++ ;
if ( im == 36 )
im = 0 ; }
}
public void update(Graphics g) {
setBackground(Color.white);
paint(g) ;
}
}