-
Bug
-
Resolution: Not an Issue
-
P4
-
None
-
1.2.2
-
x86
-
windows_98
Name: dbT83986 Date: 10/10/99
Example 3-1 from Graphic Java Vol. 2 demonstrates a Swing bug on at least Windows 98 and Windows 2000. To reproduce:
1) Start the applet (it will display a full-size image in a JScrollPane, with a slider to scale the image)
2) Scale the image small enough that the JScrollPane scrollbars disappear.
3) Now try scaling the image to another small size. The applet does NOT refresh itself, unless you force it to refresh by, for example, resizing the window.
4) If you scale the image to a size large enough that the scrollbars reappear, the applet will automatically refresh itself, so it is only when the image is smaller than the JScrollPane that the bug occurs.
Here's the sourcecode (from p. 90 of Geary):
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JApplet {
DefaultBoundedRangeModel model =
new DefaultBoundedRangeModel(100,0,0,100);
JSlider slider = new JSlider(model);
JLabel readOut = new JLabel("100%");
ImageIcon image = new ImageIcon("shortcake.jpg");
ImageView imageView = new ImageView(image, model);
public void init() {
Container contentPane = getContentPane();
JPanel panel = new JPanel();
panel.add(new JLabel("Set Image Size:"));
panel.add(slider);
panel.add(readOut);
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(imageView, BorderLayout.CENTER);
model.addChangeListener(new ReadOutSynchronizer());
}
class ReadOutSynchronizer implements ChangeListener {
public void stateChanged(ChangeEvent e) {
String s = Integer.toString(model.getValue());
readOut.setText(s + "%");
readOut.revalidate();
}
}
}
class ImageView extends JScrollPane {
JPanel panel = new JPanel();
Dimension originalSize = new Dimension();
Image originalImage;
ImageIcon icon;
public ImageView(ImageIcon icon, BoundedRangeModel model) {
panel.setLayout(new BorderLayout());
panel.add(new JLabel(icon));
this.icon = icon;
this.originalImage = icon.getImage();
setViewportView(panel);
model.addChangeListener(new ModelListener());
originalSize.width = icon.getIconWidth();
originalSize.height = icon.getIconHeight();
}
class ModelListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
BoundedRangeModel model =
(BoundedRangeModel)e.getSource();
if( ! model.getValueIsAdjusting()) {
int min = model.getMinimum(),
max = model.getMaximum(),
span = max - min,
value = model.getValue();
double multiplier = (double)value / (double)span;
multiplier = multiplier == 0.0 ?
0.01 : multiplier;
Image scaled = originalImage.getScaledInstance(
(int)(originalSize.width * multiplier),
(int)(originalSize.height * multiplier),
Image.SCALE_FAST);
icon.setImage(scaled);
panel.revalidate();
}
}
}
}
Here's the applet HTML file:
<title>Test</title>
<hr>
<applet code="Test.class" width=375 height=400>
</applet>
<hr>
The version of Java I'm using:
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)
The machine I'm using is a Dell Inspiron 3500 (although I strongly suspect this bug is platform independent).
(Review ID: 96311)
======================================================================