|
Description
|
I have the following source code to consider:
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.UIManager;
public class vtcSlider extends JComponent
{
public vtcSlider()
{
this(0, 0, 100);
}
public vtcSlider(int orientation)
{
this(orientation, 0, 100);
}
public vtcSlider(int min, int max)
{
this(0, min, max);
}
public vtcSlider(int orientation, int min, int max)
{
super();
_slider = new JSlider(orientation);
_icon = UIManager.getIcon("Slider.horizontalThumbIcon");
setLayout(null);
}
public void paint(Graphics g)
{
_icon.paintIcon(_slider, g, 0, 0);
// Why can't I do the following instead?
// _icon.paintIcon(this, g, 0, 0);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
vtcSlider slider = new vtcSlider();
frame.getContentPane().add(slider);
frame.pack();
frame.setVisible(true);
}
//
// Public data member section
//
//
// Private data member section
//
private JSlider
_slider;
private Icon
_icon;
}
What I wanted to do is to reuse the HorizontalSliderThumbIcon class
from one of the xxxIconFactory (xxx can be Metal, Motif, or Windows).
I can get an instance of this class by using the UIManager.getIcon(String)
method. However, to draw this icon I would have to pass an
instance of JSlider into the paintIcon(Component, Graphics, int, int)
method. It won't take my vtcSlider (for lack of a better name)
component. I have looked at the HorizontalSliderThumbIcon.paintIcon(...)
method and the reason it won't take my vtcSlider component is
because there is a line that casts a Component to JSlider.
However, the code in that method does not depend on whether a
Component is a JSlider customer or not. So why is the class casting
necessary?
I am using Windows NT 4.0 with patch 3.
(Review ID: 54979)
======================================================================
Posted Date : 2005-09-19 16:25:28.0
|