15. Übung - oth

Werbung
Programmieren in Java
15. Übung
Lösungen
1. Aufgabe
import java.awt.*;
import java.awt.event.*;
class Ueb152 extends Frame
{
final static double MAXX = 5.0;
final static double MAXY = 1;
Ueb152 ()
{
super ("Funktions-Plotter");
setBounds (40,40,640,480);
addWindowListener (new WindowAdapter()
{
public void windowClosing (WindowEvent we)
{
System.exit(0);
}
});
}
public double function (double x)
{
return Math.sin(x) * Math.cos(x);
// return Math.sin(x)*Math.sin(x)*Math.sin(x);
// return Math.tan(x)/Math.pow(Math.sin(x),3);
}
public double screenToWorldX(int x)
{
double midX = getSize().width/2;
return ((x-midX)/midX)*MAXX;
}
public int worldToScreenX (double x)
{
double midX = getSize().width/2;
return (int)((x/MAXX)*midX+midX);
}
public int worldToScreenY (double y)
{
double midY = getSize().height/2-30;
return getSize().height - (int)((y/MAXY) * midY + midY)-20;
}
public void paint (Graphics g)
{
// horizontale Linie
g.drawLine (
0,
worldToScreenY(0),
getSize().width,
worldToScreenY(0)
);
1
Programmieren in Java
// Verticale Linie
g.drawLine (
worldToScreenX(0),
worldToScreenY(MAXY),
worldToScreenX(0),
worldToScreenY(-MAXY)
);
// Unterteilung der horizontalen Linie in 1er Schritte
for (int i=(int)-MAXX;i<=(int)MAXX;i++)
{
g.drawLine (
worldToScreenX(i),
worldToScreenY(0)-5,
worldToScreenX(i),
worldToScreenY(0)+5
);
}
// Unterteilung der verticalen Linie in 1er Schritte
for
(int i=(int)-MAXY;i<=(int)MAXY;i++) {
g.drawLine (
worldToScreenX(0)-5,
worldToScreenY(i),
worldToScreenX(0)+5,
worldToScreenY(i)
);
}
// Zeichnen der Funktion
for (int i=10;i<getSize().width-10;i++) {
double x1 = screenToWorldX(i);
double x2 = screenToWorldX(i+1);
double y1 = function (x1);
double y2 = function (x2);
g.drawLine (
i,
worldToScreenY(y1),
i+1,
worldToScreenY(y2)
);
}
}
public static void main (String args[])
{
Ueb152 test = new Ueb152();
test.show();
}
}
2. Aufgabe
import java.awt.*;
import java.util.*;
public class BetrSeemann extends java.applet.Applet
implements Runnable
{
Thread faden;
int xMax, yMax, xPos, yPos;
Random rand = new Random();
Color farbe;
// Eigene Methoden
int zRand(int mod)
{
2
Programmieren in Java
return Math.abs(rand.nextInt() % mod);
}
// Ueberschriebene Methoden
public void init()
{
setBackground(Color.lightGray);
xMax = size().width;
yMax = size().height;
xPos = xMax / 2;
yPos = yMax / 2;
}
public void start()
{
if (faden == null)
{
faden = new Thread(this);
faden.start();
}
}
public void run()
{
for (int i = 1; i <= 16; i++)
{
farbe = new Color(255 / i, 16 * i - 1, (16 * i) % 255);
repaint();
try { Thread.sleep(1000); }
catch(InterruptedException e) { }
xPos = xMax / 2;
yPos = yMax / 2;
}
}
public void update(Graphics g)
{
g.setColor(farbe);
do
{
int winkel = zRand(359);
g.drawLine(xPos,yPos,
(int) (xPos+25*Math.cos(winkel*Math.PI/180.0)),
(int) (yPos+25*Math.sin(winkel*Math.PI/180.0)));
xPos = (int) (xPos+25*Math.cos(winkel*Math.PI/180.0));
yPos = (int) (yPos+25*Math.sin(winkel*Math.PI/180.0));
} while ((xPos < xMax) && (xPos > 0) &&
(yPos < yMax) && (yPos > 0));
}
public void paint(Graphics g)
{
g.setColor(farbe);
}
}
3
Herunterladen