Hi all,
Well, I was trying to render the JSlider to a different UI and I am facing a problem in the orientation of the JSlider. when i try to use the JSliderObj.setUI(myUI). The orientation is always HORIZONTAL even though the JSlider is initialised with JSlider.VERTICAL. But when i dont use JSliderObj.setUI(myUI); The JSlider is behaving as expected ie VERTICAL. The Code is as follows. Please let me know what needs to be done to solve this problem.
MyUI->ShapeSliderUI
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class ShapeSliderUI extends SliderUI //its extending the Component UI ie javax.swing.plaf.SliderUI
{
private Shape primaryShape;
private Shape secondaryShape;
private Color primaryColor = null;
private Color secondaryColor = null;
private int margin = 1;
private MouseInputListener mouseListener;
private ChangeListener changeListener;
private KeyListener keyListener;
public ShapeSliderUI()
{
}
public static ComponentUI createUI(JComponent c)
{
return new ShapeSliderUI();
}
public void installColors()
{
if (primaryShape == null)
{
primaryShape=makeSlide();
}
if (secondaryShape == null)
{
secondaryShape=new Square().getSquare2D(30, 50, 40, 150);
}
if (primaryColor == null)
primaryColor = Color.BLACK;
if (secondaryColor == null)
secondaryColor = Color.GRAY;
}
/**
* Build the 2D Slider
*/
protected Shape makeSlide()
{
return new Square().getSquare2D(30, 50, 40, 150);
}
public Dimension getPreferredSize(JComponent c)
{
JSlider slider = (JSlider) c;
int size = 16;
return new Dimension(size * slider.getMaximum() + (2 * margin),
size + 2 * margin);
}
public Dimension getMinimumSize(JComponent c)
{
JSlider slider = (JSlider) c;
int size = 10;
return new Dimension(size * slider.getMaximum() + (2 * margin),
size + 2 * margin);
}
public Dimension getMaximumSize(JComponent c)
{
JSlider slider = (JSlider) c;
int size = 200;
return new Dimension(size * slider.getMaximum() + (2 * margin),
size + 2 * margin);
}
protected int getSize(JSlider slider)
{
int size = (slider.getWidth() - (2 * margin)) / slider.getMaximum();
int alt = slider.getHeight() - (2 * margin);
if (alt < size)
{
size = alt;
}
return size;
}
public void paint(Graphics g1, JComponent c)
{
Graphics2D g = (Graphics2D) g1;
JSlider slider = (JSlider) c;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
BoundedRangeModel model = slider.getModel();
int slots = model.getMaximum();
int setting = model.getValue();
double size = getSize(slider);
double z = size / 100.0;
double iz = 1.0 / z;
g.translate(margin, margin);
boolean enabled = slider.isEnabled();
for (int i = 0; i < slots; i++)
{
g.scale(z, z);
paintShape(g, i < setting ? 1.0 : 0.0, enabled);
g.scale(iz, iz);
g.translate(size, 0);
}
}
/** Paint a shape. All translation and scaling are already taken care of. */
public void paintShape(Graphics2D g, double percent, boolean enabled)
{
Shape s = secondaryShape;
if (percent > 0.0)
{
s = primaryShape;
g.setColor(enabled ? primaryColor : secondaryColor);
}
else
{
if (!enabled)
return;
s = secondaryShape;
g.setColor(secondaryColor);
}
g.fill(s);
}
public Shape getPrimaryShape()
{
return primaryShape;
}
/** Shape should be centered in the rect (0,0,100,100). */
public void setPrimaryShape(Shape primaryShape)
{
this.primaryShape = primaryShape;
}
public Shape getSecondaryShape()
{
return secondaryShape;
}
/** Shape should be centered in the rect (0,0,100,100). */
public void setSecondaryShape(Shape secondaryShape)
{
this.secondaryShape = secondaryShape;
}
public Color getPrimaryColor()
{
return primaryColor;
}
public void setPrimaryColor(Color primaryColor)
{
this.primaryColor = primaryColor;
}
public Color getSecondaryColor()
{
return secondaryColor;
}
public void setSecondaryColor(Color secondaryColor)
{
this.secondaryColor = secondaryColor;
}
public int getMargin()
{
return margin;
}
public void setMargin(int margin)
{
this.margin = margin;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Main Program.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public class SliderTest
{
public SliderTest()
{
}
public static GridBagConstraints grid(int x, int y)
{
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(2, 2, 2, 2);
return gbc;
}
public static void main(
String args[]) throws Exception
{
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new GridBagLayout());
int y = 0;
JSlider s = new JSlider(JSlider.VERTICAL,0,20,0);
ShapeSliderUI ui=new ShapeSliderUI();
s.setUI(ui);
p.add(new JLabel("Enabled 2D Slider"), grid(0, y));
p.add(s, grid(1, y++));
f.getContentPane().add(p);
f.pack();
f.setVisible(true);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expecting a Speedy Reply
Thank you
Regards
[ November 18, 2006: Message edited by: Ranganath Junpal ]