Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.awt.image.WritableRaster;
- import java.util.Random;
- public class SwingTest {
- BufferedImage image = new BufferedImage(200,200, BufferedImage.TYPE_INT_ARGB);
- public static void main(String[] args){
- SwingUtilities.invokeLater(new Runnable(){
- public void run(){
- new SwingTest().init();
- }
- });
- }
- private void init(){
- for(int x=0; x<image.getWidth(); x++)
- for(int y=0; y<image.getHeight(); y++)
- image.setRGB(x, y, 0xFFFFFFFF);
- final JFrame frame = new JFrame();
- frame.add(new ImageFrame(image));
- frame.setPreferredSize(new Dimension(300,300));
- frame.setMinimumSize(new Dimension(300,300));
- frame.pack();
- frame.setVisible(true);
- Timer timer = new Timer(0, new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- frame.repaint();
- }
- });
- timer.start();
- new Thread(new ImagePainter(image, 0,100,0,100, Color.RED)).start();
- new Thread(new ImagePainter(image, 100,200,0,100, Color.BLUE)).start();
- new Thread(new ImagePainter(image, 0,100,100,200, Color.GREEN)).start();
- }
- }
- class ImageFrame extends JPanel{
- Image image;
- ImageFrame(Image img){
- image = img;
- }
- @Override
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- g.fillRect(0,0,getWidth(), getHeight());
- g.drawImage(image, 25, 25, null);
- }
- }
- class ImagePainter implements Runnable{
- BufferedImage image;
- int xMin, xMax;
- int yMin, yMax;
- Random rnd;
- Color color;
- ImagePainter(BufferedImage img, int xMin, int xMax, int yMin, int yMax, Color c){
- image = img;
- this.xMin = xMin;
- this.xMax = xMax;
- this.yMin = yMin;
- this.yMax = yMax;
- rnd = new Random();
- color = c;
- }
- public void run(){
- while(true){
- try {
- Thread.sleep((long)(Math.random() * 100));
- } catch (InterruptedException e) {
- e.printStackTrace();
- return;
- }
- int x = rnd.nextInt(xMax - xMin) + xMin;
- int y = rnd.nextInt(yMax - yMin) + yMin;
- image.setRGB(x, y, color.getRGB());
- }
- }
- }
Add Comment
Please, Sign In to add comment