Advertisement
TrodelHD

dasfdsgsg

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