Advertisement
DragonG1

Untitled

May 27th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. package pacote01;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.KeyEventDispatcher;
  8. import java.awt.KeyboardFocusManager;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.image.BufferedImage;
  13. import java.util.Dictionary;
  14. import java.util.Hashtable;
  15. import javax.swing.ImageIcon;
  16. import javax.swing.JComponent;
  17. import javax.swing.JFrame;
  18. import static javax.swing.JFrame.EXIT_ON_CLOSE;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.JSlider;
  22. import javax.swing.JTextField;
  23. import javax.swing.SwingUtilities;
  24.  
  25. public class NovoClass1 extends JFrame {
  26.  
  27. private float imc = 0;
  28. JTextField campo = new JTextField();
  29. Slider1 sl = new Slider1(imc);
  30.  
  31. public NovoClass1() {
  32. setSize(525, 300);
  33. add(monta());
  34. setLocationRelativeTo(null);
  35. setVisible(true);
  36. setDefaultCloseOperation(EXIT_ON_CLOSE);
  37. }
  38.  
  39. private JComponent monta() {
  40. JPanel jp = new JPanel();
  41. jp.add(campo);
  42. campo.setPreferredSize(new Dimension(100, 20));
  43.  
  44. campo.addActionListener(new ActionListener()
  45. {
  46. @Override
  47. public void actionPerformed(ActionEvent e) {
  48. KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
  49. @Override
  50. public boolean dispatchKeyEvent(KeyEvent e) {
  51. if (e.getID() == KeyEvent.KEY_RELEASED && e.getKeyCode() == KeyEvent.VK_ENTER) {
  52. float valor1 = Float.valueOf(campo.getText());
  53. imc = valor1;
  54. System.out.println(imc);
  55. sl = new Slider1(imc);
  56. } else {
  57.  
  58. }
  59. return false;
  60. }
  61. });
  62. }
  63. });
  64.  
  65. jp.add(sl);
  66.  
  67. return jp;
  68. }
  69. }
  70.  
  71. //Classe do componente.
  72. class Slider1 extends JPanel {
  73.  
  74. public static final Color[] COLORS = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue};
  75. private static final int COMPRIMENTO = 30;
  76. private static final int ALTURA = 10;
  77. public static final String[] VALORES = {"10", "20", "30", "40", "50"};
  78.  
  79. private JSlider slider = new JSlider(0, 100, 0);
  80. public JTextField campo = new JTextField();
  81.  
  82. public Slider1(float valor) {
  83. //converter valores
  84. float f = valor;
  85. int tr = (int) f;
  86. System.out.println("converteru: " + tr);
  87. //passar valor para o método.
  88. definePonteiro(tr);
  89.  
  90. add(montaSlider());
  91. }
  92.  
  93. private JComponent montaSlider() {
  94. JPanel jpSlider = new JPanel();
  95.  
  96. int majorSpacing = slider.getMaximum() / (COLORS.length - 1);
  97. Dictionary<Integer, JLabel> dictionary = new Hashtable<>();
  98. slider.setMajorTickSpacing(majorSpacing);
  99. slider.setPaintLabels(true);
  100. slider.setPaintTicks(true);
  101. slider.setSnapToTicks(true);
  102. for (int i = 0; i < COLORS.length; i++) {
  103. ImageIcon icon = createColorIcon(COLORS[i]);
  104. JLabel label = new JLabel(icon);
  105. //aqui você vai adicionar o valor
  106. //corresponder a cor de mesmo indice
  107. label.setText(VALORES[i]);
  108. label.setForeground(Color.white);//altere a cor dos numeros como quiser aqui
  109. label.setHorizontalTextPosition(JLabel.CENTER);//centraliza o texto
  110. int key = i * majorSpacing;
  111. dictionary.put(key, label);
  112. }
  113. slider.setLabelTable(dictionary);
  114. jpSlider.add(slider, BorderLayout.CENTER);
  115. return jpSlider;
  116. }
  117.  
  118. private ImageIcon createColorIcon(Color color) {
  119. BufferedImage img = new BufferedImage(COMPRIMENTO, ALTURA, BufferedImage.TYPE_INT_RGB);
  120. Graphics g = img.getGraphics();
  121. g.setColor(color);
  122. g.fillRect(0, 0, COMPRIMENTO, ALTURA);
  123. g.dispose();
  124. return new ImageIcon(img);
  125. }
  126.  
  127. public static void main(String[] args) {
  128. SwingUtilities.invokeLater(new Runnable() {
  129. @Override
  130. public void run() {
  131. NovoClass1 s = new NovoClass1();
  132. }
  133. });
  134. }
  135.  
  136. public void definePonteiro(int valor) {
  137.  
  138. //valor = Double.valueOf(campo.getText());
  139. //variavel que armazenará a posicao do knob
  140. int sliderPos = 0;
  141.  
  142. if (valor <= 11.5) {
  143. sliderPos = 0;
  144. } else if (valor <= 20) {
  145. sliderPos = 25;
  146. } else if (valor <= 30) {
  147. sliderPos = 50;
  148. } else if (valor <= 40) {
  149. sliderPos = 75;
  150. } else if (valor <= 50) {
  151. sliderPos = 100;
  152. }
  153. slider.setValue(sliderPos);
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement