Hallo alle zusammen,
ich habe eine Uhr Programmiert die die Zeit als Binären Code anzeigt, dass aber Grafisch.
Das Programm läuft auch, mein Problem ist aber, dass ich am liebsten die Dreiecke die ich einzeln Zeichnen lasse
so einfach wie möglichst vom Code her gezeichnet werden.
Danke für eure Ideen und eure Hilfe.
ich habe eine Uhr Programmiert die die Zeit als Binären Code anzeigt, dass aber Grafisch.
Das Programm läuft auch, mein Problem ist aber, dass ich am liebsten die Dreiecke die ich einzeln Zeichnen lasse
so einfach wie möglichst vom Code her gezeichnet werden.
Danke für eure Ideen und eure Hilfe.
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class BinaerUhr extends Frame {
// Anfang Variablen
private Label label1 = new Label();
private Panel panel1 = new Panel(null);
private Label label2 = new Label();
private Zeit zt = new Zeit();
// Ende Variablen
public BinaerUhr(String title) {
// Frame-Initialisierung
super(title);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) { System.exit(0); }
});
int frameWidth = 341;
int frameHeight = 468;
setSize(frameWidth, frameHeight);
Panel cp = new Panel(null);
cp.setBackground(Color.gray);
add(cp);
// Anfang Komponenten
label1.setBounds(96, 24, 123, 24);
label1.setText("Binary Watch");
label1.setFont (new Font("MS Sans Serif", Font.BOLD, 17));
cp.add(label1);
panel1.setBounds(16, 56, 300, 280);
panel1.setBackground(Color.lightGray);
cp.add(panel1);
label2.setBounds(16, 336, 301, 29);
label2.setBackground(Color.lightGray);
label2.setAlignment(1);
label2.setText(" ");
label2.setFont (new Font("MS Sans Serif", Font.BOLD, 21));
label2.setVisible(false);
cp.add(label2);
// Ende Komponenten
setResizable(false);
setVisible(true);
doit(); // starten
}
// Anfang Ereignisprozeduren
private void DreieckeZeichnen() {
Graphics g = panel1.getGraphics();
int[] x1Coord = {100, 140, 60, 100}; // x-Koordinaten
int[] y1Coord = { 10, 90, 90, 10}; // y-Koordinaten
int anz = x1Coord.length;
g.setColor(Color.red);
g.fillPolygon(x1Coord,y1Coord,anz);
int[] x2Coord = {110, 190, 150, 110}; // x-Koordinaten
int[] y2Coord = { 10, 10, 90, 10}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x2Coord,y2Coord,anz );
int[] x3Coord = {200, 240, 160, 200}; // x-Koordinaten
int[] y3Coord = { 10, 90, 90, 10}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x3Coord,y3Coord,anz);
int[] x4Coord = { 50, 90, 10, 50}; // x-Koordinaten
int[] y4Coord = {100, 180, 180, 100}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x4Coord,y4Coord,anz );
int[] x5Coord = { 60, 140, 100, 60}; // x-Koordinaten
int[] y5Coord = {100, 100, 180, 100}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x5Coord,y5Coord,anz );
int[] x6Coord = {150, 190, 110, 150}; // x-Koordinaten
int[] y6Coord = {100, 180, 180, 100}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x6Coord,y6Coord,anz);
int[] x7Coord = {160, 240, 200, 160}; // x-Koordinaten
int[] y7Coord = {100, 100, 180, 100}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x7Coord,y7Coord,anz);
int[] x8Coord = {250, 290, 210, 250}; // x-Koordinaten
int[] y8Coord = {100, 180, 180, 100}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x8Coord,y8Coord,anz);
int[] x9Coord = { 10, 90, 50, 10}; // x-Koordinaten
int[] y9Coord = {190, 190, 270, 190}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x9Coord,y9Coord,anz);
int[] x10Coord = {100, 140, 60, 100}; // x-Koordinaten
int[] y10Coord = {190, 270, 270, 190}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x10Coord,y10Coord,anz);
int[] x11Coord = {110, 190, 150, 110}; // x-Koordinaten
int[] y11Coord = {190, 190, 270, 190}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x11Coord,y11Coord,anz);
int[] x12Coord = {200, 240, 160, 200}; // x-Koordinaten
int[] y12Coord = {190, 270, 270, 190}; // y-Koordinaten
g.setColor(Color.red);
g.fillPolygon(x12Coord,y12Coord,anz);
int[] x13Coord = {210, 290, 250, 210}; // x-Koordinaten
int[] y13Coord = {190, 190, 270, 190}; // y-Koordinaten
g.setColor(Color.blue);
g.fillPolygon(x13Coord,y13Coord,anz);
} // end of dreieckeZeichnen
private void sekBalken(int s, int c) {
Graphics g = panel1.getGraphics();
int[][] xsCoord = {{291,299,259,251},{201,209,249,241},{191,199,159,151},
{101,109,149,141},{ 91, 99, 59, 51},{ 1, 9, 49, 41}};
int[] ysCoord = {190,190,270,270};
int anz = ysCoord.length;
if (c == 0) {
g.setColor(Color.black);
}
else {
g.setColor(Color.lightGray);
}
g.fillPolygon(xsCoord[s],ysCoord,anz);
} // end of sekBalken
private void minBalken(int m, int c) {
Graphics g = panel1.getGraphics();
int[][] xmCoord = {{251,259,299,291},{241,249,209,201},{151,159,199,191},
{141,149,109,101},{ 51, 59, 99, 91},{ 41, 49, 9, 1}};
int[] ymCoord = {100,100,180,180};
int anz = ymCoord.length;
if (c == 0) {
g.setColor(Color.black);
}
else {
g.setColor(Color.lightGray);
}
g.fillPolygon(xmCoord[m],ymCoord,anz);
} // end of minBalken
private void stdBalken(int h, int c) {
Graphics g = panel1.getGraphics();
int[][] xhCoord = {{201,209,249,241},{191,199,159,151},{101,109,149,141},
{ 91, 99, 59, 51}};
int[] yhCoord = {10,10,90,90};
int anz = yhCoord.length;
if (c == 0) {
g.setColor(Color.black);
}
else {
g.setColor(Color.lightGray);
}
g.fillPolygon(xhCoord[h],yhCoord,anz);
} // end of stdBalken
private static boolean getBit(int worin, int bitnr) {
boolean gesetzt = false;
if ((worin & (1 << bitnr)) != 0) {
gesetzt = true;
}
return gesetzt;
} // end of betBit
private void BalkenZeichnen(char hms, int x) {
switch (hms) {
case 'h' : int std = x;
for (int i = 0; i <= 3 ; i++ ) { // Balken für Stunden
if (getBit(std,i) == true) {
stdBalken(i,0);
}
else {
stdBalken(i,1);
}
}
break;
case 'm' : int min = x;
for (int i = 0; i <= 5 ; i++ ) { // Balken für Minuten
if (getBit(min,i) == true) {
minBalken(i,0);
}
else {
minBalken(i,1);
}
}
break;
case 's' : int sek = x;
for (int i = 0; i <= 5 ; i++ ) { // Balken für Sekunden
if (getBit(sek,i) == true) {
sekBalken(i,0);
}
else {
sekBalken(i,1);
}
}
break;
}
} // end of BalkenZeichnen
private void AmPm (int apm) {
if (apm == 0) {
label2.setText("AM");
} else {
label2.setText("PM");
}
} // end of AmPm
public class Zeit extends Thread {
public void run() {
try {
while (!Zeit.interrupted()) {
Calendar kalender = new GregorianCalendar();
BalkenZeichnen('s',kalender.get(Calendar.SECOND));
BalkenZeichnen('m',kalender.get(Calendar.MINUTE));
BalkenZeichnen('h',kalender.get(Calendar.HOUR));
AmPm(kalender.get(Calendar.AM_PM));
Thread.sleep(1);
}
}
catch (InterruptedException ie) {}
}
} //end of Zeit-Thread
private void doit(){
Graphics g = panel1.getGraphics();
label2.setVisible(true);
DreieckeZeichnen();
if (!zt.isAlive()) {
zt = new Zeit();
zt.start();
}
}
// Ende Ereignisprozeduren
public static void main(String[] args) {
new BinaerUhr("BinaerUhr");
}
} // end of BinaerUhr