Java Swing drawing disappears when resizing window

前端 未结 4 1871
刺人心
刺人心 2021-01-14 06:23

i need your help badly because i cannot solve this problem on my own. I am trying to create a GUI and want to draw something in it after pressing a button, but i seem to ha

4条回答
  •  佛祖请我去吃肉
    2021-01-14 06:28

    Try your hands on this code, and ask any questions that may arise, do painting inside the paintComponent(...) method of the JPanel. Instead of providing size everytime for the said JComponent you can simply override getPreferredSize(), of the said component. In order to call your paintComponent(...) you can simply write repaint() instead of explicitly making a call to paintComponent(...) from within your program, Swing will do that automatically.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class PaintingExample
    {
        private CustomPanel paintingPanel;
        private Timer timer;
        private int x = 1;
        private int y = 1;
        private ActionListener timerAction = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                x++;
                y++;
                paintingPanel.setPosition(x, y);
            }
        };
    
        private void createAndDisplayGUI()
        {
            JFrame frame = new JFrame("Painting Example");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            paintingPanel = new CustomPanel();
            final JButton startStopButton = new JButton("STOP");
            startStopButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (timer.isRunning())
                    {
                        startStopButton.setText("START");
                        timer.stop();
                    }
                    else if (!timer.isRunning())
                    {
                        startStopButton.setText("STOP");
                        timer.start();
                    }
                }
            });
    
            frame.add(paintingPanel, BorderLayout.CENTER);
            frame.add(startStopButton, BorderLayout.PAGE_END);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            timer = new Timer(100, timerAction);
            timer.start();
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new PaintingExample().createAndDisplayGUI();
                }
            });
        }
    }
    
    class CustomPanel extends JPanel
    {
        private int x = 0;
        private int y = 0;
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(800, 600));
        }
    
        public void setPosition(int a, int b)
        {
            x = a;
            y = b;
            if (x <(getWidth() - 10) && y < (getHeight() - 10))
                repaint();
            else
                System.out.println("Nothing is happening...");
        }
    
        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.clearRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.MAGENTA);
            g.fillOval(x, y, 10, 10);
        }
    }
    

提交回复
热议问题