Skip to content
Snippets Groups Projects
Commit f2fd4f14 authored by Dennis Eisold's avatar Dennis Eisold
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #128 failed with stages
in 1 minute and 40 seconds
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
package de.itstall;
import javax.swing.*;
import java.awt.*;
public class AmpelGui extends JFrame {
JButton btnWeiter = new JButton("Weiter");
AmpelPanel panel = new AmpelPanel();
public AmpelGui() {
initializeComponents();
}
public void anzeigen() {
this.setVisible(true);
}
private void initializeComponents() {
// Eigenschaften des Frames setzen
Dimension dim = new Dimension(150, 450);
this.setLayout(new BorderLayout());
this.setTitle("Ampelschaltung");
this.setSize(400, 700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
this.setVisible(true);
this.setMinimumSize(dim);
// Neue ZeichenFlaeche erzeugen und dies auf der Oberfläche platzieren
panel.setBackground(Color.GRAY); // Ändern der Hintergrundfarbe
this.add(panel, BorderLayout.CENTER);
btnWeiter.addActionListener(ae -> weiterSchalten());
this.add(btnWeiter, BorderLayout.PAGE_END);
this.pack();
}
private void weiterSchalten() {
if (panel.ampelPhase == 3) {
panel.ampelPhase = 0;
} else {
panel.ampelPhase++;
}
}
}
package de.itstall;
import javax.swing.*;
import java.awt.*;
public class AmpelPanel extends JPanel {
public int ampelPhase;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int ampelWidth = getWidth() - 40;
/** Rahmen der Lampen zeichnen */
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
BasicStroke myStroke = new BasicStroke(4.0f);
g2d.setStroke(myStroke);
g2d.drawOval(18, 18, ampelWidth + 4, ampelWidth + 4);
g2d.drawOval(18, ampelWidth + 48, ampelWidth + 4, ampelWidth + 4);
g2d.drawOval(18, ampelWidth * 2 + 68, ampelWidth + 4, ampelWidth + 4);
switch (ampelPhase) {
case 0:
g.setColor(Color.RED);
g.fillOval(20, 20, ampelWidth, ampelWidth);
break;
case 1:
g.setColor(Color.RED);
g.fillOval(20, 20, ampelWidth, ampelWidth);
g.setColor(Color.YELLOW);
g.fillOval(20, ampelWidth + 50, ampelWidth, ampelWidth);
break;
case 2:
g.setColor(Color.GREEN);
g.fillOval(20, ampelWidth * 2 + 70, ampelWidth, ampelWidth);
break;
case 3:
g.setColor(Color.YELLOW);
g.fillOval(20, ampelWidth + 50, ampelWidth, ampelWidth);
break;
}
repaint();
}
}
package de.itstall;
public class Main {
public static void main(String[] args) {
new AmpelGui().anzeigen();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment