Guest User

Untitled

a guest
Jul 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. package test;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.WritableRaster;
  9. import java.util.Random;
  10.  
  11. public class SwingTest {
  12. BufferedImage image = new BufferedImage(200,200, BufferedImage.TYPE_INT_ARGB);
  13.  
  14. public static void main(String[] args){
  15. SwingUtilities.invokeLater(new Runnable(){
  16. public void run(){
  17. new SwingTest().init();
  18. }
  19. });
  20. }
  21.  
  22. private void init(){
  23. for(int x=0; x<image.getWidth(); x++)
  24. for(int y=0; y<image.getHeight(); y++)
  25. image.setRGB(x, y, 0xFFFFFFFF);
  26.  
  27. final JFrame frame = new JFrame();
  28.  
  29. frame.add(new ImageFrame(image));
  30.  
  31. frame.setPreferredSize(new Dimension(300,300));
  32. frame.setMinimumSize(new Dimension(300,300));
  33. frame.pack();
  34. frame.setVisible(true);
  35.  
  36. Timer timer = new Timer(0, new ActionListener(){
  37. public void actionPerformed(ActionEvent e) {
  38. frame.repaint();
  39. }
  40. });
  41.  
  42.  
  43. timer.start();
  44.  
  45. new Thread(new ImagePainter(image, 0,100,0,100, Color.RED)).start();
  46. new Thread(new ImagePainter(image, 100,200,0,100, Color.BLUE)).start();
  47. new Thread(new ImagePainter(image, 0,100,100,200, Color.GREEN)).start();
  48. }
  49. }
  50.  
  51. class ImageFrame extends JPanel{
  52. Image image;
  53.  
  54. ImageFrame(Image img){
  55. image = img;
  56. }
  57.  
  58. @Override
  59. public void paintComponent(Graphics g){
  60. super.paintComponent(g);
  61.  
  62. g.fillRect(0,0,getWidth(), getHeight());
  63.  
  64. g.drawImage(image, 25, 25, null);
  65. }
  66. }
  67.  
  68. class ImagePainter implements Runnable{
  69. BufferedImage image;
  70. int xMin, xMax;
  71. int yMin, yMax;
  72. Random rnd;
  73. Color color;
  74.  
  75. ImagePainter(BufferedImage img, int xMin, int xMax, int yMin, int yMax, Color c){
  76. image = img;
  77. this.xMin = xMin;
  78. this.xMax = xMax;
  79. this.yMin = yMin;
  80. this.yMax = yMax;
  81. rnd = new Random();
  82. color = c;
  83. }
  84.  
  85. public void run(){
  86. while(true){
  87. try {
  88. Thread.sleep((long)(Math.random() * 100));
  89. } catch (InterruptedException e) {
  90. e.printStackTrace();
  91. return;
  92. }
  93.  
  94. int x = rnd.nextInt(xMax - xMin) + xMin;
  95. int y = rnd.nextInt(yMax - yMin) + yMin;
  96.  
  97. image.setRGB(x, y, color.getRGB());
  98. }
  99. }
  100. }
Add Comment
Please, Sign In to add comment