Advertisement
TrodelHD

ma2

Mar 9th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 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 int RADIUS = 10;
  29. private JFrame f;
  30. private Color co = new Color(0,0,0);
  31. private JButton c;
  32. private int sek = 180;
  33. Timer timer = new Timer();
  34.  
  35.  
  36. private boolean aktiv = true;
  37. public MyPanel(JFrame f) {
  38.  
  39. pos = new Point(50,50);
  40. this.f = f;
  41. initcomps();
  42. updateColor();
  43. startZeit();
  44. addEventListener();
  45. repaint();
  46.  
  47.  
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. public void startZeit() {
  56.  
  57.  
  58.  
  59. TimerTask task = new TimerTask() {
  60.  
  61. @Override
  62. public void run() {
  63.  
  64.  
  65.  
  66. if(aktiv==true){
  67.  
  68. if(sek==0){
  69. timer.cancel();
  70.  
  71. }
  72.  
  73. String anzeige = Integer.toString(sek);
  74. Time.setText(anzeige);
  75. System.out.println(sek);
  76.  
  77. sek--;
  78. }else{
  79.  
  80. }
  81. }
  82. };
  83. timer.schedule(task, 0, 1000);
  84.  
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. private void updateColor() {
  97.  
  98. this.c.addActionListener(new ActionListener() {
  99.  
  100. @Override
  101. public void actionPerformed(ActionEvent e) {
  102. JColorChooser colorChooser = new JColorChooser();
  103. Color ccccccccccccc = colorChooser.showDialog(null, "color", Color.BLACK);
  104. co = ccccccccccccc;
  105. }
  106. });
  107.  
  108.  
  109. }
  110.  
  111.  
  112.  
  113. private void initcomps() {
  114.  
  115. this.setLayout(null);
  116.  
  117. this.Time = new JTextArea();
  118. this.add(Time);
  119. Time.setEditable(false);
  120. Font fo = new Font("Arial", Font.BOLD, 30);
  121. Time.setFont(fo);
  122. Time.setForeground(Color.BLACK);
  123. Time.setBounds(0, 0, 50, 50);
  124. Time.setText("180");
  125.  
  126. this.c = new JButton("color");
  127. this.add(c);
  128. c.setBounds(700,500 , 75, 50);
  129.  
  130.  
  131. JSlider Big;
  132.  
  133. Big = new JSlider(JSlider.HORIZONTAL,0, 20, 10);
  134. this.add(Big);
  135. Big.setMajorTickSpacing(20);
  136. Big.setMinorTickSpacing(1);
  137. Big.setPaintTicks(true);
  138. Big.setPaintLabels(true);
  139. Big.setForeground(new Color(0, 0, 0));
  140. Big.setBounds(500, 500, 190, 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(true);
  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.  
  222. int DIAMETER = 2 * RADIUS;
  223.  
  224. g.fillOval(x, y, DIAMETER, DIAMETER);
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement