Saya memiliki dua kode: jam analog dan kalender grafis. Saya ingin memasukkan kode kalender ke dalam kode jam sehingga kalender berada di bawah jam saat program dimulai. Saya telah mencoba melakukan ini dengan beberapa cara berbeda, tetapi kalender tidak pernah terlihat. Kode di bawah ini.

Jam:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension; 
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Clock extends JFrame {
 private static final long serialVersionUID = 1L;
 private static final Color BACKGROUND_COLOR = new Color(24, 116, 205); 
 public Clock() {
 ClockPanel container = new ClockPanel();
 add(container, BorderLayout.CENTER);
 setBackground(BACKGROUND_COLOR);
 setDefaultCloseOperation(EXIT_ON_CLOSE);
 setResizable(false);
 pack();
 setVisible(true);
 }
 public static void main(String[] args) {
 new Clock();
 }
}
class ClockPanel extends JPanel implements Runnable {
 private static final long serialVersionUID = 1L;
 Thread t = new Thread(this);
 /** The coordinates used to paint the clock hands. */
 int xHandSec, yHandSec, xHandMin, yHandMin, xHandHour, yHandHour;
 /** The size of the clock. */
 private final int HORIZONTAL_SIZE = 500; private final int VERTICAL_SIZE = 500;
 /** The length of the clock hands relative to the clock size. */
 private final int secondHandLength = HORIZONTAL_SIZE / 2 - 50;
 private final int minuteHandLength = HORIZONTAL_SIZE / 2 - 70;
 private final int hourHandLength = HORIZONTAL_SIZE / 2 - 100;
 /** The distance of the dots from the origin (center of the clock). */
 private final int DISTANCE_DOT_FROM_ORIGIN = HORIZONTAL_SIZE / 2 - 40;
 private final int DIAMETER_BIG_DOT = 8;
 private final int DIAMETER_SMALL_DOT = 4;
 
 private final static Color GREY_COLOR = new Color(160,160,160);
 public ClockPanel() {
 setMinimumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
 setMaximumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
 setPreferredSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
 setLayout(null);
 t.start();
 }
 /**
 * At each iteration we recalculate the coordinates of the clock hands,
 * and repaint everything.
 */
 public void run(){
 while(true){
 try{
 int currentSecond = Calendar.getInstance().get(Calendar.SECOND);
 int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
 int currentHour = Calendar.getInstance().get(Calendar.HOUR);
 xHandSec = minToLocation(currentSecond, secondHandLength).x;
 yHandSec = minToLocation(currentSecond, secondHandLength).y;
 xHandMin = minToLocation(currentMinute, minuteHandLength).x;
 yHandMin = minToLocation(currentMinute, minuteHandLength).y;
 xHandHour = minToLocation(currentHour * 5 + 
getRelativeHour(currentMinute), hourHandLength).x;
 yHandHour = minToLocation(currentHour * 5 + 
getRelativeHour(currentMinute), hourHandLength).y;
 repaint();
 Thread.sleep(500);
 } catch(InterruptedException ie){
 ie.printStackTrace();
 }
 }
 }
 /**
 * Returns how much the hour hand should be ahead
 * according to the minutes value.
 * 04:00, return 0.
 * 04:12, return 1, so that we move the hour handle ahead of one dot. 
 * @param min The current minute.
 * @return The relative offset to add to the hour hand. 
 */
 private int getRelativeHour(int min) {
 return min / 12; }
 protected void paintComponent(Graphics g){
 Graphics2D g2 = (Graphics2D)g;
 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 RenderingHints.VALUE_ANTIALIAS_ON);
 g2.clearRect(0, 0, getWidth(), getHeight());
 // Draw the dots
 g2.setColor(GREY_COLOR);
 for (int i = 0; i < 60; i++) {
 
 Point dotCoordinates = minToLocation(i, DISTANCE_DOT_FROM_ORIGIN);
 g2.setColor((i <= Calendar.getInstance().get(Calendar.SECOND)) ? 
Color.black : GREY_COLOR);
 
 if (i % 5 == 0) {
 // big dot
 g2.fillOval(dotCoordinates.x - (DIAMETER_BIG_DOT / 2),
 dotCoordinates.y - (DIAMETER_BIG_DOT / 2),
 DIAMETER_BIG_DOT,
 DIAMETER_BIG_DOT);
 } else {
 // small dot
 g2.fillOval(dotCoordinates.x - (DIAMETER_SMALL_DOT / 2),
 dotCoordinates.y - (DIAMETER_SMALL_DOT / 2),
 DIAMETER_SMALL_DOT,
 DIAMETER_SMALL_DOT);
 }
 }
 // Draw the clock hands
 g2.setColor(Color.black);
 g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandSec, 
yHandSec);
 g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandMin, 
yHandMin);
 g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandHour, 
