How to see lines drawn in real time. Java Swing

Snibb :

I have a small program that outputs patterns. Id like to watch the lines being drawn in real time rather than just seeing the end program output, like mine. My end result would be to produce a real time kaleidoscope effect. How would I go about this? ( Just the real time bit )

Thank you.

public void paintComponent(Graphics myPen) {
    super.paintComponent(myPen);
    myPen.setColor(Color.red);
    int increment = 1;
    int count = 0;
    while (count < 5) {
        int x = 0;
        int y = 0;
        while (x < 400)

        {
            myPen.drawLine(200, 200, x, y);
            x = x + increment;
        }
        while (y < 400) {
            myPen.drawLine(200, 200, x, y);
            y = y + increment;
        }
        while (x > 0) {
            myPen.drawLine(200, 200, x, y);
            x = x - increment;
        }
        while (y > 0) {
            myPen.drawLine(200, 200, x, y);
            y = y - increment;
        }
        if (count == 1) {
            myPen.setColor(Color.blue);
        }
        if (count == 2) {
            myPen.setColor(Color.green);
        }
        if (count == 3) {
            myPen.setColor(Color.white);
        }
        if (count == 4) {
            myPen.setColor(Color.magenta);
        }
        if (count == 5) {
            myPen.setColor(Color.yellow);
        }
        increment++;
        count++;
    }
}

}

Hovercraft Full Of Eels :

What you are trying to do is to animate the GUI drawing, incrementally updating the image. There are several steps involved in solving this

  • Use an animation or "game" loop to drive the animation, and this is most easily achieved using a Swing Timer
  • Inside the animation loop, draw onto a BufferedImage, and call repaint()
  • Draw that image within a paintComponent method using the Graphics#drawImage(...) method.

Simple example:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class SimpleAnimation extends JPanel {
    private static final int IMG_W = 800;
    private static final int IMG_H = IMG_W;
    private static final int TIMER_DELAY = 40;
    public static final Stroke STROKE = new BasicStroke(6f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    public static final Color DRAW_COLOR = Color.RED;
    public static final double DELTA = 8;
    private BufferedImage img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
    private Timer animationTimer = null;
    private int myX = 0;
    private int myY = 0;

    public SimpleAnimation() {
        setBackground(Color.WHITE);
        JButton drawButton = new JButton("Draw!");
        drawButton.addActionListener(e -> draw());
        add(drawButton);
    }

    private void draw() {
        // if timer currently running, stop it
        if (animationTimer != null && animationTimer.isRunning()) {
            animationTimer.stop();
        }
        myX = 0;
        myY = 0;
        img = new BufferedImage(IMG_W, IMG_H, BufferedImage.TYPE_INT_ARGB);
        animationTimer = new Timer(TIMER_DELAY, new TimerListener());
        animationTimer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            g.drawImage(img, 0, 0, this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(IMG_W, IMG_H);
    }

    private class TimerListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (img == null) {
                return;
            }
            Graphics2D g2 = img.createGraphics();
            g2.setStroke(STROKE);
            g2.setColor(DRAW_COLOR);
            int x = myX + (int) (DELTA * Math.random());
            int y = myY + (int) (DELTA * Math.random());
            g2.drawLine(x, y, myX, myY);
            g2.dispose();
            myX = x;
            myY = y;

            if (myX > IMG_W || myY > IMG_H) {
                ((Timer) e.getSource()).stop();
            }
            repaint();
        }
    }   

    private static void createAndShowGui() {
        SimpleAnimation mainPanel = new SimpleAnimation();

        JFrame frame = new JFrame("SimpleAnimation");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415825&siteId=1