-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.0, 1.1.2
-
x86, sparc
-
solaris_2.5.1, windows_nt
Name: wm7046 Date: 06/12/97
I have an applet, that I set the background color
to blue. I have an extended panel that overrides pdate(Graphics) to call paint(Graphics g) directly. paint(Graphics g) fills a rectangle (0, 0, 150, 200) with red and draws the current java version and vendor.
The problem:
If I set that background color of the applet to blue, and the
background color of my extended panel to green, I get the
following results. The paint still fills the red rectangle and draws the appropriate text but the unpainted portion (150, 0, 300, 200) is green. The problem is that nowhere in my extended panel am I filling the green color.
If you examine the source for java.awt.Component,
/*
* [ head comments deleted]
*/
/*
* @(#)Component.java 1.65 95/12/14 Arthur van Hoff
*
*/
you will find that update(Graphics g) does:
/**
* Updates the component. This method is called in
* response to a call to repaint. You can assume that
* the background is not cleared.
* @param g the specified Graphics window
* @see #paint
* @see #repaint
*/
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
paint(g);
}
and in
/*
* @(#)Component.java 1.145 97/05/04
*
* [header comments deleted]
*/
/**
* Updates the component. This method is called in
* response to a call to repaint. You can assume that
* the background is not cleared.
* @param g the specified Graphics window
* @see #paint
* @see #repaint
*/
public void update(Graphics g) {
if (! (peer instanceof java.awt.peer.LightweightPeer)) {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
}
paint(g);
}
So the issue is that overriding update(Graphics g) to call
paint(Graphics g) directly should prevent a background fill from ever taking place. In other words, the sample should not display any green in the extended panel. Instead, the applet should display a blue applet with a panel that has a only a red rectangle and text displayed.
Here is the Java code and the html file:
import java.awt.*;
import java.applet.*;
public class Sample extends Applet {
public void init() {
//set the applet color to blue
setBackground(Color.blue);
MyPanel p = new MyPanel();
p.setBackground(Color.green);
add(p);
}
}
class MyPanel extends Panel {
public MyPanel() {
super();
}
public void update(Graphics g) {
//do not fill with the background
//but call paint directly
paint(g);
}
public void paint(Graphics g) {
//fill a rectangle only on left side
//of the panel (0,0,150,200)
Color c = g.getColor();
g.setColor(Color.red);
g.fillRect(0, 0, 150, 200);
//draw the current java version in the panel
g.setColor(Color.black);
String version = System.getProperty("java.version");
String vendor = System.getProperty("java.vendor");
g.drawString("Java version: " + version, 10, 45);
g.drawString("Vendor: " + vendor, 10, 65);
g.setColor(c);
}
public Dimension preferredSize() {
return new Dimension(300, 200);
}
}
<HTML>
<HEAD>
<TITLE> A sample program </TITLE>
</HEAD>
<BODY>
<APPLET CODE="Sample.class" WIDTH=400 HEIGHT=300></APPLET>
</BODY>
</HTML>
System: Microsoft Windows NT 4.00.1381 Service Pack 2
Computer: Pentium Pro 200 with 64 Meg
Tried this test with various JDKs and browsers (JDK 1.02, Netscape 3.0 Gold, IE4.0, Symantec Cafe 1.51, and JDK 1.1.2E) all with the same results.
======================================================================