public void paint(Graphics - oth

Werbung
Lösungen
1. Aufgabe
Gegeben ist ein Path-Objekt mit folgender Beschreibung des Hauses vom Nikolaus:
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Definition der Form (Verwende Even-Odd-Regel)
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(-25,50);
path.lineTo(-25.0f,0.0f); path.lineTo(0.0f,-30.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(-25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.lineTo(-25.0f,50.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.closePath();
// Schliessen
g2d.draw(path);
}
1. Implementiere die paint()-Methode im Rahmen eines Applets. Führe das Applet aus und zeige
über das Applet-Fenster) an, was vom Haus des Nikolaus aufgrund der angegenen Punkte (mit den
Abmessungen des Hauses vom Nikolaus) zu sehen ist.
2. Verschiebe das Haus des Nikolaus um (100,100).
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Definition der Form (Verwende Even-Odd-Regel)
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(-25,50);
path.lineTo(-25.0f,0.0f); path.lineTo(0.0f,-30.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(-25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.lineTo(-25.0f,50.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.closePath();
// Schliessen
// Transformation
AffineTransform at = new AffineTransform();
at.setToTranslation(100.0f,100.0f);
g2d.transform(at);
g2d.draw(path);
}
3
3. Zusätzlich zur Translation soll das Haus noch 45 Grad im Uhrzeigersinn gedreht werden
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Definition der Form (Verwende Even-Odd-Regel)
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(-25,50);
path.lineTo(-25.0f,0.0f); path.lineTo(0.0f,-30.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(-25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.lineTo(-25.0f,50.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.closePath();
// Schliessen
// Transformation
AffineTransform at = new AffineTransform();
// Setzen der affinen Transformation
at.setToTranslation(100.0f,100.0f);
// Erweitern der affinen Transformation
at.rotate(Math.PI/4);
g2d.transform(at);
g2d.draw(path);
}
4. Die Geometrie zum Haus des Nikolaus soll um den Skalenfaktor 1.5 vergrößert werden.
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Definition der Form (Verwende Even-Odd-Regel)
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(-25,50);
path.lineTo(-25.0f,0.0f); path.lineTo(0.0f,-30.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(-25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.lineTo(-25.0f,50.0f);
path.lineTo(25.0f,0.0f);
path.lineTo(25.0f,50.0f);
path.closePath();
// Schliessen
// Transformation
AffineTransform at = new AffineTransform();
// Setzen der affinen Transformation
at.setToTranslation(100.0f,100.0f);
// Erweitern der affinen Transformation
at.rotate(Math.PI/4);
// Skalieren
at.scale(1.5f,1.5f);
g2d.transform(at);
// Zeichnen
g2d.draw(path);
}
5. Welcher unerwünschte Nebeneffekt ist dabei aufgetreten?
Das Fenster zeigt: Die Skalierung in Java2D beeinflusst auch die Stroke- und Paint-Eigenschaften,
d.h. bei einer Vergrößerung wird auch die Liniendicke größer.
4
2. Aufgabe
2
Implementiere eine Applikation , die mit Hilfe von Java2D die folgenden geometrischen Formen
erzeugt.
Die dritte Figur von links zeigt eine Füllung über TexturePaint.
import
import
import
import
java.awt.*;
java.awt.event.*;
java.awt.geom.*;
java.awt.image.*;
public class PR43705 extends Frame
{
public PR43705()
{
super("Zeichnen geometrischer Formen");
setSize(425,160);
setBackground(Color.lightGray);
setVisible(true);
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(5,30,
Color.blue,
35,100,
Color.yellow,
true));
g2d.fill(new Ellipse2D.Double(5,30,65,100));
g2d.setPaint(Color.red);
g2d.setStroke(new BasicStroke(10.0f));
g2d.draw(new Rectangle2D.Double(80,30,65,100));
BufferedImage buffImage =
new BufferedImage(10,10,BufferedImage.TYPE_INT_RGB);
Graphics2D gg = buffImage.createGraphics();
gg.setColor(Color.yellow);
gg.fillRect(0,0,10,10);
gg.setColor( Color.black ); // draw in black
gg.drawRect(1,1,6,6);
// draw a rectangle
gg.setColor(Color.blue );
// draw in blue
gg.fillRect(1,1,3,3);
// draw a filled rectangle
gg.setColor(Color.red );
// draw in red
gg.fillRect(4,4,3,3);
// draw a filled rectangle
// paint buffImage onto the Frame
g2d.setPaint(
new TexturePaint(
buffImage, new Rectangle(10,10)));
2
pr43700
5
g2d.fill(
new RoundRectangle2D.Double(
155, 30, 75, 100, 50, 50 ));
// draw 2D pie-shaped arc in white
g2d.setPaint( Color.white );
g2d.setStroke( new BasicStroke( 6.0f ) );
g2d.draw(
new Arc2D.Double(
240, 30, 75, 100, 0, 270, Arc2D.PIE));
// draw 2D lines in green and yellow
g2d.setPaint( Color.green );
g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
float dashes[] = { 10 };
g2d.setPaint( Color.yellow );
g2d.setStroke(
new BasicStroke(4,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
10, dashes, 0 ) );
g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
}
public static void main(String args[])
{
PR43705 app = new PR43705();
app.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
} );
}
}
6
3. Aufgabe
3
Implementiere eine Applikation , die mit Java2D den folgenden Sternenkranz erzeugt.
// Demonstrating a general path
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
public class PR43706 extends Frame
{
public PR43706()
{
super( "Zeichnen Sternenkranz" );
setBackground( Color.yellow );
setSize( 400, 400 );
setVisible(true);
}
public void paint( Graphics g )
{
int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Graphics2D g2d = ( Graphics2D ) g;
// create a star from a series of points
GeneralPath star = new GeneralPath();
// set the initial coordinate of the General Path
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
// create the star--this does not draw the star
for ( int k = 1; k < xPoints.length; k++ )
star.lineTo( xPoints[ k ], yPoints[ k ] );
// close the shape
star.closePath();
3
pr43700
7
// translate the origin to (200, 200)
g2d.translate( 200, 200 );
// rotate around origin and draw stars in random colors
for ( int j = 1; j <= 20; j++ )
{
g2d.rotate( Math.PI / 10.0 );
g2d.setColor(
new Color( ( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ) ) );
g2d.fill( star ); // draw a filled star
}
}
public static void main( String args[] )
{
PR43706 app = new PR43706();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
});
}
}
4. Aufgabe: Animation mit Java2D
4
1. Zeichne mit Hilfe eines Applets an den Koordinatenursprung ein rotes Rechteck (200 Pixel breit,
100 Pixel hoch), das nach rechts unten aus dem Fenster verschwindet.
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
public class PR43700 extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Rectangle2D.Float rechteck = new Rectangle2D.Float(
0.0f,0.0f,200.0f,100.0f);
Stroke stroke = new BasicStroke(5.0f);
AffineTransform at = new AffineTransform();
g2d.setStroke(stroke);
for (int i = 0;i < 100; i++)
{
at.translate(10.0f,10.0f);
g2d.setTransform(at); g2d.setPaint(Color.black); g2d.draw(rechteck);
g2d.setPaint(Color.red); g2d.fill(rechteck);
try { Thread.sleep(2000); } catch(Exception e) { }
g2d.setPaint(Color.white); g2d.draw(rechteck); g2d.fill(rechteck);
}
}
}
4
pr43700
8
2. Verbessere auftretendes Flimmern bei der Animation durch "Double Buffering".
import java.awt.*;
import java.applet.*;
import java.awt.geom.*;
public class PR43701 extends java.applet.Applet
{
private Image memImage = null;
public void createBuffer()
{
Dimension d = getSize();
if ((memImage == null) || (memImage.getWidth(null) != d.width)
|| (memImage.getHeight(null) != d.height))
memImage = createImage(d.width,d.height);
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
Rectangle2D.Float rechteck = new Rectangle2D.Float(
0.0f,0.0f,200.0f,100.0f);
Stroke stroke = new BasicStroke(5.0f);
AffineTransform at = new AffineTransform();
createBuffer();
Graphics2D g2m = (Graphics2D) memImage.getGraphics();
for (int i = 0;i < 100; i++)
{
g2m.setColor(getBackground());
// g2m.setStroke(stroke);
g2m.fillRect(0,0,getSize().width,getSize().height);
at.translate(2.0f,2.0f); g2m.setTransform(at);
g2m.setPaint(Color.black); g2m.draw(rechteck);
g2m.setPaint(Color.red); g2m.fill(rechteck);
g2d.drawImage(memImage,0,0,null);
try { Thread.sleep(2000); } catch(Exception e) { }
}
}
}
9
Herunterladen