yHandHour);
 g.setFont(new Font("TimesRoman", Font.BOLD, 15));
 g.drawString("1", 360, 60);
 g.drawString("2", 440, 140);
 g.drawString("3", 470, 250);
 g.drawString("4", 440, 370);
 g.drawString("5", 360, 450);
 g.drawString("6", 244, 480);
 g.drawString("7", 130, 450);
 g.drawString("8", 50, 370);
 g.drawString("9", 20, 250);
 g.drawString("10", 50, 140);
 g.drawString("11", 130, 60);
 g.drawString("12", 244, 25);
 }
 /**
 * Converts current second/minute/hour to x and y coordinates.
 * @param min The current minute
 * @param radius The radius length 
 * @return the coordinates point
 */
 private Point minToLocation(int timeStep, int radius) {
 double t = 2 * Math.PI * (timeStep-15) / 60;
 int x = (int)(HORIZONTAL_SIZE / 2 + radius * Math.cos(t));
 int y = (int)(VERTICAL_SIZE / 2 + radius * Math.sin(t));
 return new Point(x, y);
 } 
}

Dan ini adalah kalender

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JPanel {
 SimpleDateFormat month = new SimpleDateFormat("MMMM");
 SimpleDateFormat year = new SimpleDateFormat("yyyy");
 SimpleDateFormat day = new SimpleDateFormat("d");
 Date date = new Date();
 public Main() {
 setPreferredSize(new Dimension(300, 280));
 }
 public void setDate(Date date) {
 this.date = date;
 }
 public void paintComponent(Graphics g) {
 ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 RenderingHints.VALUE_ANTIALIAS_ON);
 g.fillRect(0, 0, getWidth(), getHeight());
 super.paintComponent(g);
 g.setFont(new Font("TimesRoman", Font.PLAIN, 17));
 g.setColor(Color.black);
 g.drawString(month.format(date).toUpperCase(), 34, 36);
 g.setColor(Color.black);
 g.drawString(year.format(date), 235, 36);
 g.setFont(new Font("TimesRoman", Font.PLAIN, 10)); Calendar today = Calendar.getInstance();
 today.setTime(date);
 Calendar cal = Calendar.getInstance();
 cal.setTime(date);
 cal.set(Calendar.DATE, 1);
 cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
 for (int week = 0; week < 5; week++) {
 for (int d = 0; d < 7; d++) {
 g.drawString(day.format(cal.getTime()), d * 30 + 46 + 4, week * 29 + 81 + 20);
 cal.add(Calendar.DATE, +1);
 }
 }
 }
 public static void main(String[] args) {
 JFrame frame = new JFrame();frame.setPreferredSize(new Dimension(300, 280));
Main ch = new Main();
ch.setDate(new Date());
frame.getContentPane().add(ch);
frame.setUndecorated(true);
frame.pack();
frame.setVisible(true);
 }
}
-2
Dawid 7 Mei 2021, 21:48

1 menjawab

Jawaban Terbaik

Inilah salah satu cara untuk menggabungkan GUI Anda.

Clock / Calendar

Saya mengubah kode JFrame Anda.

Berikut kode runnable lengkapnya.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AnalogClock extends JFrame {
    private static final long serialVersionUID = 1L;
    private static final Color BACKGROUND_COLOR = new Color(24, 116, 205);

    public AnalogClock() {
        setTitle("Clock / Calendar");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        
        ClockPanel container = new ClockPanel();
        add(container, BorderLayout.BEFORE_FIRST_LINE);
        CalendarPanel panel = new CalendarPanel();
        panel.setDate(new Date());
        add(panel, BorderLayout.CENTER);
        
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AnalogClock();
            }
        });
    }
}

