Advertisement
sergAccount

Untitled

Feb 22nd, 2021
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 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 javaapplication91;
  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 java.time.LocalDateTime;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15.  
  16. public class DrawPanel extends JPanel implements ActionListener {
  17.     // таймер    
  18.     Timer drawTimer;
  19.     // используется для отрисовки фигуры определенным
  20.     int val;
  21.     //
  22.     public DrawPanel() {
  23.         // создание таймера - определяем интервал времени и
  24.         // объект в котором определено периодическое действие
  25.         drawTimer = new Timer(200, this);
  26.         // запуск таймера
  27.         drawTimer.start();
  28.     }
  29.     // метод отвечает за отрисовку разл объектов
  30.     @Override
  31.     public void paintComponent(Graphics g){
  32.         super.paintComponent(g);
  33.         // устанавливаем текущ цвет
  34. //        g.setColor(Color.red);
  35. //        g.drawRect(10, 10, 500, 500);
  36.         for(int i = 0; i<=255; i++){
  37.             int colorPart = (i*val)%255; // % - остаток от деления
  38.             Color c = new Color(colorPart, 0, 0); // создаем цвет
  39.             g.setColor(c); // устанваливаем цвет
  40.             g.drawRect(250 - i/2, 250 - i/2, i, i);
  41.         }
  42.     }
  43.     @Override
  44.     public void actionPerformed(ActionEvent e) {
  45.         //
  46. //        System.out.println("DrawPanel.actionPerformed!");
  47. //        //выводит текущую дату и время
  48. //        LocalDateTime dt = LocalDateTime.now();
  49. //        System.out.println("actionPerformed.dt=" + dt);
  50.         val++; // 0<=val<=255
  51.         if(val==256){
  52.             val=0;
  53.         }      
  54.         repaint();
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement