程序不是绘画 [英] Program not Painting

查看:243
本文介绍了程序不是绘画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算为此在我的JPanel上画一个正方形,但是,它没有出现。
我在做什么错了?

  class GUI扩展JPanel {
private static 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);


$ / code $ / pre

$ hr

编辑:代码

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

类GUI扩展JPanel {
private static 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;
//开发板尺寸25x25px
final private 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();
}
}


解决方案

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



其他建议:


  • 删除所有静态变量,除了contants。
  • 在主方法中调用invokeLater,而不是在JPanel的构造函数中调用
  • >
  • 再次,将您的绘画图形用户界面添加到您的实际GUI中,以便将它添加到显示它的内容中。 不要调用 getGraphics(),因为它会为你提供一个非持久化的Graphics对象。





例如,

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

类GUI扩展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);
}

//使用@Override确保你的方法是一个真正的覆盖
//注意paintComponent应该被保护,而不是public
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.white);

//在此避免魔术数字。
g.fillRect(RECT_X,RECT_Y,RECT_WIDTH,RECT_WIDTH);
g.setColor(Color.red);
g.drawRect(RECT_X,RECT_Y,RECT_WIDTH,RECT_WIDTH);
}

//以便布局管理员知道我想要这个JPanel有多大。
//这是一个愚蠢的方法实现。你希望更聪明
@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();
}
});
}
}


I intended for this to paint a square on my JPanel, however, it does not show up. What am I doing wrong?

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);
    }
}


Edit: the code

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();
    }
}

解决方案

I looked at your code. Where do you ever add GUI to anything? Answer: you don't, and if you don't, nothing will be painted. Solution: add it to the gui, and read the tutorials as there is much to fix in your code.

Other suggestions:

  • Get rid of all static variables except for the contants.
  • Call your invokeLater in your main method, not in a JPanel's constructor
  • Again, add your painting GUI to your actual gui so that it gets added to something that will display it.
  • Don't call getGraphics() on any component as that will get you a non-persisting Graphics object.

e.g.,

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();
         }
      });
   }
}

这篇关于程序不是绘画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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