Java Can';t从主方法运行绘制方法

Java Can';t从主方法运行绘制方法,java,object,Java,Object,在main方法中,我尝试运行以下命令: public static void main(String[] args) { game.paintBlocks(g); } 并为“g”参数获取“无法解析为变量”错误 在其他地方,我有这个,它调用另一个类中的另一个方法(paint(g))来绘制块的网格: public void paintBlocks(Graphics g) { for (int r = 0; r<7; r++) { for (int

在main方法中,我尝试运行以下命令:

public static void main(String[] args)
{
    game.paintBlocks(g);
}
并为“g”参数获取“无法解析为变量”错误

在其他地方,我有这个,它调用另一个类中的另一个方法(paint(g))来绘制块的网格:

public void paintBlocks(Graphics g)
{  
    for (int r = 0; r<7; r++)
    {
        for (int c = 0; c<5; c++)
        {
            block[r][c].paint(g);
        }
    }
public void paintblock(图形g)
{  

对于(int r=0;r,在
paintBlocks
的情况下,
g
是传递给方法的参数。在
main
的情况下,
g
引用的变量没有在任何地方创建

和是抽象类,通常不打算通过Swing进行实例化。
Graphics
Graphics2D
为您提供了在组件(如JPanel或BuffereImage)上绘制的上下文

根据您的描述,您可能希望在某种Swing组件上绘制块(虽然有点不清楚,但这是一种正常的做法)例如,如果要在
JPanel
上绘制块,通常要做的是创建一个扩展
JPanel
的类,并重写
paintComponent()
方法。可以这样做:

public class BlocksPanel extends JPanel {
   // Normal class fields, etc.
   // ...

   // I would consider making this private, but this is your method from above:
   public void paintBlocks(Graphics g) {  
      for (int r = 0; r<7; r++) {
         for (int c = 0; c<5; c++) {
            block[r][c].paint(g);
         }
      }
   }

   @Override
   public void paintComponent(Graphics g) {
      paintBlocks(Graphics g);
   }
}
公共类BlocksPanel扩展了JPanel{
//普通类字段等。
// ...
我会考虑把这个私有化,但这是你的方法:
公共空白画板(图形g){

对于(int r=0;r变量
g
在主上下文中未定义,因为您尚未声明/初始化它。如果查看
paintBlocks(Graphics g g)
方法,
g
作为参数传递,但是该变量(
g
)的范围在大括号内(
{}
)方法
颜料块(图形g)

如果您有一个名为
MyClass
的类来扩展组件,比如
JPanel
,您可以执行以下操作:

class MyClass extends JPanel
{
     public static void main(String[] args)
     {
         Graphics g = getGraphics(); //would return the graphics object for the JPanel
         game.paintBlocks(g);
     }
}
public static void main(String[] args)
{
   repaint(); //this repaints the component, calling the paintComponent method
}
同样值得注意的是,上述方法在某些情况下会被标记为糟糕的编程风格。还有一种选择。您可以使用组件提供的
paintComponent(Graphics g)
方法

然后,您的主视图将如下所示:

class MyClass extends JPanel
{
     public static void main(String[] args)
     {
         Graphics g = getGraphics(); //would return the graphics object for the JPanel
         game.paintBlocks(g);
     }
}
public static void main(String[] args)
{
   repaint(); //this repaints the component, calling the paintComponent method
}
自己调用
paintComponent(Graphics g)
也是一种糟糕的编程风格。你应该允许系统调用该方法,这就是为什么你有
repaint()
方法。当你重新绘制时,系统会自动调用
paintComponent(Graphics g)

paintComponent(图形g)
可以执行以下操作:

public void paintComponent(Graphics g)
{
    super.paintComponent(g); //repainting the panel,not necessary in some cases
    game.paintBlocks(g); //passing the graphics object used by the component
}

希望有帮助!

你想在哪里画画?我假设你可能想在屏幕上的一个窗口上画画,在这种情况下,你不会自己调用画画*,你会让Swing框架在适当的时候调用它。在这种情况下,如果
game
JFrame
,那么你只需要让它可见;或者如果
game
是其他类型的组件,您需要将其添加到可见窗口中。这是我在用Java教授基本图形时通常使用的模式:

public class MyGame extends JPanel {
  public static void main() {
    JFrame window = new JFrame("My Game");
    window.add(new MyGame());
    window.pack();
    window.setLocationRelativeTo(null); // Centers the window on the screen
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible();
  }
  public Dimension getPreferredSize() {
    return new Dimension(800, 600);
  }
  protected void paintComponent(Graphics g) {
    // Do my drawing here
  }
}
如果要绘制屏幕外图像,则需要创建自己的图形上下文以传递到paint*方法:

BufferedImage hello = new BufferedImage(game.getWidth(), game.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = hello.getGraphics();
game.paintBlocks( g );

您是否在main类上创建了一个新的图形对象g,以便在main函数中使用它?我假设,您导入了java.awt.Graphics,或者很快导入了java.awt.*。那么这就像是创建一个访问器方法吗?我想这有点让我难以理解。非常遗憾,谢谢大家的帮助。不,这不是,这只是一种更好的方法,可以完成您想要做的事情。