如何在java.applet.Applet中获取java.awt.Graphics的实例? [英] How to get an instance of java.awt.Graphics in a java.applet.Applet?

查看:926
本文介绍了如何在java.applet.Applet中获取java.awt.Graphics的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的计算机科学课编写Snake游戏.但是,我遇到了很多困难(因为我是自己中唯一一个独自工作的人),所以我不得不重新启动.

I am currently coding the game of Snake for my computer science class. However, I ran into a lot of difficulties (since I am the only one in my block that is working alone) and I had to restart.

现在,我只是想从我制作的第二个类(段)中创建一个对象数组(名为Snake).

Right now I am just trying to make an array of objects (named Snake) from the second class (Segment) I made.

我在第26行收到NullPointerException:g.fillRect(xValue, yValue, size, size); 我应该在哪里获取Graphics的实例以调用此方法?

I get a NullPointerException on line 26: g.fillRect(xValue, yValue, size, size); Where should I get an instance of Graphics to call this method on?

import java.awt.*;
import java.applet.*;

public class Segment extends Applet
{
   private Graphics g;

   private int xValue;
   private int yValue;
   private int size;

   public Segment () {}

  public Segment(int x, int y)
  {
  xValue = x;
  yValue = y;

  size = 10;

  drawSegment(g);
   }

   public void drawSegment(Graphics g)
   {
      g.fillRect(xValue, yValue, size, size);
   }

  }

import java.awt.*;
import java.applet.*;

public class SnakeGame extends Applet
{
   private Segment[] snake;
   private Graphics g;

   private int length;
   private int sxValue;
   private int syValue;

   public SnakeGame()
   {
      length = 6;
      snake = new Segment[length];
      sxValue = 100;
      syValue = 100;

      for (int c = 0; c < snake.length; c++)
      {
         snake[c] = new Segment(sxValue, syValue);
         sxValue = sxValue + 10;     
      }

      drawSnake(g);

    }

   public void drawSnake(Graphics g)
   {
      for(int counter = 0; counter < length; counter++)
      {
          snake[counter].drawSegment(g);

      }  
    }


}

但是,我不断收到诸如以下的错误

However, I keep getting errors such as

java.lang.NullPointerException
at Segment.drawSegment(Segment.java:26)
at Segment.<init>(Segment.java:21)
at SnakeGame.<init>(SnakeGame.java:22)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at java.lang.Class.newInstance(Class.java:433)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:799)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:728)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)

推荐答案

从自定义绘制组件的paint(Graphics)paintComponent(Graphics)方法中获取Graphics对象.那个时间(当JVM请求时)是应该绘制AWT/Swing GUI的时间.有关更多详细信息和工作来源,请参见执行自定义绘画.

Get a Graphics object from within the paint(Graphics) or paintComponent(Graphics) method of the custom painted component. That time (when requested to do so by the JVM) is the time that an AWT/Swing GUI should be painted. See Performing Custom Painting for more details and working source.

  1. 为什么要编写小程序?如果是由于老师指定的原因,请参考为什么CS老师应该停止教授Java小程序.
  2. 为什么要使用AWT?出于很多理由,请放弃使用支持的组件放弃AWT,请参见此答案挥杆.
  3. 通常认为,最好在添加到顶级容器的PanelCanvasJPanel中进行自定义渲染,而不是容器本身.这更容易适应,因为我们可以在小程序,框架,对话框或其他面板的布局约束中显示面板.还有其他优点.
  4. 上面有两个applet,但是只有SnakeGame应该是applet.相反,Segment类应该只知道在被要求时如何将自己绘制为Graphics对象.然后SnakeGame可以存储Segment对象的列表,并(在需要时)依次绘制每个对象.
  5. 不要为applet声明构造函数,因为没有办法确保在Event Dispatch Thread上调用它.在init()方法中仅第一次"执行所有操作.但是请认真避免使用小程序,因为小程序是一种过时的技术,只会让您头疼.
  1. Why code an applet? If it is due to the teacher specifying it, please refer them to Why CS teachers should stop teaching Java applets.
  2. Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing.
  3. It is generally considered better to do custom rendering in a Panel, Canvas or JPanel that is added to the top level container, rather than the container itself. This is more easily adaptable in that we might show the panel in an applet, frame, dialog or a layout constraint of another panel. There are also other advantages.
  4. There are two applets above, but only SnakeGame should be an applet. The Segment class should instead just know how to draw itself to a Graphics object when asked to do so. Then SnakeGame can store a list of Segment objects and (when requested) draw each in turn.
  5. Don't declare a constructor for an applet, since there is no way to ensure it is called on the Event Dispatch Thread. Do everything 'the first time only' in the init() method. But seriously, avoid applets as they are an obsolescent technology that will give you nothing but headaches.

这篇关于如何在java.applet.Applet中获取java.awt.Graphics的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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