• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rendering JSlider Problem

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ranganath,

Have you tried calling JSliderObj.setOrientation() AFTER the JSliderObj.setUI(myUI) call? It should work!

Darrin
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried it out man. still its not working..
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want a solution, create a sample "working" program - one that we can
copy/paste/compile and run, so that we can see what you're seeing.

remove everything unrelated to the problem - listeners etc,
and use the code tags when you post the program.
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As each and every thing is interrelated i have pasted the entire code. I dont see any waste code out there..

Thank you
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I dont see any waste code out there..

OK, I'm always willing to learn something.
How does a mouseListener possibly affect the orientation when the program starts?

I won't read the code because it's not formatted, so I tried to run it to see
what it was doing, but could only compile as far as new Square().

If you want people to look at your problem, remove the barriers.
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok fine done.. i hope i have cleared all barriers. Please help in solving this...

Thank you
[ November 18, 2006: Message edited by: Ranganath Junpal ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic