Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

ClockWriter , somebody help me

java clock

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 EdisjanSadikaj

EdisjanSadikaj

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 18 December 2012 - 12:38 PM

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class ClockWriter extends JPanel
{ public ClockWriter ()
{ int width = 200;
  JFrame clock = new JFrame();
  clock.getContentPane().add(this);
  clock.setTitle("Ora");
  clock.setSize (width, width);
  clock.setVisible(true);
}
public void paintComponent(Graphics g)
{ int width = 200;
  g.setColor(Color.red);
  g.fillRect(0,0,width,width);
  GregorianCalendar time = new GregorianCalendar();
  int minutesAngle = 90 - (time.get(Calendar.MINUTE)*6);
  int hoursAngle = 90 - (time.get(Calendar.HOUR)*30);
  int secondsAngle = 90 - (time.get(Calendar.SECOND)*6);
  int leftedge = 40;
  int top = 60;
  int diameter = 100;
  g.setColor(Color.white);
  g.drawOval(leftedge, top,diameter , 2 * diameter);
  g.setColor(Color.black);
  g.fillArc(leftedge + 5, top + 5, diameter - 10, diameter - 10, minutesAngle,-4);
  g.setColor(Color.blue);
  g.fillArc(leftedge + 10, top + 10, diameter - 10, diameter - 10, hoursAngle, -8);
  g.setColor(Color.white);
  g.fillArc(leftedge, top, diameter, diameter, secondsAngle,5);
  this.repaint();
}
public static void main(String[] a)
{ new ClockWriter();
}
}


(this is normal clock , help me to do digital one , i don`t know how to do it.... )

Edited by Roger, 19 December 2012 - 08:03 AM.


#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 18 December 2012 - 06:54 PM

Well, the analog clock looks good but this is your homework, right? So to help you to make a digital clock following your analog clock, we need a heads up from you -- so better start on it.

#3 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 19 December 2012 - 01:01 AM

Digital is easier :D, you just need to print the numbers in the pane.

#4 Roman Y

Roman Y

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 196 posts

Posted 19 December 2012 - 01:10 AM

Would be cooler to draw it in seven-segment display character representation.

Edited by Roman Y, 19 December 2012 - 01:11 AM.


#5 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts

Posted 19 December 2012 - 06:27 AM

Here is start up -- following code will draw the first version of digital clock -- the very basic one.

public void paintComponent(Graphics g)
{
int width = this.getWidth();
int height = this.getHeight();
g.setColor(Color.red);
g.fillRect(0,0,width,height);
GregorianCalendar time = new GregorianCalendar();
g.setColor(Color.blue);
String currentTime = String.format("%d:%d:%d", time.get(Calendar.HOUR), time.get(Calendar.MINUTE), time.get(Calendar.SECOND));
g.drawString(currentTime, width/2, height/2);
this.repaint();
}

Next, you can start writing a method to display a segmented (perhaps the seven-segment) number. However, the simple idea to draw a seven segment display is described in the image below--
seven_segment.png

Draw segment 1 only if the number to be drawn is 0, 2, 3, 5, 6, 7, 8 or 9. And same logic for other segments --- you know what I mean, right? Following is an incomplete method to draw a seven segment number --
// Here <x, y, width, height> is the rectangle of the seven-segment to be drawn.
public void drawSevenSegmentDigit(Graphics g, int x, int y, int width, int height, int n)
{
// segment 1
if (0 == n || 2 == n || 3 == n || 5 == n || 6 == n || 7 == n || 8 == n || 9 == n)
{
g.drawLine(x, y, x + width, y);
}

// draw other segments here
}
And, we need to call the drawSevenSegmentDigit method from the paintComponent method with providing a box/rectangle for each digit of hour(s), minute(s) and second(s).

Edited by kernelcoder, 19 December 2012 - 08:17 AM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download