为什么我的方法不清除的ArrayList? [英] Why is my method not clearing the ArrayList?

查看:121
本文介绍了为什么我的方法不清除的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注:我的code中的问题只是我创建清除rects和一切,但我做错了唯一的方法,实例化去内DrawPanel类的myDraw对象()方法。所以,我必须与再次停止实例DrawPanel并创造了一个全新的对象。所以,我最终调用比分别被添加到一个rects不同DrawPanel对象的clearRects方法。反正,我决定用MadProgrammer code建议去,因为他的code是究竟是如何的Java:初学者指南教了,就干净多了

好吧,我一直在跑来跑去的计算器,因为今天上午已经能够解决了很多问题,我的code,但我还是坚持了这个问题的ArrayList

Well, I have been running around StackOverflow since this morning and have been able to fix a lot of problems with my code but I am still stuck with this problem with ArrayLists.

我有下面这段code的似乎并不做什么,我打算为它做。现在我知道我是一个犯了一个错误的地方,但真的不知道如何纠正它。

I have the following piece of code that does not seem to do what I intend for it to do. Now I am aware that I am the one making a mistake somewhere but not really sure how to correct it.

这是设置方式是,当我打的停止按钮,ArrayList中应明确,所以我有一个空白的JPanel可以这么说,这里的code片段。我可以张贴整个程序,如果你要我,虽然,但我只是在这里粘贴代码片段,因为我假设我在我的部分制作pretty简单而愚蠢的错误:

The way it is set up is that when I hit the stop button, the ArrayList should clear so I have a blank JPanel so to speak, here's the code snippets. I can post the whole program if you want me to though but I am only pasting the snippet here because I'm assuming I'm making a pretty simple and dumb mistake on my part:

class DrawPanel extends JPanel {
    ArrayList<MyRectangle> rects = new ArrayList<>();
    Random rand = new Random();

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        addRect();

        for(MyRectangle r : rects) {
        g.setColor(r.getColor());
        g.fillRect(r.x, r.y, r.width, r.height);
        }
    }

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

    public ArrayList<MyRectangle> addRect() {
        int ht = rand.nextInt(getHeight());
    int wd = rand.nextInt(getWidth());

    int x = rand.nextInt(getWidth() - wd);
    int y = rand.nextInt(getHeight() - ht);

    int r = rand.nextInt(256);
    int g = rand.nextInt(256);
    int b = rand.nextInt(256);

    rects.add(new MyRectangle(x, y, wd, ht, new Color(r, b, g)));
    System.out.println(rects.size());
    return rects;
}

    public void clearEvent(ActionEvent e) {
        System.out.println(rects.size());
        rects.clear();
        frame.repaint();
        System.out.println("I was called");
    }
}

和这里的地方的按钮调用它的actionPerformed方法的一部分:

And here's the part where the button calls it in its actionPerformed method:

class StopListener implements ActionListener {
    DrawPanel draw = new DrawPanel();
    public void actionPerformed(ActionEvent e) {
        timer.stop();
        draw.clearEvent(e);
    }
    }

编辑:据我所知,我的clearEvent方法是指ArrayList对象是不一样的一个的addRect()添加的东西。什么我问,我猜,是如何使其连接这样我就可以擦干净的东西用的JButton。

I understand that the arraylist object that my clearEvent method refers to is not the same one that addRect()'s adding stuff to. What I am asking, I guess, is how to make it "connect" so I can wipe things clean using the JButton.

编辑:以下是完整的程序:

Here's the full program:

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import java.awt.*;

public class TwoButtonsRandomRec {

