I've created an analogclock were u can type a digital time and the program draws a clock that shows the time and updates it self secondly. But at every update the old lines still remain. the variables are named german but i don't think thats that much of an issue.
Code for GUI:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author user
*/
public class Uhr extends javax.swing.JFrame implements ActionListener {
/**
* Creates new form Uhr
*/
public Uhr() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
panel = new javax.swing.JPanel();
ueberschrift = new javax.swing.JLabel();
eingabe = new javax.swing.JTextField();
eingabe_label = new javax.swing.JLabel();
button = new javax.swing.JButton();
label = new javax.swing.JLabel();
jButton1.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setName("Uhr"); // NOI18N
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 220, Short.MAX_VALUE)
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 220, Short.MAX_VALUE)
);
ueberschrift.setFont(new java.awt.Font("MS Reference Sans Serif", 0, 18)); // NOI18N
ueberschrift.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
ueberschrift.setText("Analoguhr");
eingabe.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eingabeActionPerformed(evt);
}
});
eingabe_label.setFont(new java.awt.Font("MS Reference Sans Serif", 0, 12)); // NOI18N
eingabe_label.setText("Uhrzeit:");
button.setText("Uhr zeichnen");
button.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
});
label.setFont(new java.awt.Font("MS Reference Sans Serif", 0, 24)); // NOI18N
label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(eingabe)
.addComponent(eingabe_label, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(button, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
.addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
.addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(98, 98, 98)
.addComponent(ueberschrift, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(ueberschrift, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(eingabe_label, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eingabe, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(button)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(label, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83))
.addComponent(panel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
pack();
}// </editor-fold>
public void uhr_paint() {
Graphics2D leinwand = (Graphics2D) panel.getGraphics();
leinwand.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
leinwand.setColor(Color.BLACK);
leinwand.drawOval(10, 10, 200, 200);
}
public void zifferblatt() {
Graphics2D leinwand = (Graphics2D) panel.getGraphics();
leinwand.setStroke(new BasicStroke(10));
leinwand.setColor(Color.BLACK);
double winkel;
int zifferblatt_x, zifferblatt_y;
int radius = 85;
for (int i = 1; i <= 12; i++) {
winkel = i * Math.PI / 6;
zifferblatt_x = (int) (radius * Math.sin(winkel));
zifferblatt_y = (int) (radius * Math.cos(winkel));
leinwand.drawString(Integer.toString(i), 110 + zifferblatt_x, 110 - zifferblatt_y);
}
}
private void eingabeActionPerformed(java.awt.event.ActionEvent evt) {
}
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
Zeit.zeit();
uhr_paint();
zifferblatt();
Timer.timer();
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Uhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Uhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Uhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Uhr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Uhr().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JButton button;
public static javax.swing.JTextField eingabe;
private javax.swing.JLabel eingabe_label;
private javax.swing.JButton jButton1;
public static javax.swing.JLabel label;
public static javax.swing.JPanel panel;
private javax.swing.JLabel ueberschrift;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Code to get the time:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author user
* die Eingabe in Integer umwandeln
*/
public class Zeit {
public static int stundenzeit, minutenzeit, sekundenzeit,
stunde_x, stunde_y, minute_x, minute_y, sekunde_x, sekunde_y;
public static String uhrzeit;
public static void zeit() {
try {
uhrzeit = Uhr.eingabe.getText();
String[] uhrzeit_split = uhrzeit.split(":");
String stundenzeit_string = uhrzeit_split[0];
String minutenzeit_string = uhrzeit_split[1];
String sekundenzeit_string = uhrzeit_split[2];
stundenzeit = Integer.parseInt(stundenzeit_string);
minutenzeit = Integer.parseInt(minutenzeit_string);
sekundenzeit = Integer.parseInt(sekundenzeit_string);
if (stundenzeit < 1 | sekundenzeit > 12
| minutenzeit < 0 | minutenzeit > 59
| sekundenzeit < 0 | sekundenzeit > 59) {
System.err.println("Bitte schreibe ein Uhrzeit!");
}
} catch (NumberFormatException e) {
System.err.println("Bitte schreibe ein Uhrzeit!");
}
}
}
Clock hands:
import java.awt.BasicStroke;
import java.awt.Color;
import static java.awt.Color.black;
import static java.awt.Color.red;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
/**
*
* @author user Die Zeiger berechnen und zeichnen
*/
public class Zeiger {
public static int Stunde_x, Stunde_y, Minute_x, Minute_y, Sekunde_x, Sekunde_y;
public static int stundenzeit_x(int Zeigerlänge, int radiusUhr) {
Stunde_x = (int) ((int) Zeigerlänge * Math.sin(Math.toRadians(((12 - Timer.stundenzeit_update) * 30) - 180 - (Timer.minutenzeit_update / 2))) + radiusUhr);
return Stunde_x;
}
public static int stundenzeit_y(int Zeigerlänge, int radiusUhr) {
Stunde_y = (int) ((int) Zeigerlänge * Math.cos(Math.toRadians(((12 - Timer.stundenzeit_update) * 30) - 180 - (Timer.minutenzeit_update / 2))) + radiusUhr);
return Stunde_y;
}
public static int minutenzeit_x(int Zeigerlänge, int radiusUhr) {
Minute_x = (int) (Zeigerlänge * Math.sin(Math.toRadians(((60 - Timer.minutenzeit_update) * 6) - 180)) + radiusUhr);
return Minute_x;
}
public static int minutenzeit_y(int Zeigerlänge, int radiusUhr) {
Minute_y = (int) (Zeigerlänge * Math.cos(Math.toRadians(((60 - Timer.minutenzeit_update) * 6) - 180)) + radiusUhr);
return Minute_y;
}
public static int sekundenzeit_x(int Zeigerlänge, int radiusUhr) {
Sekunde_x = (int) (Zeigerlänge * Math.sin(Math.toRadians(((60 - Timer.sekundenzeit_update) * 6) - 180)) + radiusUhr);
return Sekunde_x;
}
public static int sekundenzeit_y(int Zeigerlänge, int radiusUhr) {
Sekunde_y = (int) (Zeigerlänge * Math.cos(Math.toRadians(((60 - Timer.sekundenzeit_update) * 6) - 180)) + radiusUhr);
return Sekunde_y;
}
public static void stundenzeiger_paint() {
Graphics2D leinwand = (Graphics2D) Uhr.panel.getGraphics();
int radiusUhr = 110;
stundenzeit_x(40, radiusUhr);
stundenzeit_y(40, radiusUhr);
leinwand.setStroke(new BasicStroke((float) 1.5));
leinwand.setColor(black);
leinwand.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
leinwand.drawLine(110, 110, Stunde_x, Stunde_y);
}
public static void minutenzeiger_paint() {
Graphics2D leinwand = (Graphics2D) Uhr.panel.getGraphics();
int radiusUhr = 110;
minutenzeit_x(70, radiusUhr);
minutenzeit_y(70, radiusUhr);
leinwand.setStroke(new BasicStroke((float) 1.25));
leinwand.setColor(Color.BLUE);
leinwand.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
leinwand.drawLine(110, 110, Minute_x, Minute_y);
}
public static synchronized void sekundenzeiger_paint() {
Graphics2D leinwand = (Graphics2D) Uhr.panel.getGraphics();
int radiusUhr = 110;
sekundenzeit_x(80, radiusUhr);
sekundenzeit_y(80, radiusUhr);
leinwand.setStroke(new BasicStroke(1));
leinwand.setColor(red);
leinwand.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
leinwand.drawLine(110, 110, Sekunde_x, Sekunde_y);
}
}
And finally the timer:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
*
* @author user
* Sekuendliches aktualisieren des Timer-labels und der Uhr
*/
public class Timer {
static int sekundenzeit_update = Zeit.sekundenzeit,
minutenzeit_update = Zeit.minutenzeit,
stundenzeit_update = Zeit.stundenzeit;
static String uhrzeit_update = Zeit.stundenzeit + ":"
+ Zeit.minutenzeit + ":" + Zeit.sekundenzeit;
public static void timer() {
Runnable helloRunnable;
helloRunnable = () -> {
Uhr.label.setText("");
Uhr.label.setText(uhrzeit_update);
timer_ausführen();
Zeiger.stundenzeiger_paint();
Zeiger.minutenzeiger_paint();
Zeiger.sekundenzeiger_paint();
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}
public static void timer_ausführen() {
sekundenzeit_update += 1;
if (sekundenzeit_update > 60) {
sekundenzeit_update = 0;
minutenzeit_update += 1;
if (minutenzeit_update > 60) {
minutenzeit_update = 0;
stundenzeit_update += 1;
}
}
uhrzeit_update = stundenzeit_update + ":" + minutenzeit_update
+ ":" + sekundenzeit_update;
}
}
clock in the beginning: https://i.stack.imgur.com/Nf5Hu.png
I did this with Netbeans and I had the idea to draw this by hand but I really dont know how to do this.
This is not the way Swing JComponents are supposed to be used. You are supposed to create a subclass (of JPanel in your case) and in the subclass override
paintComponent, which should be made to always paint the correct time whenever it is called. Then you can use a javax.swing.Timer to callrepaintevery second.Sie sollen Swing in ein anderes weg nutzen. Sie mussen JPanel "Subclassen", und dann diese funktion "paintComponent" uberritten :)