Drehregler

J

jakomadred

Mitglied
Hallo zusammen ich bin anfänger und habe aufgabe
ich habe hier das Programm für Drehregler und will damit die uhrzeit umstellen. ( ich will mit diesem Drehknopf die Stunden und die minuten Hoch und runter zählen ) kann jemand mir hilfen ich schreibe das Code des Uhr Programm im Kommentare vielen dank

Java:
/*
 * RotaryButtonDemo.java
 */

import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;

class RotaryButtonDemo extends JFrame {
    Calendar calendar;
    SimpleDateFormat timeFormat;
    JLabel timeLabel;
    String time;
    public RotaryButtonDemo() {
        super("RotaryButtonDemo");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setLayout(new GridBagLayout());
        add(new RotaryButton(0, 0));
    }

    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new RotaryButtonDemo().setVisible(true);
            }
        });
    }
}

class RotaryButton extends JComponent {

    private ButtonLabel b;
    private int xB;
    private int yB;
    private int size = 100;

    public RotaryButton(final int x, final int y) {
        xB = x + 15;
        yB = y + 15;
        setLayout(null);
        b = new ButtonLabel();
        add(b);
        setPreferredSize(new Dimension(size + 70, size + 60));
    }

    @Override
    protected void paintComponent(final Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.drawString("Uhrmacher", xB, yB + size + 40);
        double angle = 45;
        for (int i = 0; i < 15; i++) {
            double sin = Math.sin(Math.toRadians(angle));
            double cos = Math.cos(Math.toRadians(angle));
            int x1 = (int) ((xB + size / 2) - cos * 40 - sin * 40);
            int y1 = (int) ((yB + size / 2) + sin * 40 - cos * 40);
            g.setColor(Color.GRAY);
            g.drawLine(x1, y1, (xB + size / 2), (yB + size / 2));
            if (i % 5 == 0) {
                g.drawString(String.valueOf(i), x1, y1 + 3);
            }
            angle -= 18;
        }
        g.setColor(Color.BLUE);
        g.drawRect(xB-15, yB-15, size + 69, size + 59);
        g.drawString(b.toString(), xB, yB + size + 20);
    }

    class ButtonLabel extends JLabel {

        private double angle;
        private int value;
        private GradientPaint gradient1;

        public ButtonLabel() {
            setLocation(xB, yB);
            setSize(new Dimension(size, size));
            gradient1 = new GradientPaint(0, 50, Color.LIGHT_GRAY, 0, 0, Color.GRAY, true);
            addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(final MouseEvent e) {
                    if (SwingUtilities.isLeftMouseButton(e)) {
                        if (angle != 18 * 14) {
                            angle += 18;
                            value++;
                        }
                    } else {
                        if (angle != 0) {
                            angle -= 18;
                            value--;
                        }
                    }
                    repaint();
                }
            });

        }

        @Override
        protected void paintComponent(final Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.rotate(Math.toRadians(angle), 50, 50);
            g2.setPaint(gradient1);
            g.fillOval(0, 0, size, size);
            g.setColor(Color.DARK_GRAY);
            g.drawLine(1, 50, 15, 45);
            g.drawLine(1, 50, 15, 55);
        }

        @Override
        public String toString() {
            return "Value: " + value;
        }
    }
}
 
J

jakomadred

Mitglied
Java:
package com.company;
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.JFrame;
public class myfarme extends JFrame{
    Calendar calendar;
    SimpleDateFormat timeFormat;
    JLabel timeLabel;
    String time ;
    myfarme()
    {

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Clock Program");
        this.setLayout(new FlowLayout());
        this.setSize(500,300);
        timeFormat = new SimpleDateFormat("HH:MM a");
        timeLabel = new JLabel();
        timeLabel.setFont(new Font("Verdana",Font.PLAIN,50));
        timeLabel.setForeground(new Color(0x00FF00));
        timeLabel.setBackground(Color.black);
        timeLabel.setOpaque(true);
        this.add(timeLabel);
        this.setVisible(true);
        setTime();
    }



    public void setTime() {
        while(true) {
            time = timeFormat.format(Calendar.getInstance().getTime());
            timeLabel.setText(time);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
 

Oben