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.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 -= 1;
cy -= 1;
}
}

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);

}

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));
pack();
this.setResizable(false);
bi = new BufferedImage(mican.getHeight(), mican.getWidth(), BufferedImage.TYPE_INT_RGB);
g = bi.createGraphics();
mt = new Timer();
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.

2
Contributors
9
Replies
31
Views
4 Years
Discussion Span
Last Post by untio

Mixing Swing and AWT like that is always risky. Maybe try using a JPanel instead of a Canvas?
YOu may also find some useful info on which methods to call and which to use with Swing here. If you use just Swing conponents you have double-buffering as standard, which may allow you to simplify your code.

Hi,

First, thanks James.

I have tried changing the code to use only awt. Another day I will try with swing.

The problem persist. The only diference is that now the extra space is white. With the old version it was grey. Here is the current code:

``````import java.awt.Frame;
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.Timer;
import java.awt.BasicStroke;

public class SimpleClock extends Frame {

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 -= 1;
cy -= 1;
}
}

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);

}

@Override
public void run(){
mican.repaint();
}
}

private class MiCanvas extends Canvas{
@Override
public void paint(Graphics gr){
Graphics2D g2 = (Graphics2D) gr;
dibujaImagen();
g2.drawImage(bi, 0, 0, null);
}
@Override
public void update(Graphics g2) {
paint(g2);
}
}

private SimpleClock() {
super("A simple clock:");
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
setLocation(50, 50);
mican = new MiCanvas();
mican.setPreferredSize(new Dimension(401, 401));
pack();
setResizable(false);
bi = new BufferedImage(mican.getHeight(), mican.getWidth(), BufferedImage.TYPE_INT_RGB);
g = bi.createGraphics();
mt = new Timer();
setVisible(true);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
new SimpleClock();

}

}
``````

Thanks once more.

It's more than 15 years since I last used AWT rather than Swing, so any experience I may have had with it is horribly out of date, and probably no use to you. However, it would not surprise me to learn that there are problems under Windows 8 with a Java GUI that was superceeded in 1998.

Well,

Another day I will try to write the swing version.

May be you are right (I think so). May be in the recent JVM the use of awt is superceeded.

Thanks,

Cheers.

AWT is still officially supported in every version of the JVM, and it will be "forever". Swing is built on top of many pieces of AWT, so if AWT stopped working, Swing would stop as well. All I was saying is that there may be some bugs in the Windows 8 environment that haven't been identified and/or fixed yet if they only affect AWT but not Swing.
It's not hard to move to Swing, and you will benefit from a vastly superior GUI toolset, so don't leave it too long!
Cheers
J

Hi James,

you were right. I have writen the swing version (or so I believe) and it runs nice.

This is the new code (with a lot of copy and paste):

``````import javax.swing.JFrame;
import javax.swing.JPanel;

import java.util.GregorianCalendar;
import java.util.Timer;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class SimpleClock2 extends JFrame{

private MiPanel jp;
private Timer mt;

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 -= 1;
cy -= 1;
}
}

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));
}

@Override
public void run(){
jp.repaint();
}
}

private class MiPanel extends JPanel {
public MiPanel(){
super();

}
@Override
public void paint(Graphics gr){
Graphics2D g = (Graphics2D) gr;
g.setBackground(new Color(255, 255, 255));
g.clearRect(0, 0, getWidth(), getHeight());
dibujaCirculos(g);
dibujaAgujas(g);

}

}

SimpleClock2() {
super("The second simple clock");
setDefaultCloseOperation(EXIT_ON_CLOSE);
jp = new MiPanel();
jp.setPreferredSize(new Dimension(401, 401));
pack();
mt = new Timer();

setVisible(true);

}

public static void main(String[] args) {
// TODO Auto-generated method stub
new SimpleClock2();
}

}
``````

Thanks once more and my best wishes.

Edited by untio

Excellent! Thanks for the feedback.
J

Hi once more.

The problem is not solved at all. It seemed to work in the swing version because into it there is not this line:

setResizable(false);

When I add this line to the swing version the behaviour is the same.

And, when I comment the line in the first version it runs nice.

Is there another method to achieve this result?

Thanks once more.

Hi once more,

The problem solves writing setResizable(false) before pack().

And this solution works in the two versions (awt and swing).

Thanks once more.