Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pacote01;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.KeyEventDispatcher;
- import java.awt.KeyboardFocusManager;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferedImage;
- import java.util.Dictionary;
- import java.util.Hashtable;
- import javax.swing.ImageIcon;
- import javax.swing.JComponent;
- import javax.swing.JFrame;
- import static javax.swing.JFrame.EXIT_ON_CLOSE;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JSlider;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- public class NovoClass1 extends JFrame {
- private float imc = 0;
- JTextField campo = new JTextField();
- Slider1 sl = new Slider1(imc);
- public NovoClass1() {
- setSize(525, 300);
- add(monta());
- setLocationRelativeTo(null);
- setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- private JComponent monta() {
- JPanel jp = new JPanel();
- jp.add(campo);
- campo.setPreferredSize(new Dimension(100, 20));
- campo.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent e) {
- KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
- @Override
- public boolean dispatchKeyEvent(KeyEvent e) {
- if (e.getID() == KeyEvent.KEY_RELEASED && e.getKeyCode() == KeyEvent.VK_ENTER) {
- float valor1 = Float.valueOf(campo.getText());
- imc = valor1;
- System.out.println(imc);
- sl = new Slider1(imc);
- } else {
- }
- return false;
- }
- });
- }
- });
- jp.add(sl);
- return jp;
- }
- }
- //Classe do componente.
- class Slider1 extends JPanel {
- public static final Color[] COLORS = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue};
- private static final int COMPRIMENTO = 30;
- private static final int ALTURA = 10;
- public static final String[] VALORES = {"10", "20", "30", "40", "50"};
- private JSlider slider = new JSlider(0, 100, 0);
- public JTextField campo = new JTextField();
- public Slider1(float valor) {
- //converter valores
- float f = valor;
- int tr = (int) f;
- System.out.println("converteru: " + tr);
- //passar valor para o método.
- definePonteiro(tr);
- add(montaSlider());
- }
- private JComponent montaSlider() {
- JPanel jpSlider = new JPanel();
- int majorSpacing = slider.getMaximum() / (COLORS.length - 1);
- Dictionary<Integer, JLabel> dictionary = new Hashtable<>();
- 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);
- //aqui você vai adicionar o valor
- //corresponder a cor de mesmo indice
- label.setText(VALORES[i]);
- label.setForeground(Color.white);//altere a cor dos numeros como quiser aqui
- label.setHorizontalTextPosition(JLabel.CENTER);//centraliza o texto
- int key = i * majorSpacing;
- dictionary.put(key, label);
- }
- slider.setLabelTable(dictionary);
- jpSlider.add(slider, BorderLayout.CENTER);
- return jpSlider;
- }
- private ImageIcon createColorIcon(Color color) {
- BufferedImage img = new BufferedImage(COMPRIMENTO, ALTURA, BufferedImage.TYPE_INT_RGB);
- Graphics g = img.getGraphics();
- g.setColor(color);
- g.fillRect(0, 0, COMPRIMENTO, ALTURA);
- g.dispose();
- return new ImageIcon(img);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- NovoClass1 s = new NovoClass1();
- }
- });
- }
- public void definePonteiro(int valor) {
- //valor = Double.valueOf(campo.getText());
- //variavel que armazenará a posicao do knob
- int sliderPos = 0;
- if (valor <= 11.5) {
- sliderPos = 0;
- } else if (valor <= 20) {
- sliderPos = 25;
- } else if (valor <= 30) {
- sliderPos = 50;
- } else if (valor <= 40) {
- sliderPos = 75;
- } else if (valor <= 50) {
- sliderPos = 100;
- }
- slider.setValue(sliderPos);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement