简体   繁体   English

图形列表2D

[英]List of graphics2D

Howcome this code below wont work?为什么下面的这段代码不起作用? I want to add new Ovals to the ArrayList every 200 ms and display them and run them one by one.我想每 200 毫秒向 ArrayList 添加新的椭圆并显示它们并一一运行它们。 It works fine when Im running one particle s.runner();当我运行一个粒子时它工作正常 s.runner(); but it doesnt seem to run all my particles.但它似乎并没有运行我所有的粒子。

MAIN:主要的:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.Timer;

public class ExempelGraphics extends JFrame implements ActionListener {
    Timer t;
    private int inc = 0;
    ArrayList<Surface> particle = new ArrayList<>();
    Surface s;

    public ExempelGraphics() {
        t = new Timer(10, this);
        t.start();
        s = new Surface(10, 10);
        initUI();
    }

    private void initUI() {
        add(s);
        setSize(350, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
//        s.runner();
        // add
        if (inc++ % 20 == 0) {
            particle.add(new Surface(10, 10));
        }

        // display
        for (int i = 0; i < particle.size(); i++) {
            Surface p = particle.get(i);
            p.runner();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ExempelGraphics ex = new ExempelGraphics();
                ex.setVisible(true);
            }
        });
    }
}

GRAPHICS:图形:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class Surface extends JPanel {
    private int locX = 0;
    private int locY = 0;

    public Surface(int locX, int locY) {
        this.locX = locX;
        this.locY = locY;
    }

    public void runner() {
        locX = locX + 1;
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.fillOval(locX, locY, 10, 10);
    }
}

I think that you're program structure is broken.我认为您的程序结构已损坏。 You should have only one JPanel here that does the drawing, that has its paintComponent overridden, and your Surface class should be a logical class and not a component class -- in other words, don't have it extend JPanel, and give it a public void draw(Graphics g) method where you draw the oval.您应该在这里只有一个 JPanel 来进行绘图,它的paintComponent 被覆盖,并且您的 Surface 类应该是一个逻辑类而不是组件类——换句话说,不要让它扩展 JPanel,并给它一个绘制椭圆的public void draw(Graphics g)方法。 Then have the drawing JPanel hold an ArrayList of these surfaces, and in the main JPanel's paintComponent method, iterate through the surfaces, calling each one's draw method.然后让绘图 JPanel 持有这些表面的 ArrayList,并在主 JPanel 的 paintComponent 方法中,遍历这些表面,调用每个表面的 draw 方法。

Also your Timer's delay is not realistic and is too small.此外,您的 Timer 延迟不现实且太小。 15 would be much more realistic. 15 会更现实。

Also, don't call repaint() from within surface, since that will generate too many repaint calls unnecessarily.另外,不要从表面内部调用repaint() ,因为这会产生太多不必要的重绘调用。 Instead call it from within the Timer's ActionListener after calling the runner methods on all the Surface objects.而是在所有 Surface 对象上调用 runner 方法后从 Timer 的 ActionListener 中调用它。

Also note that every time you add a component to a JFrame's contentPane in a default fashion, you cover up the previously added components.另请注意,每次以默认方式将组件添加到 JFrame 的 contentPane 时,都会覆盖先前添加的组件。 If you go by my recommendations above, this isn't an issue since you'd only be adding that single JPanel to it.如果您遵循我上面的建议,这不是问题,因为您只会向其中添加单个 JPanel。

For example:例如:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

@SuppressWarnings("serial")
public class ExampleGraphics2 extends JPanel {
    private static final int PREF_W = 650;
    private static final int PREF_H = 500;
    private static final int TIMER_DELAY = 20;
    private List<Surface> surfaces = new ArrayList<>();

    public ExampleGraphics2() {
        new Timer(TIMER_DELAY, new TimerListener()).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for (Surface surface : surfaces) {
            surface.draw(g);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class TimerListener implements ActionListener {
        private int index = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            index++;
            index %= 20;
            if (index == 0) {
                surfaces.add(new Surface(10, 10));
            }

            for (Surface surface : surfaces) {
                surface.runner();
            }
            repaint();
        }
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("Example Graphics 2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ExampleGraphics2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

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

package foo1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class Surface {
    private int locX = 0;
    private int locY = 0;

    public Surface(int locX, int locY) {
        this.locX = locX;
        this.locY = locY;
    }

    public void runner() {
        locX = locX + 1;
    }

    public void draw(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.RED);
        g2d.fillOval(locX, locY, 10, 10);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM