程序不绘画

时间:2013-12-19 23:32:32

标签: java swing graphics

我打算在我的JPanel上画一个正方形,然而,它没有显示出来。 我做错了什么?

class GUI extends JPanel {
    private static Game game = new Game();
    ...
    public GUI () {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setAttributes();
                makeMenu();
            }
        });
    }
    ... 
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.white);
        g.drawRect(20, 20, 100, 100);
    }
}

编辑:代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class GUI extends JPanel {
    private static Game game = new Game();

    private static JPanel panel = new JPanel();
    private static JFrame frame = new JFrame();
    final private static int FRAME_HEIGHT = 500;
    final private static int FRAME_WIDTH = 500;
    //Board size 25x25px
    final private static int PIXEL_SIZE = 20;
    public GUI () {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            setAttributes();
            makeMenu();
         }
      });
    }
    public static void setAttributes() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("");
        frame.setBackground(Color.black);
        frame.setVisible(true);
    }

    private static void makeMenu() {
        JButton start = new JButton("Start");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                game.startGame();
            }
        });
        panel.add(start);
        frame.add(panel);
        frame.pack();
    }
    public void setGameFrame() {
        panel.removeAll();
        frame.setTitle("Snake v0.1");
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.white);
      g.drawRect(20, 20, 100, 100);
   }
    public void paintGraphics() {
        int[][] pixels = Game.getGraphics();
    }
}

1 个答案:

答案 0 :(得分:3)

我看了你的代码。你在哪里添加GUI到任何东西?答:你没有,如果你不这样做,什么都不会画。解决方案:将其添加到gui,并阅读教程,因为代码中有很多内容需要修复。

其他建议:

  • 除去污垢以外的所有静态变量。
  • 在main方法中调用invokeLater,而不是在JPanel的构造函数
  • 中调用
  • 再次,将您的绘画GUI添加到您的实际gui中,以便将其添加到将显示它的内容中。
  • 请勿在任何组件上调用getGraphics(),因为这会使您获得非持久性图形对象。

如,

import javax.swing.*;
import java.awt.*;

class GUI extends JPanel {
   private static final int PREF_W = 200;
   private static final int PREF_H = PREF_W;
   private static final int RECT_X = 20;
   private static final int RECT_Y = RECT_X;
   private static final int RECT_WIDTH = 100;

   public GUI() {
      setBackground(Color.darkGray);
   }

   // use @Override to be sure that your method is a true override
   // Note that paintComponent should be protected, not public
   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.white);

      // avoiding "magic" numbers here.
      g.fillRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
      g.setColor(Color.red);
      g.drawRect(RECT_X, RECT_Y, RECT_WIDTH, RECT_WIDTH);
   }

   // so that the layout managers know how big I want this JPanel to be.
   // this is a dumb method implementation. Yours will hopefully be smarter
   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H); 
   }

   private static void createAndShowGui() {
      GUI mainPanel = new GUI();

      JFrame frame = new JFrame("GUI");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}