class ClockPanel extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    Thread t = new Thread(this);
    /** The coordinates used to paint the clock hands. */
    int xHandSec, yHandSec, xHandMin, yHandMin, xHandHour, yHandHour;
    /** The size of the clock. */
    private final int HORIZONTAL_SIZE = 500;
    private final int VERTICAL_SIZE = 500;
    /** The length of the clock hands relative to the clock size. */
    private final int secondHandLength = HORIZONTAL_SIZE / 2 - 50;
    private final int minuteHandLength = HORIZONTAL_SIZE / 2 - 70;
    private final int hourHandLength = HORIZONTAL_SIZE / 2 - 100;
    /** The distance of the dots from the origin (center of the clock). */
    private final int DISTANCE_DOT_FROM_ORIGIN = HORIZONTAL_SIZE / 2 - 40;
    private final int DIAMETER_BIG_DOT = 8;
    private final int DIAMETER_SMALL_DOT = 4;

    private final static Color GREY_COLOR = new Color(160, 160, 160);

    public ClockPanel() {
        setMinimumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
        setMaximumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
        setPreferredSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
        setLayout(null);
        t.start();
    }

    /**
     * At each iteration we recalculate the coordinates of the clock hands, and
     * repaint everything.
     */
    public void run() {
        while (true) {
            try {
                int currentSecond = Calendar.getInstance().get(Calendar.SECOND);
                int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
                int currentHour = Calendar.getInstance().get(Calendar.HOUR);
                xHandSec = minToLocation(currentSecond, secondHandLength).x;
                yHandSec = minToLocation(currentSecond, secondHandLength).y;
                xHandMin = minToLocation(currentMinute, minuteHandLength).x;
                yHandMin = minToLocation(currentMinute, minuteHandLength).y;
                xHandHour = minToLocation(currentHour * 5 + 
                        getRelativeHour(currentMinute), hourHandLength).x;
                yHandHour = minToLocation(currentHour * 5 + 
                        getRelativeHour(currentMinute), hourHandLength).y;
                repaint();
                Thread.sleep(500);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    /**
     * Returns how much the hour hand should be ahead according to the minutes
     * value. 04:00, return 0. 04:12, return 1, so that we move the hour handle
     * ahead of one dot.
     * 
     * @param min The current minute.
     * @return The relative offset to add to the hour hand.
     */
    private int getRelativeHour(int min) {
        return min / 12;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.clearRect(0, 0, getWidth(), getHeight());
        // Draw the dots
        g2.setColor(GREY_COLOR);
        for (int i = 0; i < 60; i++) {

            Point dotCoordinates = minToLocation(i, DISTANCE_DOT_FROM_ORIGIN);
            g2.setColor((i <= Calendar.getInstance().get(Calendar.SECOND)) ? 
                    Color.black : GREY_COLOR);

            if (i % 5 == 0) {
                // big dot
                g2.fillOval(dotCoordinates.x - (DIAMETER_BIG_DOT / 2), 
                        dotCoordinates.y - (DIAMETER_BIG_DOT / 2),
                        DIAMETER_BIG_DOT, DIAMETER_BIG_DOT);
            } else {
                // small dot
                g2.fillOval(dotCoordinates.x - (DIAMETER_SMALL_DOT / 2), 
                        dotCoordinates.y - (DIAMETER_SMALL_DOT / 2),
                        DIAMETER_SMALL_DOT, DIAMETER_SMALL_DOT);
            }
        }
        // Draw the clock hands
        g2.setColor(Color.black);
        g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandSec, yHandSec);
        g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandMin, yHandMin);
        g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandHour, yHandHour);
        g.setFont(new Font("Times New Roman", Font.BOLD, 15));
        g.drawString("1", 360, 60);
        g.drawString("2", 440, 140);
        g.drawString("3", 470, 250);
        g.drawString("4", 440, 370);
        g.drawString("5", 360, 450);
        g.drawString("6", 244, 480);
        g.drawString("7", 130, 450);
        g.drawString("8", 50, 370);
        g.drawString("9", 20, 250);
        g.drawString("10", 50, 140);
        g.drawString("11", 130, 60);
        g.drawString("12", 244, 25);
    }

    /**
     * Converts current second/minute/hour to x and y coordinates.
     * 
     * @param min    The current minute
     * @param radius The radius length
     * @return the coordinates point
     */
    private Point minToLocation(int timeStep, int radius) {
        double t = 2 * Math.PI * (timeStep - 15) / 60;
        int x = (int) (HORIZONTAL_SIZE / 2 + radius * Math.cos(t));
        int y = (int) (VERTICAL_SIZE / 2 + radius * Math.sin(t));
        return new Point(x, y);
    }
    
}

class CalendarPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    
    SimpleDateFormat month = new SimpleDateFormat("MMMM");
    SimpleDateFormat year = new SimpleDateFormat("yyyy");
    SimpleDateFormat day = new SimpleDateFormat("d");
    Date date = new Date();

    public CalendarPanel() {
        setPreferredSize(new Dimension(300, 280));
    }

    public void setDate(Date date) {
        this.date = date;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);

        g.setFont(new Font("Times New Roman", Font.PLAIN, 17));
        g.setColor(Color.black);
        g.drawString(month.format(date).toUpperCase(), 34, 36);
        g.setColor(Color.black);
        g.drawString(year.format(date), 235, 36);
        g.setFont(new Font("Times New Roman", Font.PLAIN, 10));

        Calendar today = Calendar.getInstance();
        today.setTime(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
        for (int week = 0; week < 6; week++) {
            for (int d = 0; d < 7; d++) {
                g.drawString(day.format(cal.getTime()), d * 30 + 46 + 4, 
                        week * 29 + 81 + 20);
                cal.add(Calendar.DATE, +1);
            }
        }
    }
     
}
0
Gilbert Le Blanc 7 Mei 2021, 20:30