11
© Sofia ZAIDENBERG CNRS Mai 2007 1 Interfaces Graphiques Tracer une fonction y = f(x) avec JAVA 2D

Interfaces Graphiques

  • Upload
    emma

  • View
    34

  • Download
    1

Embed Size (px)

DESCRIPTION

Interfaces Graphiques. Tracer une fonction y = f(x) avec JAVA 2D. x. y. y. Afficher la courbe d’une fonction dans un fenêtre (un JPanel ou un JComponent ). x. F(x) = sin(x). Espace écran : (« Device Coordinate System »). Espace utilisateur (« World Coordinate System »). Entier - PowerPoint PPT Presentation

Citation preview

Page 1: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 1

Interfaces GraphiquesInterfaces Graphiques

Tracer une fonction y = f(x)

avec JAVA 2D

Page 2: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 2

Java 2DJava 2D

Afficher la courbe d’une fonction dans un fenêtre (un JPanel ou un JComponent)y

x

Espace utilisateur (« World Coordinate System »)

Espace écran :(« Device Coordinate System »)

F(x) = sin(x)

x

y

RéelNon borné

EntierBorné

Page 3: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 3

Java 2DJava 2D

Afficher la courbe d’une fonction dans une fenêtre (un JPanel ou un JComponent)y

x

x

y

1Définir la région de l’espace utilisateur à afficher

2Appliquer au coordonnées exprimées dans WCS une transformation vers DCS

Espace utilisateur (« World Coordinate System »)

F(x) = sin(x)

RéelNon borné

Espace écran :(« Device Coordinate System »)

EntierBorné

Page 4: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 4

Java 2DJava 2D

Définir la région de l'espace utilisateur à affichery

x

x

y

xWminyWmin

xWmaxyWmax

.

.

Définition d’une fenêtre dans l’espace utilisateur :XWmin, YWmin coordonnées dans WCS du coin inférieur gauche de la fenêtreXWmax,YWmax coordonnées dans WCS du coin supérieur droit de la fenêtre

WCS DCS

Page 5: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 5

Java 2DJava 2D

Transformation coordonnées WCS coordonnées DCSy

x

x

y

xWminyWmin

xWmaxyWmax

.

.

. .xwyw

xdyd

xdyd

xwyw

= T

T dépend dexWin, yWmin, xWmax, yWmax

et de ld (largeur) et hd (hauteur) de la région d’affichage

ld

hd

WCS DCS

Page 6: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 6

Java 2DJava 2D

Transformation coordonnées WCS coordonnées DCSy

x

x

y

xWminyWmin

xWmaxyWmax

.

.

. .xwyw

xdyd

xdyd

xwyw

= Tld

hd

WCS DCS

.

.

xWminyWmin

0hd

T

xWmaxyWmax

ld0

T

Page 7: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 7

Java 2DJava 2D

Transformation coordonnées WCS coordonnées DCSy

x

x

y

xWminyWmin

xWmaxyWmax

.

.

. .xwyw

xdyd

xdyd

xwyw

= Tld

hd

WCS DCS

xw - xWmin

lw

xd

ld=

Les proportions doivent être conservées :La position relative de (xd,yd) par rapportà la région d’affichage doit être la mêmeque la position relative de (xw,xw) par

rapport à la fenêtre utilisateur yd

hd

yWmax - yw

hw= hw =(yWmax – yWmin)

lw =(xWmax – xWmin)

Page 8: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 8

Java 2DJava 2D

Transformation coordonnées WCS coordonnées DCSy

x

x

y

xWminyWmin

xWmaxyWmax

.

.

. .xwyw

xdyd

xdyd

xwyw

= Tld

hd

WCS DCS

xd

ld

xw - xWmin

lw=

yd

hd

yWmax - yw

hw=

xd = xw * - xWmin *ld

lw

ld

lw

hd

hwyd = - yw * + yWmax *

hd

hw

homothétie translation

ld

lw

hd

hw-

0

0

10 0

ld

lw- xWmin *

hd

hwyWmax *

xw

yw

1

xd

yd

1

= *

T

Page 9: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 9

Java 2DJava 2D

Transformation coordonnées WCS coordonnées DCSy

x

x

y

xWminyWmin

xWmaxyWmax

.

.

. .xwyw

xdyd

ld

hd

WCS DCS

.

.

ld

lw

hd

hw-

0

0

10 0

ld

lw- xWmin *

hd

hwyWmax *

xWmin

yWmin

1

0

hd

1

= *

ld

lw

hd

hw-

0

0

10 0

ld

lw- xWmin *

hd

hwyWmax *

ld

0

1

=

xWmax

yWmax

1

*

Page 10: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 10

Java 2DJava 2D

Transformation WCS DCS : prise en charge par Java2D public void paintComponent(Graphics g) {

super.paintComponent(g);

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);

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

}

ld

lw

hd

hw-

0

0

10 0

ld

lw- xWmin *

hd

hwyWmax *

xw

yw

1

xd

yd

1

= *

T

[ x'] = [ m00 m01 m02 ] [ x ] = [ m00x + m01y + m02 ][ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ][ 1 ] = [ 0 0 1 ] [ 1 ] = [ 1 ]

T définie par un objet java.awt.AffineTransform

Cette transformation est combinée à latransformation courante maintenue par le contexte graphique

Les coordonnées des primitives graphiques (« Shape ») sont ensuite exprimées dans WCSet seront automatiquement transforméelors du rendu.

Page 11: Interfaces Graphiques

© Sofia ZAIDENBERG CNRS Mai 2007 11

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

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);

g2.transform(t);

g2.setStroke(new BasicStroke(0.0f));

}

Échantillonnage de la courbe

for (double x = xWmin; x <= xWmax; x += pasX) { tracer segment (x,f(x)) (x + pasX, f(x + pasX); }

La courbe va être approximée par des segments de droite.

Prendre garde de ne pas tracerdes primitives géométriques trop petites ( < 1 pixel)

Le pas d’échantillonnage doitêtre fixé en fonction desdimension de l’espace d’affichage double pasX = lw / ld * 3.0;