简体   繁体   English

使用Java Swing Graphics G在JPanel中绘制

[英]Draw in JPanel with java swing Graphics g

This is my first java project and I am trying to draw a simple rectangle on my JPanel inside my JFrame. 这是我的第一个Java项目,我试图在JFrame的JPanel上绘制一个简单的矩形。 Been trying to solve this issue with the help of the same topics on stackoverflow but still no success. 试图通过关于stackoverflow的相同主题来解决此问题,但仍然没有成功。 The exception I get when I run the program is java.lang.NullPointerException. 运行程序时遇到的异常是java.lang.NullPointerException。 From my understanding I can not draw on the JPanel itself? 据我了解,我不能依靠JPanel本身吗? which is created in mainWindow. 在mainWindow中创建的。

Main: 主要:

public class Main {
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GameBoard game = new GameBoard();
                mainWindow view = new mainWindow(game);
                mainModel model = new mainModel();
                mainController cont = new mainController(model, view, game);

                cont.controllerInit();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}

View: 视图:

public class mainWindow{
public JFrame frame;
public JPanel panel;

    GameBoard game = new GameBoard();
    frame = new JFrame();
    frame.getContentPane().setBackground(SystemColor.control);
    frame.setBounds(100, 100, 728, 435);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(game);
    frame.getContentPane().setLayout(null);

    panel = new JPanel();                           
    FlowLayout flowLayout = (FlowLayout) panel.getLayout();
    panel.setBounds(166, 44, 550, 349); 
    frame.getContentPane().add(panel);

    frame.setVisible(true);
}

Game: 游戏:

public class GameBoard extends JPanel{

@Override
public void paintComponent(Graphics g) 
{
    super.paintComponent(g);
        g.setColor(Color.BLUE);
        g.drawRect(200, 200, 200, 200);
}

}

Never, ever call paintComponent directly, no external source has any reason to do so. 永远不要直接调用paintComponent ,没有任何外部来源可以这样做。 Also, what do you thing would happen if you passed it null ? 此外,如果将其传递为null会发生什么情况?

You should start by having a look at Performing Custom Painting and Painting in AWT and Swing to get a better understand of how paint in Swing works. 您应该先看看在AWT和Swing执行自定义绘画以及绘画,以更好地了解Swing中的绘画工作原理。

The Swing API basically uses a delegate model, where the system delegates responsibility of the paint of each component to the component. Swing API基本上使用委托模型,在该模型中,系统将每个组件的绘制职责委托给该组件。 This is achieved by the system calling the components paint method, which in-turn calls (among a few others) paintComponent . 这是通过系统调用组件paint方法来实现的,该方法依次调用(其他方法中) paintComponent

Swing also uses a passive rendering approaching, meaning that painting occurs at the discretion of the paint system. Swing还使用被动渲染方法,这意味着绘画是由绘画系统自行决定的。 You component is notified of the need when its paint method is called. 当组件的paint方法被调用时,组件会收到需要的通知。 This may occur at any time. 这可能随时发生。

In order for a component to be painted, it must first be added to container which is realised on the screen (has a native peer), in most cases, this means that the component hierarchy needs to resolve to some kind of window based class, like JFrame . 为了绘制组件,必须首先将其添加到在屏幕上实现的容器中(具有本机对等体),在大多数情况下,这意味着组件层次结构需要解析为某种基于窗口的类,就像JFrame一样。

So, the answer to your question is: 因此,您的问题的答案是:

  • Read the above documentation (and get a better understanding of how the API works) 阅读上述文档(并更好地了解API的工作原理)
  • Add your GameBoard to a container which can be resolved to a window based class 将您的GameBoard添加到可以解析为基于窗口的类的容器中
  • Never call paint or paintComponent directly 切勿直接调用paintpaintComponent

Reflection.... 反射....

private mainWindow view;
private mainModel model;
public GameBoard(mainModel m, mainWindow v)
{
    view = v;
    model = m;
}

To me, this makes no sense. 对我来说,这没有任何意义。 There is no reasonable reason why GameBoard needs a reference to mainWindow . 没有合理的理由说明GameBoard需要引用mainWindow GameBoard is, in of itself, a "view". GameBoard本身就是一个“视图”。 If anything, the only thing you "should" be passing to GameBoard (assuming you're trying to use a MVC) is a controller 如果有的话,您“应该”传递给GameBoard的唯一东西(假设您尝试使用MVC)是控制器

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

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