Advertisement
sergAccount

Untitled

Dec 20th, 2020
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package app13;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import javax.swing.JPanel;
  13. import javax.swing.Timer;
  14.  
  15. public class DrawPanel extends JPanel implements ActionListener {
  16.     // переменная типа таймер - для выполнения период-ких действий
  17.     Timer drawTimer;
  18.     int val;
  19.     //
  20.     public DrawPanel() {
  21.         // создаем объект типа Timer
  22.         drawTimer = new Timer(200, this);
  23.         // запуск таймера - используем метод start
  24.         drawTimer.start();
  25.     }
  26.     // данный метод будет вызваться периодически (через опред интерфал времени)
  27.     @Override
  28.     public void actionPerformed(ActionEvent e) {
  29.         //System.out.println("actionPerformed!!!");
  30.         // для перерисовки экрана вызываем repaint (приводит к вызову paintComponent)
  31.         val++;
  32.         if(val==256){
  33.             val=0;
  34.         }        
  35.         repaint();
  36.     }
  37.     // метод отвечает за отрисовку разл объектов  
  38.     public void paintComponent(Graphics g) {
  39.         super.paintComponent(g);
  40.         //g.setColor(Color.green);      
  41.         for (int i = 0; i <= 255; i++) {
  42.              //System.out.println("i=" + i);
  43.              g.setColor(new Color(0, 0, (i*val)%255));
  44.              //g.setColor(new Color((i*val)%255, 0, 0));
  45.              //fill rect
  46.              //g.fillRect(10, 10, 200, 200);
  47.              g.drawRect(250-i/2, 250 - i/2, i, i);
  48.         }
  49.     }        
  50.     // запуск таймера
  51.     public void startAnimation(){
  52.         if(!drawTimer.isRunning()){
  53.             drawTimer.start();
  54.         }else{
  55.             System.out.println("Таймер уже запущен!");
  56.         }
  57.     }
  58.     // остановка таймера
  59.     public void stopAnimation(){
  60.         drawTimer.stop();
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement