Hi,
First, thanks for reading this stuff.
I am learning java. I recognize that I am a beginner. Last weekend I have coded a clock. Sourcecode is:
import javax.swing.JFrame;
import java.util.GregorianCalendar;
import java.awt.image.BufferedImage;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.BasicStroke;
public class SimpleClock extends JFrame {
private BufferedImage bi;
private Graphics2D g;
private Timer mt;
private MiCanvas mican;
private void dibujaAgujas(Graphics2D g) {
GregorianCalendar cal = new GregorianCalendar();
int seg, x;
double cx,cy, an;
seg = cal.get(GregorianCalendar.SECOND);
seg *= 6;
seg += 270;
an = (seg / 180.0) * Math.PI;
cy = Math.sin(an) * 170.0;
cx = Math.cos(an) * 170.0;
cy += 201;
cx += 201;
g.setColor(new Color(255, 0, 0));
g.setStroke(new BasicStroke(2));
g.drawLine(201, 201, (int) cx, (int) cy);
seg = cal.get(GregorianCalendar.MINUTE);
x = seg;
seg *= 6;
seg += 270;
an = (seg / 180.0) * Math.PI;
cy = Math.sin(an) * 155.0;
cx = Math.cos(an) * 155.0;
cy += 201;
cx += 201;
g.setColor(new Color(0, 255, 0));
g.setStroke(new BasicStroke(5));
g.drawLine(201, 201, (int) cx, (int) cy);
seg = cal.get(GregorianCalendar.HOUR);
seg *= 30;
seg += 270;
an = (seg / 180.0) * Math.PI;
an += (x / 2.0) / 180.0 * Math.PI;
cy = Math.sin(an) * 145.0;
cx = Math.cos(an) * 145.0;
cy += 201;
cx += 201;
g.setColor(new Color(0, 0, 255));
g.setStroke(new BasicStroke(8));
g.drawLine(201, 201, (int) cx, (int) cy);
}
private void dibujaCirculosUnaVez(Graphics2D g, int incr, int radi, Color col) {
int x;
double an, cx, cy;
for(x = 0; x < 360; x += incr) {
an = (x / 180.0) * Math.PI;
cy = Math.sin(an) * 185.0;
cx = Math.cos(an) * 185.0;
g.setColor(col);
g.setBackground(col);
cx += 201.0;
cy += 201.0;
cx -= radi / 2.0;
cy -= radi / 2.0;
cx -= 1;
cy -= 1;
g.fillOval((int) cx , (int) cy, radi, radi);
}
}
private void dibujaCirculos(Graphics2D g) {
dibujaCirculosUnaVez(g, 6, 7, new Color(255, 0, 0));
dibujaCirculosUnaVez(g, 30, 11, new Color(0, 255, 0));
dibujaCirculosUnaVez(g, 90, 15, new Color(0, 0, 255));
}
private void dibujaImagen() {
g.setBackground(new Color(255, 255, 255));
g.clearRect(0, 0, bi.getWidth(), bi.getHeight());
dibujaCirculos(g);
dibujaAgujas(g);
}
private class MiTimerTask extends TimerTask {
public void run(){
mican.repaint();
}
}
private class MiCanvas extends Canvas{
public void paint(Graphics gr){
Graphics2D g2 = (Graphics2D) gr;
dibujaImagen();
g2.drawImage(bi, 0, 0, null);
}
public void update(Graphics g2) {
paint(g2);
}
}
private SimpleClock() {
super("A simple clock:");
setDefaultCloseOperation(EXIT_ON_CLOSE);
mican = new MiCanvas();
mican.setPreferredSize(new Dimension(401, 401));
add(mican);
pack();
this.setResizable(false);
bi = new BufferedImage(mican.getHeight(), mican.getWidth(), BufferedImage.TYPE_INT_RGB);
g = bi.createGraphics();
mt = new Timer();
mt.schedule(new MiTimerTask(), 0, 50);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new SimpleClock();
}
}
I have developped and coded it with linux ubuntu. On it the code runs right.
I have tested with windows xp an the code runs right.
But with windows 8 the behaviour is not fully right. The frame does not adjust to the canvas. There are two margins (right and below the canvas).
And my question is: why. Is it a coding error or I use a deprecated call in last java version.
Once more. Thanks in advance.