java中java.awt.applet,如何在java.applet.Applet中获取java.awt.Graphics的实例?

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.

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

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.(Segment.java:21)

at SnakeGame.(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)

解决方案

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.

General tips/questions.

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.

Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing.

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.

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值