【问题标题】:List of graphics2Dgraphics2D 列表
【发布时间】:2017-02-26 23:48:31
【问题描述】:

为什么下面的代码不起作用?我想每 200 毫秒向 ArrayList 添加新的椭圆并显示它们并一一运行它们。当我运行一个粒子时它工作正常 s.runner();但它似乎并没有运行我所有的粒子。

主要:

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

图形:

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

【问题讨论】:

    标签: java swing arraylist graphics graphics2d


    【解决方案1】:

    我认为你的程序结构被破坏了。你应该只有一个 JPanel 来进行绘图,它的 paintComponent 被覆盖,你的 Surface 类应该是一个 logical 类而不是组件类——换句话说,没有它扩展JPanel,并给它一个public void draw(Graphics g) 方法来绘制椭圆。然后让绘图 JPanel 持有这些表面的 ArrayList,并在主 JPanel 的paintComponent 方法中,遍历表面,调用每个表面的绘图方法。

    另外,你的 Timer 的延迟是不现实的,而且太小了。 15 会更现实。

    另外,不要从表面调用repaint(),因为这会产生太多不必要的重绘调用。而是在对所有 Surface 对象调用 runner 方法后从 Timer 的 ActionListener 中调用它。

    还请注意,每次以默认方式将组件添加到 JFrame 的 contentPane 时,都会掩盖先前添加的组件。如果您按照我上面的建议进行操作,这不是问题,因为您只需将单个 JPanel 添加到其中。

    例如:

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

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2011-02-06
      • 2012-04-13
      相关资源
      最近更新 更多