Advertisement
TrodelHD

MYPANEL

Mar 9th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. package Spiel;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Point;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseMotionAdapter;
  11. import java.util.Timer;
  12. import java.util.TimerTask;
  13.  
  14. import javax.swing.JButton;
  15. import javax.swing.JColorChooser;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.JSlider;
  19. import javax.swing.JTextArea;
  20. import javax.swing.event.ChangeEvent;
  21. import javax.swing.event.ChangeListener;
  22.  
  23. public class MyPanel extends JPanel {
  24.  
  25.  
  26. private Point pos;
  27. private JTextArea Time;
  28. private static int RADIUS = 10;
  29. private static final int DIAMETER = 2 * RADIUS;
  30. private JFrame f;
  31. private Color co = new Color(0,0,0);
  32. private JButton c;
  33. private int sek = 180;
  34. Timer timer = new Timer();
  35.  
  36.  
  37. private boolean aktiv = true;
  38. public MyPanel(JFrame f) {
  39.  
  40. pos = new Point(50,50);
  41. this.f = f;
  42. initcomps();
  43. updateColor();
  44. startZeit();
  45. addEventListener();
  46. repaint();
  47.  
  48.  
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. public void startZeit() {
  57.  
  58.  
  59.  
  60. TimerTask task = new TimerTask() {
  61.  
  62. @Override
  63. public void run() {
  64.  
  65.  
  66.  
  67. if(aktiv==true){
  68.  
  69. if(sek==0){
  70. timer.cancel();
  71.  
  72. }
  73.  
  74. String anzeige = Integer.toString(sek);
  75. Time.setText(anzeige);
  76. System.out.println(sek);
  77.  
  78. sek--;
  79. }else{
  80.  
  81. }
  82. }
  83. };
  84. timer.schedule(task, 0, 1000);
  85.  
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. private void updateColor() {
  98.  
  99. this.c.addActionListener(new ActionListener() {
  100.  
  101. @Override
  102. public void actionPerformed(ActionEvent e) {
  103. JColorChooser colorChooser = new JColorChooser();
  104. Color ccccccccccccc = colorChooser.showDialog(null, "color", Color.BLACK);
  105. co = ccccccccccccc;
  106. }
  107. });
  108.  
  109.  
  110. }
  111.  
  112.  
  113.  
  114. private void initcomps() {
  115.  
  116. this.setLayout(null);
  117.  
  118. this.Time = new JTextArea();
  119. this.add(Time);
  120. Time.setEditable(false);
  121. Font fo = new Font("Arial", Font.BOLD, 30);
  122. Time.setFont(fo);
  123. Time.setForeground(Color.BLACK);
  124. Time.setBounds(0, 0, 50, 50);
  125. Time.setText("180");
  126.  
  127. this.c = new JButton("color");
  128. this.add(c);
  129. c.setBounds(700,500 , 75, 50);
  130.  
  131.  
  132. JSlider Big;
  133.  
  134. Big = new JSlider(JSlider.HORIZONTAL,0, 15, 200);
  135. Big.setMajorTickSpacing(255);
  136. Big.setMinorTickSpacing(1);
  137. Big.setPaintTicks(true);
  138. Big.setPaintLabels(true);
  139. Big.setForeground(new Color(0, 0, 0));
  140. Big.setBounds(700, 100, 400, 40);
  141.  
  142. Big.addChangeListener(new ChangeListener() {
  143.  
  144. @Override
  145. public void stateChanged(ChangeEvent e) {
  146.  
  147. int r = Big.getValue();
  148.  
  149. RADIUS = r;
  150. }
  151. });
  152.  
  153. Big.setVisible(false);
  154. Big.setPaintLabels(true);
  155. Big.setPaintTicks(false);
  156.  
  157.  
  158.  
  159.  
  160. JTextArea LOG = new JTextArea();
  161. this.add(LOG);
  162. LOG.setEditable(false);
  163. Font ff = new Font("Arial", Font.BOLD, 20);
  164. LOG.setFont(ff);
  165. LOG.setForeground(Color.black);
  166. LOG.setBounds(500, 0, 300, 500);
  167. LOG.setText("Test");
  168. repaint();
  169.  
  170.  
  171. this.setSize(810,610);
  172. Thread thread = new Thread(){
  173. public void run(){
  174. try {sleep(10);} catch (Exception e) {}
  175. f.setSize(800,600);
  176. this.interrupt();
  177. }
  178. };
  179. thread.start();
  180.  
  181.  
  182. }
  183.  
  184. private void addEventListener() {
  185.  
  186. addMouseMotionListener(new MouseMotionAdapter() {
  187.  
  188. public void mouseMoved(MouseEvent e) {
  189. updatepos(e);
  190. }
  191.  
  192. private void updatepos(MouseEvent e) {
  193.  
  194. pos.setLocation(e.getPoint());
  195. repaint();
  196.  
  197. }
  198.  
  199. });
  200.  
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210. @Override
  211. protected void paintComponent(Graphics g) {
  212.  
  213. g.setColor(Color.WHITE);
  214. g.fillRect(0, 0, getWidth(), getHeight());
  215.  
  216.  
  217. g.setColor(co);
  218. int x = pos.x - RADIUS;
  219. int y = pos.y - RADIUS;
  220.  
  221. g.fillOval(x, y, DIAMETER, DIAMETER);
  222.  
  223. }
  224.  
  225.  
  226.  
  227.  
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement