どのようにリアルタイムで描かれた行を表示します。Javaのスイング

Snibb:

私は小さなプログラムの出力パターンことがあります。ラインを見るのが好きIdはちょうど私のように、最後のプログラムの出力を見てではなく、リアルタイムで描画されています。私の最終的な結果は、リアルタイム万華鏡の効果を生み出すことであろう。私はこれについてどのように行きますか?(ただ、リアルタイムビット)

ありがとうございました。

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の描画を。これを解決するに関与するいくつかのステップがあります。

  • アニメーションを駆動するためにアニメーションや「ゲーム」ループを使用し、これを最も簡単に使用して達成されたスイングタイマーを
  • アニメーションループの内部では、BufferedImageの、およびコールに描きます repaint()
  • 使用paintComponentメソッド内でその画像を描画するGraphics#drawImage(...)方法。

簡単な例:

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=478220&siteId=1