Advertisement
TrodelHD

paint

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