Drawing Canvas on JFrame

柔情痞子 提交于 2019-12-02 08:50:14
MadProgrammer

I don't know what it is you're trying to do, but you should NEVER be calling paint and especially not pass it null.

Start by taking a look at Performing Custom Painting and Painting in AWT and Swing for details about how painting works.

In order to get the window to size to you component, you need to provide it some important information.

While Window#pack is the method you are looking for, it will not help you unless you provide appropriate sizing hints.

In this case, you need to override the getPreferredSize method of you component and provide an appropriate size value. Window#pack will use this value to determine what size it needs to be in order to accommodate it.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestPaint {

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

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

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

    public class TestPane extends JPanel {

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.GREEN);
            g.fillRect(0, 0, WIDTH, HEIGHT);
            g.setColor(Color.BLACK);
            g.fillOval(100, 100, 30, 30);
        }
    }
}

The paint chain is very important and you should avoid breaking it at all coasts. Make sure you always call super.paintXxx or be prepared for some serious weirdness

Also may want to have a read of Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

Use java.awt.Window.pack (JFrame indirectly extends Window):

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.

    // ...
    frame.add(ga);
    frame.pack();
    frame.setVisible(true);
    // ...

After painting on canvas try repaint the JFrame

public static void main(String[] args) {
    Game ga = new Game();
    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(ga);
    frame.setVisible(true);
    ga.start();

    //repaint here
    frame.repaint();
}

Also note that:

frame.pack for fixing the size issue.

frame.revalidate sometimes help when adding or removing components in runtime.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!