I am trying to paint colored rectangles of equal width under a JSlider such that the tick marks separate colors. I am really close but cannot get it quite perfect. I have printed out a bunch of values and my issue is the slider's width is not the actual length of the sliding bar. The x position of the slider is not the starting place of the bar either. Here is my code setting my Colored Rectangles bounds in terms of the sliders position.
for(int i = 0; i < Global.emSpectrum.length; i++) //emSpectrum.length is the number of colored rectangles
{
emSpectrum.get(i).setColorRect(Global.emSpectrum[i], 13 + i * (int)((this.slider.getWidth())/Global.emSpectrum.length), //13 lines up the first color under the bar
this.slider.getY() + this.slider.getHeight()/2, (int)((this.slider.getWidth())/Global.emSpectrum.length),
(int)(Global.rectHeight * getHeight()));
}
Is there just a better way to go about this?
Thanks!
Yep, a decent solution is to use a Dictionary such as a HashTable<Integer, JLabel>
and fill it with JLabels that hold ImageIcons of your color rectangles, using an Integer that corresponds with the appropriate location on the JSlider. For example, my SSCCE:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.swing.*;
public class SliderEg extends JPanel {
public static final Color[] COLORS = { Color.red, Color.orange,
Color.yellow, Color.green, Color.blue, Color.cyan};
private static final int BI_W = 30;
private static final int BI_H = 10;
private JSlider slider = new JSlider(0, 100, 0);
public SliderEg() {
int majorSpacing = slider.getMaximum() / (COLORS.length - 1);
Dictionary<Integer, JLabel> dictionary = new Hashtable<Integer, JLabel>();
slider.setMajorTickSpacing(majorSpacing);
slider.setPaintLabels(true);
slider.setPaintTicks(true);
slider.setSnapToTicks(true);
for (int i = 0; i < COLORS.length; i++) {
ImageIcon icon = createColorIcon(COLORS[i]);
JLabel label = new JLabel(icon);
int key = i * majorSpacing;
dictionary.put(key, label);
}
slider.setLabelTable(dictionary);
setLayout(new BorderLayout());
add(slider, BorderLayout.CENTER);
}
private ImageIcon createColorIcon(Color color) {
BufferedImage img = new BufferedImage(BI_W, BI_H,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, BI_W, BI_H);
g.dispose();
return new ImageIcon(img);
}
private static void createAndShowGui() {
SliderEg mainPanel = new SliderEg();
JFrame frame = new JFrame("SliderEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.