    JFrame frame;
    Timer timer;

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            TwoButtonsRandomRec test = new TwoButtonsRandomRec();
            test.go();
        }
        });
    }

    public void go() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton startButton = new JButton("Start");
    startButton.addActionListener(new StartListener());
    JButton stopButton = new JButton("Stop");
    stopButton.addActionListener(new StopListener());

    final DrawPanel myDraw = new DrawPanel();

    timer = new Timer(50, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            myDraw.repaint();
        }
        });

    frame.add(startButton, BorderLayout.NORTH);
    frame.add(stopButton, BorderLayout.SOUTH);
    frame.add(myDraw, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }

    class StartListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        timer.start();
    }
    }

    class StopListener implements ActionListener {
    DrawPanel draw = new DrawPanel();
    public void actionPerformed(ActionEvent e) {
        timer.stop();
        draw.clearEvent(e);
    }
    }

    class DrawPanel extends JPanel {
    ArrayList<MyRectangle> rects = new ArrayList<>();
    Random rand = new Random();

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);

        addRect();

        for(MyRectangle r : rects) {
        g.setColor(r.getColor());
        g.fillRect(r.x, r.y, r.width, r.height);
        }
    }

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

    public ArrayList<MyRectangle> addRect() {
        int ht = rand.nextInt(getHeight());
        int wd = rand.nextInt(getWidth());

        int x = rand.nextInt(getWidth() - wd);
        int y = rand.nextInt(getHeight() - ht);

        int r = rand.nextInt(256);
        int g = rand.nextInt(256);
        int b = rand.nextInt(256);

        rects.add(new MyRectangle(x, y, wd, ht, new Color(r, b, g)));
        System.out.println(rects.size());
        return rects;
    }

    public void clearEvent(ActionEvent e) {
        System.out.println(rects.size());
        rects.clear();
        repaint();
        System.out.println("I was called");
    }
    }
}

class MyRectangle extends Rectangle {
    Color color;
    public MyRectangle(int x, int y, int w, int h, Color c) {
    super(x, y, w, h);
    this.color = c;
    }

    public Color getColor() {
    return color;
    }
}

这里的previous相关的问题,我在这里问的情况下,任何人的兴趣。

Here's the previous relevant question I asked here in case anyone's interested.

奇怪的JFrame行为

推荐答案

我看到了两个紧迫的问题。

I see two immediate issues.

第一个是,你打电话 addRect 的paintComponent 方法,这意味着,即使您明确的内在列表,就下重画,一个新的矩形将被添加到它。

The first is, you're calling addRect within the paintComponent method, which means, even after you clear the List, on the next repaint, a new rectangle will be added to it.

其次,我称之为重绘中的 DrawPanel 而不是使用 frame.repaint (),因为你真的只想要更新的平局面板,而不是整个帧

Secondly, I would call repaint in the DrawPanel instead of using frame.repaint(), as you really only want to update the draw panel, not the entire frame

public class BadPaint05 {

    public static void main(String[] args) {
        new BadPaint05();
    }

    public BadPaint05() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MasterPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MasterPane extends JPanel {

        private DrawPanel drawPane;
        private Timer timer;

        public MasterPane() {
            setLayout(new BorderLayout());
            drawPane = new DrawPanel();

            add(drawPane);

            JButton stop = new JButton("Stop");
            stop.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    drawPane.clearEvent(e);
                    timer.stop();
                }
            });

            timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    drawPane.addRect();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

            add(stop, BorderLayout.SOUTH);

        }

    }

    class DrawPanel extends JPanel {

        ArrayList<MyRectangle> rects = new ArrayList<>();
        Random rand = new Random();

        @Override
        public void paintComponent(Graphics g) {

            super.paintComponent(g);

//            addRect();

            for (MyRectangle r : rects) {
                g.setColor(r.getColor());
                g.fillRect(r.x, r.y, r.width, r.height);
            }
        }

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

        public ArrayList<MyRectangle> addRect() {
            int ht = rand.nextInt(getHeight());
            int wd = rand.nextInt(getWidth());

            int x = rand.nextInt(getWidth() - wd);
            int y = rand.nextInt(getHeight() - ht);

            int r = rand.nextInt(256);
            int g = rand.nextInt(256);
            int b = rand.nextInt(256);

            rects.add(new MyRectangle(x, y, wd, ht, new Color(r, b, g)));
            System.out.println(rects.size());
            repaint();
            return rects;
        }

        public void clearEvent(ActionEvent e) {
            System.out.println(rects.size());
            rects.clear();
//            frame.repaint();
            repaint();
            System.out.println("I was called");
        }
    }

    public class MyRectangle {

        private int x, y, width, height;
        private Color color;

        private MyRectangle(int x, int y, int wd, int ht, Color color) {
            this.x = x;
            this.y = y;
            this.width = wd;
            this.height = ht;
            this.color = color;
        }

        public Color getColor() {
            return color;
        }

    }
}

这篇关于为什么我的方法不清除的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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