Sunday, December 19, 2010

Analog Clock

In create Analog Clock View.You much be have 2 class below
1.AnalogClock .java

2.AnalogClockView .java
// -->>>>> First AnalogClock .java

package utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class AnalogClock extends JPanel {

ImageIcon img;

private GregorianCalendar m_calendar;
private int[] x = new int[2];
private int[] y = new int[2];
private java.util.Timer clocktimer = new java.util.Timer();

/**
* You could set the TimeZone for the clock here. I used the Dfault time
* zone from the user so that every time the program runs on different
* computers the correct time is displayed
*/

private TimeZone clockTimeZone = TimeZone.getDefault();

// Constructor
public AnalogClock() {
this.setPreferredSize(new Dimension(210, 210));
this.setMinimumSize(new Dimension(210, 210));

// schedules the clocktimer task to scan for every 1000ms=1sec
clocktimer.schedule(new TickTimerTask(), 0, 1000);

}

// The Clock Face instance method
public void paint(Graphics g) {

g.setColor(Color.orange);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
drawCardinals((Graphics2D) g);
drawHands((Graphics2D) g);

}

// Endpoints of the Clock Hand
void clockMinutes(int startRadius, int endRadius, double theta) {
theta -= Math.PI / 2;
x[0] = (int) (getWidth() / 2 + startRadius * Math.cos(theta));
y[0] = (int) (getHeight() / 2 + startRadius * Math.sin(theta));
x[1] = (int) (getWidth() / 2 + endRadius * Math.cos(theta));
y[1] = (int) (getHeight() / 2 + endRadius * Math.sin(theta));
}

// The Hours/Cardinals of the clock
/** Set Stroke sets the thickness of the cardinals and hands */
void drawCardinals(Graphics2D g) {
g.setStroke(new BasicStroke(9));
g.setColor(Color.black);

for (double theta = 0; theta < h =" 2" m =" 2" s =" 2" m_calendar =" (GregorianCalendar)">>>>>. Second AnalogClockView .java
package utils;

import java.awt.Toolkit;

import javax.swing.JFrame;

public class AnalogClockView extends JFrame {

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300, 400);
frame.setTitle("My Java Project Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage("Clock.gif"));
AnalogClock m_AnalogClock = new AnalogClock();
frame.add(m_AnalogClock);
frame.setVisible(true);
}
}
Then if you have 2 class.You can run AnalogClockView.java

Output as below...


Analog Clock

Analog Clock


Tag: study code program java

Selection Sort

package utils;

public class SelectionSort {
public static void main(String a[]) {
String array[] = { "S", "D", "A", "B", "Z", "M", "O", "L", "H", "Y" };
sort(array, array.length);

System.out.println("Data items in ascending order");
for (int i = 0; i <>
System.out.print(array[i] + " ");
}

public static void sort(String array[], int n) {
for (int x = 0; x <>
int min = x;
for (int y = x; y <>
if (array[min].compareTo(array[y]) > 0) {
min = y;
}
}
String temp = array[x];
array[x] = array[min];
array[min] = temp;
}
}
}

/**
########### OUTPUT ##################
Data items in ascending order
A B D H L M O S Y Z

*/
Tag: Study Code Program Java

Insertion Sort

package utils;

public class InsertionSort {

public static void main(String[] args) {
String[] array = { "S", "D", "A", "B", "Z", "M", "O", "L", "H", "Y" };
sort(array);
for (int i = 0; i <>
System.out.print(array[i] + " ");
}
System.out.println();
}

public static void sort(String[] array) {
int N = array.length;
for (int i = 1; i <>
for (int j = i; j > 0; j--)
if (array[j - 1].compareTo(array[j]) > 0)
change(array, j, j - 1);
else
break;
}

private static void change(Comparable[] a, int i, int j) {
Comparable swap = a[i];
a[i] = a[j];
a[j] = swap;
}

}

/**
######### OUTPUT ###############
A B D H L M O S Y Z

*/

Tag: Study Code Program Java