Sunteți pe pagina 1din 11

Interfaces Graphiques

Tracer une fonction y = f(x)


avec JAVA 2D

Sofia ZAIDENBERG

CNRS

Mai 2007

Java 2D

Afficher la courbe dune fonction dans un fentre (un JPanel ou un JComponent)


y
x

y
F(x) = sin(x)

Espace cran :
( Device Coordinate System )

Espace utilisateur
( World Coordinate System )

Entier
Born

Rel
Non born

Sofia ZAIDENBERG

CNRS

Mai 2007

Java 2D

Afficher la courbe dune fonction dans une fentre (un JPanel ou un JComponent)
y
x

y
F(x) = sin(x)

Espace cran :
( Device Coordinate System )

Espace utilisateur
( World Coordinate System )
1

Dfinir la rgion de lespace


utilisateur afficher

Appliquer au coordonnes
exprimes dans WCS une
transformation vers DCS

Rel
Non born

Sofia ZAIDENBERG

CNRS

Entier
Born

Mai 2007

Java 2D

Dfinir la rgion de l'espace utilisateur afficher


y
x

xWmax
yWmax

.
xWmin
yWmin

y
DCS

WCS
Dfinition dune fentre dans lespace utilisateur :
XWmin, YWmin coordonnes dans WCS du coin infrieur gauche de la fentre
XWmax,YWmax coordonnes dans WCS du coin suprieur droit de la fentre

Sofia ZAIDENBERG

CNRS

Mai 2007

Java 2D

Transformation coordonnes WCS coordonnes DCS


y
x

xWmax
yWmax

xw
yw

xd
yd

hd

.
xWmin
yWmin

y
xd
yd

WCS

ld

= T xw
yw

DCS

T dpend de
xWin, yWmin, xWmax, yWmax
et de
ld (largeur) et hd (hauteur)
de la rgion daffichage

Sofia ZAIDENBERG

CNRS

Mai 2007

Java 2D

Transformation coordonnes WCS coordonnes DCS


y

xWmax
yWmax

xw
yw

.
x

xd
yd

hd

xWmin
yWmin

y
xd
yd

WCS

xWmin
yWmin

0
hd

xWmax
yWmax

ld
0

Sofia ZAIDENBERG

ld

= T xw
yw

CNRS

Mai 2007

DCS

Java 2D

Transformation coordonnes WCS coordonnes DCS


y
x

xWmax
yWmax

xw
yw

xd
yd

hd

.
xWmin
yWmin

y
xd
yd

WCS

Les proportions doivent tre conserves :


La position relative de (xd,yd) par rapport
la rgion daffichage doit tre la mme
que la position relative de (xw,xw) par
rapport la fentre utilisateur

xd
ld
yd
hd

ld

= T xw
yw

xw - xWmin

DCS

lw =(xWmax xWmin)

lw
yWmax - yw
=

Sofia ZAIDENBERG

hw =(yWmax yWmin)

hw

CNRS

Mai 2007

Java 2D

Transformation coordonnes WCS coordonnes DCS


y
x

xWmax
yWmax

xw
yw

xd
yd

hd

.
xWmin
yWmin

y
xd
yd

WCS

ld

= T xw
yw

DCS

T
xd
ld
yd
hd

xw - xWmin
lw
yWmax - yw

hw

ld
xd = xw *

lw

- xWmin *

ld
lw

hd
yd = - yw *

hw

ld
xd

+ yWmax *

homothtie

hd

yd

ld
0

lw

=
0

hd
-

hw
1

translation

Sofia ZAIDENBERG

CNRS

0
Mai 2007

hw
0

- xWmin *
yWmax *

lw
hd

xw

yw

hw
1

1
8

Java 2D

Transformation coordonnes WCS coordonnes DCS


y

xWmax
yWmax

xw
yw

xd
yd

hd

xWmin
yWmin

ld

DCS

WCS
ld
0
hd
1

lw

=
0
0

ld

ld
hd
-

hw
0

- xWmin *
yWmax *

lw
hd

xWmin

ld

yWmin

lw

=
0

hw

1
Sofia ZAIDENBERG

1
CNRS

ld
0

hd
-

hw
0

Mai 2007

- xWmin *
yWmax *

xWmax

lw
hd

yWmax

hw
1

Java 2D

Transformation WCS DCS : prise en charge par Java2D


T

public void paintComponent(Graphics g) {

ld
xd
yd

ld
0

lw

0
0

hd

hw
0

- xWmin *
yWmax *

lw
hd

xw

super.paintComponent(g);

yw

Graphics2D g2 = (Graphics2D) g;

int ld = this.getWidth();
int hd = this.getEight();
double lw = xWmax - xWmin;
...
double m00 = ld / lw;
...
AffineTransform t = new AffineTransform(
m00,0.0,0.0,
m11,m02,m12);

hw

T dfinie par un objet java.awt.AffineTransform


[ x'] = [ m00
[ y'] = [ m10
[ 1 ] = [ 0

m01
m11
0

m02 ] [ x ] = [ m00x + m01y + m02 ]


m12 ] [ y ] = [ m10x + m11y + m12 ]
1 ] [ 1 ] = [
1
]

Cette transformation est combine la


transformation courante maintenue par
le contexte graphique
Les coordonnes des primitives graphiques
( Shape ) sont ensuite exprimes dans WCS
et seront automatiquement transforme
lors du rendu.
}
Sofia ZAIDENBERG

CNRS

g2.transform(t);
...
g2.setStroke(new BasicStroke(0.0f));
...
g2.draw(aShape);

Mai 2007

10

Java 2D
public void paintComponent(Graphics g) {
super.paintComponent(g);

chantillonnage de la courbe

La courbe va tre approxime


par des segments de droite.

Graphics2D g2 = (Graphics2D) g;
int ld = this.getWidth();
int hd = this.getWidth();
double lw = xWmax - xWmin;
...
double m00 = ld / lw;
...
AffineTransform t = new AffineTransform(
m00,0.0,0.0,
m11,m02,m12);

Prendre garde de ne pas tracer


des primitives gomtriques
trop petites ( < 1 pixel)

g2.transform(t);

Le pas dchantillonnage doit


tre fix en fonction des
dimension de lespace
daffichage

g2.setStroke(new BasicStroke(0.0f));
double pasX = lw / ld * 3.0;
for (double x = xWmin; x <= xWmax; x += pasX) {
tracer segment (x,f(x)) (x + pasX, f(x + pasX);
}
}

Sofia ZAIDENBERG

CNRS

Mai 2007

11

S-ar putea să vă placă și