Can anyone tell me why the following two programs (test.java and
test1.java) produce different results.
I feel that they should display a list of y co-ordinates down the center
of the window. The only difference is that one uses a BufferedImage to
create the display and then copies it to the screen and the other writes
directly to the screen. I feel that the one that writes directly to the
screen is correct. It also seems that if I remove the call to
translate() and do the co-oridinate translation myself that the
BufferedImage version works.
Am I missing something or is this a bug in the JDK.
Thanks
Bob Stafford
[EMAIL PROTECTED]
test.java
import java.awt.*;
import java.awt.geom.*;
import java.lang.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test extends JApplet
public void init()
{
// just set up some color stuff
setBackground(Color.white);
setForeground(Color.black);
}
public void paint(Graphics g)
{
// get size of JApplet
Dimension d = getSize();
// covert g to a Graphics2D
Graphics2D g2 = (Graphics2D) g;
// Translate co-oridnate system so that y=0 is aligned with
// the vertical center of the JApplet
g2.translate(0.0,d.height/2.0);
// Plot a list of y co-ordinates down the center of the JApplet
for (int y=-d.height/2;y<d.height/2;y+=10)
{
g2.drawString(String.valueOf(y),d.width/2,y);
}
}
public static void main(String[] args)
{
JFrame f=new JFrame("test");
JApplet applet= new test();
f.getContentPane().add(applet,"Center");
applet.init();
f.pack();
f.setSize(new Dimension(300,300));
f.show();
}
test1.java::
import java.awt.*;
import java.awt.geom.*;
import java.lang.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test1 extends JApplet
public void init()
{
setBackground(Color.white);
setForeground(Color.black);
}
public void paint(Graphics g)
{
Dimension d = getSize();
// Get a buffered image the same size as the JApplet
BufferedImage bimg=(BufferedImage) createImage(d.width,d.height)
// Get the graphics and convert it to a Graphics2D
Graphics2D g2 = (Graphics2D) bimg.createGraphics();
// Same as test.java
g2.translate(0.0,d.height/2.0);
for (int y=-d.height/2;y<d.height/2;y+=10)
{
g2.drawString(String.valueOf(y),d.width/2,y);
}
// Copy BufferedImage to JApplet
g.drawImage(bimg,0,0,this);
}
public static void main(String[] args)
{
JFrame f=new JFrame("test1");
JApplet applet= new test1();
f.getContentPane().add(applet,"Center");
applet.init();
f.pack();
f.setSize(new Dimension(300,300));
f.show();
}
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]