This is an old revision of the document!
A traffic light in Java, have fun.
import java.awt.*;
public class Ampel {
// Anfang Attribute
private boolean rot = true;
private boolean gelb = false;
private boolean gruen = false;
private int x;
private int y;
// Ende Attribute
public Ampel (int x,int y)
{
this.x = x;
this.y = y;
}
public void rot(){
rot = true;
gelb = false;
gruen = false;
}
public void rotgelb(){
rot = true;
gelb = true;
gruen = false;
}
public void gruen(){
rot = false;
gelb = false;
gruen = true;
}
public void gelb(){
rot = false;
gelb = true;
gruen = false;
}
// Anfang Methoden
public void paint(Graphics g)
{
((Graphics2D)g).setRenderingHint (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(10,30,245,730);
g.setColor(Color.BLACK);
g.fillRect(x,y, 200,400);
if (rot) {
g.setColor(Color.RED);
g.fillOval(x + 55, y + 30,100,100);
}
else {
g.setColor(Color.DARK_GRAY);
g.fillOval(x + 55, y + 30,100,100);
}
if (gelb) {
g.setColor(Color.YELLOW);
g.fillOval(x + 55,y + 150,100,100);
}
else {
g.setColor(Color.DARK_GRAY);
g.fillOval(x + 55,y + 150,100,100);
}
if (gruen) {
g.setColor(Color.GREEN);
g.fillOval(x + 55,y + 270,100,100);
}
else {
g.setColor(Color.DARK_GRAY);
g.fillOval(x + 55,y + 270,100,100);
}
}
public static void main (String[] args) throws InterruptedException{
}
// Ende Methoden
}
Discussion