Changing Swing Component L&F
Hey
I am programming a game that takes place in the Middle Age, and to protect the player's suspension of disbelief, I'd like the few Swing components I use to look like they are pieces of wood.
I use a JScrollPane to scroll on the game's 2D viewport. I was thinking of getting the Graphics of the JScrollBars and repaint them, but it is not performance efficient.
Does anybody know how I could put my own pictures instead of the default tabs?
Thanks, Guillaume
[495 byte] By [
gulm.org] at [2007-9-27 21:35:40]

This is a sample of a self painted JScrollBar
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;
class TinySp extends JScrollBar
{
public TinySp()
{
super.setUI(new myCustomScrollBarUI());
setPreferredSize(new Dimension(9,1));
setOpaque(false);
}
}
class myCustomScrollBarUI extends BasicScrollBarUI
{
public void configureScrollBarColors()
{
super.configureScrollBarColors();
}
public Dimension getMaximumThumbSize()
{
return(new Dimension(3,10));
}
protected void paintTrack(Graphics g, JComponent c, Rectangle trec)
{
g.setColor(Color.lightGray);
g.fillRect(trec.width/2,trec.y,1,trec.height);
}
protected void paintThumb(Graphics g, JComponent c, Rectangle trec)
{
g.setColor(Color.blue);
g.fillRect(3,trec.y,3,trec.height);
}
protected JButton createDecreaseButton(int orientation)
{
AbstractButton a1 = new myButton(1);
return((JButton)a1);
}
protected JButton createIncreaseButton(int orientation)
{
AbstractButton a2 = new myButton(0);
return((JButton)a2);
}
class myButton extends JButton
{
int d = 0;
public myButton(int i)
{
super();
d = i;
setOpaque(false);
setPreferredSize(new Dimension(9,9));
setBorderPainted(false);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.black);
if (d == 1)
{
g2.drawLine(4,1,1,6);
g2.drawLine(4,1,7,6);
g2.setColor(Color.lightGray);
g2.fillRect(4,7,1,5);
}
else
{
g2.drawLine(4,getHeight()-2,1,getHeight()-7);
g2.drawLine(4,getHeight()-2,7,getHeight()-7);
g2.setColor(Color.lightGray);
g2.fillRect(4,getHeight()-12,1,5);
}
g2.dispose();
}
}
}
Noah