Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Spiel;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionAdapter;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JButton;
- import javax.swing.JColorChooser;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JSlider;
- import javax.swing.JTextArea;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- public class MyPanel extends JPanel {
- private JSlider Big;
- private Graphics g;
- private boolean clicked;
- private Point pos;
- private JTextArea Time;
- private int RADIUS = 10;
- private JFrame f;
- private Color co = new Color(0,0,0);
- private JButton c;
- private int sek = 180;
- Timer timer = new Timer();
- private boolean aktiv = true;
- public MyPanel(JFrame f) {
- pos = new Point(50,50);
- this.f = f;
- initcomps();
- updateColor();
- startZeit();
- addEventListener();
- repaint();
- }
- public void startZeit() {
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- if(aktiv==true){
- if(sek==0){
- timer.cancel();
- }
- String anzeige = Integer.toString(sek);
- Time.setText(anzeige);
- System.out.println(sek);
- sek--;
- }else{
- }
- }
- };
- timer.schedule(task, 0, 1000);
- }
- private void updateColor() {
- this.c.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JColorChooser colorChooser = new JColorChooser();
- Color ccccccccccccc = colorChooser.showDialog(null, "color", Color.BLACK);
- co = ccccccccccccc;
- }
- });
- }
- private void initcomps() {
- this.setLayout(null);
- this.Time = new JTextArea();
- this.add(Time);
- Time.setEditable(false);
- Font fo = new Font("Arial", Font.BOLD, 30);
- Time.setFont(fo);
- Time.setForeground(Color.BLACK);
- Time.setBounds(0, 0, 50, 50);
- Time.setText("180");
- this.c = new JButton("color");
- this.add(c);
- c.setBounds(700,500 , 75, 50);
- Big = new JSlider(JSlider.HORIZONTAL,1, 20, 10);
- this.add(Big);
- Big.setMajorTickSpacing(19);
- Big.setMinorTickSpacing(1);
- Big.setPaintTicks(true);
- Big.setPaintLabels(true);
- Big.setForeground(new Color(0, 0, 0));
- Big.setBounds(500, 500, 190, 40);
- Big.addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent e) {
- int r = Big.getValue();
- RADIUS = r;
- }
- });
- Big.setVisible(true);
- Big.setPaintLabels(true);
- Big.setPaintTicks(false);
- JTextArea LOG = new JTextArea();
- this.add(LOG);
- LOG.setEditable(false);
- Font ff = new Font("Arial", Font.BOLD, 20);
- LOG.setFont(ff);
- LOG.setForeground(Color.black);
- LOG.setBounds(500, 0, 300, 500);
- LOG.setText("Test");
- repaint();
- this.setSize(810,610);
- Thread thread = new Thread(){
- public void run(){
- try {sleep(10);} catch (Exception e) {}
- f.setSize(800,600);
- this.interrupt();
- }
- };
- thread.start();
- }
- private void addEventListener() {
- addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent e) {
- updatepos(e);
- }
- private void updatepos(MouseEvent e) {
- pos.setLocation(e.getPoint());
- repaint();
- }
- });
- addMouseListener(new MouseListener() {
- @Override
- public void mouseReleased(MouseEvent e) {
- clicked = false;
- }
- @Override
- public void mousePressed(MouseEvent e) {
- clicked = true;
- paintthing(e.getX(),e.getY());
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- });
- }
- @Override
- protected void paintComponent(Graphics g) {
- g.setColor(Color.WHITE);
- g.fillRect(0, 0, getWidth(), getHeight());
- g.setColor(co);
- int x = pos.x - RADIUS;
- int y = pos.y - RADIUS;
- int DIAMETER = 2 * RADIUS;
- g.fillOval(x, y, DIAMETER, DIAMETER);
- this.g= g;
- }
- private void paintthing(int x, int y) {
- Thread thread = new Thread(){
- public void run(){
- while(clicked) {
- int ra = Big.getValue();
- ra = ra +20;
- int dia = ra * 2;
- g.setColor(co);
- g.fillOval(pos.x - ra, pos.y -ra , dia, dia);
- repaint();
- }
- this.interrupt();
- }
- };
- thread.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement