java project swing analog clock program
This Analog Clock project is a swing java project.
What should you know before start?
- what is java?
- what is javax.swing package
- swing GUI metaphors
Q. what is java?
A. Java is an object-oriented general-purpose programming language. It is
intended to let application developers write once and run anywhere. Java
is one of the most popular programming languages in use according to
GitHub, particularly for client-server web applications. Java is designed
by James Gosling and developed by Sun Microsystem.
Q. What is swing?
A. Swing is used to create window-based applications. It is built on the
top of AWT (Abstract Windowing Toolkit) API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight
components. The javax.swing package provides classes for java swing API
such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
Know more through:-
1) Wikipedia-
https://en.wikipedia.org/wiki/Java_(programming_language)
2) JavaTpoint- https://www.javatpoint.com/java-tutorial
vsCode Editor:- Visual Studio Code is a code editor redefined and
optimized for building and debugging modern web and cloud
applications.
Link for downloading :-
VsCode Editor
Link for downloading :- Java Playlist for all the video on java pattern programming- link:-
1. Introduction:
https://youtu.be/wI5ll75ziMs
2. create dial:
https://youtu.be/o-5YLVSIXQk 3. draw hands:
https://youtu.be/JZJ6J_Opm_g
4. draw Label:
https://youtu.be/2aSHsJqyKd4
5. start clock or move hands:
https://youtu.be/QwhwvYS89SE
find me on linkedIn:
Click Here
find me on Instagram:
click Here
In this program Java, java Swing package and vscode editor have been used.
In this program Java, java Swing package and vscode editor have been used.
import java.util.List; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class App extends JFrame{ private static final long serialVersionUID = 1L; ClockContainer container = null; public App(){ setLayout(null); container = new ClockContainer(); container.setLocation(0,0); add(container); setBounds(20,20,300,328); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void startClock(){ List<Point> secondPoint = container.grabPoint(140); List<Point> minutePoint = container.grabPoint(130); List<Point> hourPoint = container.grabPoint(120); while(true){ for(int i = 0; i < 360; i += 6){ Point hour = hourPoint.get(i); container.updateHour(150 + hour.getX(), 150 -hour.getY()); for(int j = 0; j < 360; j += 6){ Point minute = minutePoint.get(j); container.updateMinute(150 + minute.getX(), 150 - minute.getY()); for(int k = 0; k < 360; k += 6){ Point second = secondPoint.get(k); container.updateSecond(150 + second.getX(), 150 - second.getY()); //wait 1 sec try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(() -> { App app = new App(); app.setTitle("Analog Clock"); new Thread(() -> { app.startClock(); }).start(); }); } }
public class Point { private final int x; private final int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public int getY() { return y; } @Override public String toString() { return "Point [x=" + x + ", y=" + y + "]"; } }
import java.awt.*; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; public class ClockContainer extends JPanel{ private static final long serialVersionUID = 1L; private int secondX = 150, secondY = 10; private int minuteX = 150, minuteY = 20; private int hourX = 150, hourY = 30; ClockContainer(){ setBackground(Color.GRAY); setSize(300,300); setPreferredSize(new Dimension(300,300)); } //get all circlefrance x, y point public List<Point> grabPoint(int handLen){ if(handLen == 0){ handLen = 140; } List<Point> pointList = new ArrayList<>(); for(int i = 0; i < 360; i++){ double radians = Math.toRadians(i); double sinValue = Math.sin(radians); double cosValue = Math.cos(radians); final int X = (int) (sinValue * handLen); final int Y = (int) (cosValue * handLen); pointList.add(new Point(X,Y)); } return pointList; } //update minute hour second public void updateSecond(int secondX, int secondY){ this.secondX = secondX; this.secondY = secondY; repaint(); updateUI(); } public void updateMinute(int minuteX, int minuteY){ this.minuteX = minuteX; this.minuteY = minuteY; } public void updateHour(int HourX, int HourY){ this.hourX = HourX; this.hourY = HourY; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); drawDial(g); drawSecondHand(g); drawMinuteHand(g); drawHourHand(g); drawLabel(g); } private void drawSecondHand(Graphics g) { g.setColor(Color.GREEN); g.drawLine(150, 150, secondX, secondY); } private void drawMinuteHand(Graphics g) { g.setColor(Color.RED); g.drawLine(150, 150, minuteX, minuteY); } private void drawHourHand(Graphics g) { g.setColor(Color.ORANGE); g.drawLine(150, 150, hourX, hourY); } public void drawDial(Graphics g){ g.setColor(Color.BLACK); g.drawOval(0, 0, 300, 300); g.fillOval(145, 145, 10, 10); } //lable public void drawLabel(Graphics g){ List<Point> p = grabPoint(140); List<Point> points = new ArrayList<>(); //reverse list for(int i = p.size() -1; i >= 0; i--){ points.add(p.get(i)); } for(int j = 0; j < 360; j++){ if((j + 1) % 30 == 0){ int hour = (j+1) / 30; if(hour == 0){ hour = 12; } g.setColor(Color.ORANGE); g.drawString("" + hour , 145 - points.get(j).getX(), 150 - points.get(j).getY()); } else if (( j+1 ) % 6 == 0){ g.setColor(Color.BLACK); g.fillOval(148 - points.get(j).getX(), 148 - points.get(j).getY(), 4, 4); } } } }
import java.util.Scanner fun main() { val scanner = Scanner(System.`in`) print("Enter your name: ") var str = scanner.nextLine() println("Hello "+ str) }
So much helpful
ReplyDelete