如何查看实时绘制的线. Java秋千 [英] How to see lines drawn in real time. Java Swing

查看:78
本文介绍了如何查看实时绘制的线. Java秋千的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出图案的小程序.我想实时观察画出的线条,而不是像我一样看到最终程序的输出.我的最终结果将是产生实时的万花筒效果.我将如何处理? (只是实时位)

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 )

谢谢.

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++;
    }
}

}

推荐答案

您要尝试的是对GUI图形进行动画动画,以增量方式更新图像.解决这个问题涉及几个步骤

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.

简单的例子:

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());
    }
}

这篇关于如何查看实时绘制的线. Java秋千的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