Commit e768e40f authored by Ali Shakoori's avatar Ali Shakoori

lab 3 full commit

parents
Pipeline #320 failed with stages
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/lab3.iml" filepath="$PROJECT_DIR$/lab3.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import org.clock.logic.ClockLogic;
import org.clock.logic.TimeZone;
import org.clock.ui.ClockUI;
public class Main {
public static void main(String[] args) {
// ClockLogic n = new ClockLogic(11,23,55);
//n.tik(7700);
TimeZone a = new TimeZone(2, 12, 0);
ClockLogic n = new ClockLogic(6, 40, 3);
TimeZone a2 = new TimeZone(3, 30, 1);
TimeZone a3 = new TimeZone(5, 0, 0);
ClockUI x = new ClockUI(n.getHours(), n.getMin(), n.getSec());
ClockLogic n2 = new ClockLogic(6, 40, 3, a2);
ClockUI x2 = new ClockUI(n.getHours(), n.getMin(), n.getSec());
ClockLogic n3 = new ClockLogic(6, 40, 3, a3);
ClockUI x3 = new ClockUI(n.getHours(), n.getMin(), n.getSec());
while (true) {
n.tik(1);
x.setHour(n.getHours());
x.setMinute(n.getMin());
x.setSecond(n.getSec());
n2.tik(1);
x2.setHour(n2.getHours());
x2.setMinute(n2.getMin());
x2.setSecond(n2.getSec());
n3.tik(1);
x3.setHour(n3.getHours());
x3.setMinute(n3.getMin());
x3.setSecond(n3.getSec());
try {
Thread.sleep(1000);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
\ No newline at end of file
package org.clock.logic;
import org.clock.logic.TimeZone;
public class ClockLogic {
private int hours, min, sec;
public void setHours(int hours) {
this.hours = hours;
}
public void setMin(int min) {
this.min = min;
}
public void setSec(int sec) {
this.sec = sec;
}
public int getHours() {
return hours;
}
public int getMin() {
return min;
}
public int getSec() {
return sec;
}
public void tik(int second) {
int hour = 0;
int minute = 0;
if (second >= 3600) {
hour = second / 3600;
hours += hour;
if (second % 3600 > 60) {
minute = (second % 3600) / 60;
min += minute;
}
sec += second - ((hour * 3600) + (minute * 60));
} else {
minute = (second) / 60;
min += minute;
sec += second - (minute * 60);
}
if(sec>=60) {
min+=sec/60;
sec = sec - ((sec/60)*60);
}
if(min>=60){
hours+=min/60;
min = min - ((min/60)*60);
}
if(hours>24){
hours = hours - ((hours/24)*24);
}
}
public ClockLogic (int hours , int min , int sec){
this.setSec(sec);
this.setHours(hours);
this.setMin(min);
}
public ClockLogic (int hours , int min , int sec , TimeZone tz) {
if(tz.getP() == 1){
this.hours = hours ;
this.min = min ;
this.sec = sec ;
tik(tz.getH()*3600 + tz.getM() * 60);
}else {
this.hours += hours-tz.getH() ;
this.min += min-tz.getM() ;
this.sec = sec ;
if(this.hours <0){
this.hours=24+this.hours ;
}
if(this.min <0){
this.min = 60 + this.min;
}
}
}
}
package org.clock.logic;
public class TimeZone {
private int h,m,p ;
public void setH(int h) {
this.h = h;
}
public void setM(int m) {
this.m = m;
}
public void setP(int p) {
this.p = p;
}
public int getH() {
return h;
}
public int getM() {
return m;
}
public int getP() {
return p;
}
public TimeZone (int h , int m , int p ){
this.setH(h);
this.setM(m);
this.setP(p);
}
}
package org.clock.ui;
import javax.swing.*;
import java.awt.*;
class ClockPanel extends JPanel {
/**
* The coordinates used to paint the clock hands.
*/
private int xHandSec, yHandSec, xHandMin, yHandMin, xHandHour, yHandHour;
/**
* The size of the clock.
*/
private final int HORIZONTAL_SIZE = 500;
private final int VERTICAL_SIZE = 500;
private final static Color GRAY_COLOR = new Color(160, 160, 160);
private static final Color BACKGROUND_COLOR = new Color(24, 116, 205);
private int second;
private int minute;
private int hour;
/**
* creates clock container, initialize background and size
*/
ClockPanel() {
setMinimumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
setMaximumSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
setPreferredSize(new Dimension(HORIZONTAL_SIZE, VERTICAL_SIZE));
setBackground(BACKGROUND_COLOR);
setLayout(null);
}
/**
* set seconds
*
* @param second second value
*/
void setSecond(int second) {
this.second = second;
}
/**
* set minute
*
* @param minute minute value
*/
void setMinute(int minute) {
this.minute = minute;
}
/**
* set hour
*
* @param hour hour value
*/
void setHour(int hour) {
this.hour = hour;
}
/**
* recalculates the coordinates of the clock hands,
* and repaint everything.
*/
void draw() {
int secondHandLength = HORIZONTAL_SIZE / 2 - 50;
xHandSec = minToLocation(second, secondHandLength).x;
yHandSec = minToLocation(second, secondHandLength).y;
int minuteHandLength = HORIZONTAL_SIZE / 2 - 70;
xHandMin = minToLocation(minute, minuteHandLength).x;
yHandMin = minToLocation(minute, minuteHandLength).y;
int hourHandLength = HORIZONTAL_SIZE / 2 - 100;
xHandHour = minToLocation(hour * 5 + getRelativeHour(hour), hourHandLength).x;
yHandHour = minToLocation(hour * 5 + getRelativeHour(hour), hourHandLength).y;
repaint();
}
/**
* 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;
}
/**
* paint clock hands
*
* @param g don't touch it!
*/
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(BACKGROUND_COLOR);
g2.fillRect(0, 0, getWidth(), getHeight());
// Draw the dots
g2.setColor(GRAY_COLOR);
for (int i = 0; i < 60; i++) {
int DISTANCE_DOT_FROM_ORIGIN = HORIZONTAL_SIZE / 2 - 40;
Point dotCoordinates = minToLocation(i, DISTANCE_DOT_FROM_ORIGIN);
g2.setColor((i <= second ? Color.white : GRAY_COLOR));
if (i % 5 == 0) {
// big dot
int DIAMETER_BIG_DOT = 8;
g2.fillOval(dotCoordinates.x - (DIAMETER_BIG_DOT / 2),
dotCoordinates.y - (DIAMETER_BIG_DOT / 2),
DIAMETER_BIG_DOT,
DIAMETER_BIG_DOT);
} else {
// small dot
int DIAMETER_SMALL_DOT = 4;
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.white);
g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandSec, yHandSec);
g2.setStroke(new BasicStroke(3));
g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandMin, yHandMin);
g2.setStroke(new BasicStroke(5));
g2.drawLine(HORIZONTAL_SIZE / 2, VERTICAL_SIZE / 2, xHandHour, yHandHour);
}
/**
* Converts current second/minute/hour to x and y coordinates.
*
* @param timeStep The current second/minute/hour
* @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);
}
}
\ No newline at end of file
package org.clock.ui; /**
* ClockUI is a simple package for drawing ClockUI!
* <p>
* Thanks to Paolo Boschini for his <a href="https://github.com/paoloboschini/analog-clock/blob/master/Clock.java">code</a>!
* <p>
* @author MohammadHossein Hessar
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
public class ClockUI extends JFrame {
private ClockPanel container;
public ClockUI(int hour, int minute, int second){
container = new ClockPanel();
setClock(hour,minute,second);
add(container, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setSize(500, 500);
setMinimumSize(new Dimension(500, 500));
setLocationRelativeTo(null);
pack();
setVisible(true);
}
public ClockUI() {
this(0,0,0);
}
/**
* set time to display
* do nothing when values are out of range!
*
* @param hour hour value
* @param minute minute value
* @param second second value
*/
public void setClock(int hour, int minute, int second) {
if (hour > 24 || hour < 0)
return;
if (minute < 0 || minute > 60)
return;
if (second < 0 || second > 60)
return;
container.setHour(hour);
container.setMinute(minute);
container.setSecond(second);
container.draw();
}
/**
* set hour to display
* do nothing when values are out of range!
*
* @param hour hour value
*/
public void setHour(int hour) {
if (hour > 24 || hour < 0)
return;
container.setHour(hour);
container.draw();
}
/**
* set minute to display
* do nothing when values are out of range!
*
* @param minute minute value
*/
public void setMinute(int minute) {
if (minute < 0 || minute > 60)
return;
container.setMinute(minute);
container.draw();
}
/**
* set second to display
* do nothing when values are out of range!
*
* @param second second value
*/
public void setSecond(int second) {
if (second < 0 || second > 60)
return;
container.setSecond(second);
container.draw();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment