Java - 在Applet中绘制文本

时间:2014-08-04 23:13:01

标签: java text applet drawing awt

我知道有很多答案,但它们似乎不起作用。我不知道我是不是在某种程度上使用不同版本的Java,或者applet的代码是不同的,但这是我的代码:

来自HUD课程的片段:

public void paint (Graphics g)
{

    if(g instanceof Graphics2D)
    {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.drawString("This is gona be awesome", 200, 200);
    }

主类:

import java.applet.Applet; (more, they just glitched with the code block)


public class Main extends Applet implements Runnable, KeyListener {

static ArrayList<Block> all_blocks = new ArrayList<Block>();
Player player;
HUD hud;
static int amount = 0;

public void init(){

    setSize(700, 500);
    addKeyListener(this);

}

public void start(){

    player = new Player(100, 100);
    HUD hud = new HUD();

    for (int i = 0; i < 75; i++){

        new Grass(i*30, 400);

    }
    new Spikes(250, 370);


    Thread thread = new Thread(this);
    thread.start();

}

@Override
public void run() {

    while (true){

        player.update();

        repaint();
        camera_scroll(this.amount);

        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

public void stop(){

}

public void camera_scroll(int amount){
    int new_amount;

    if (amount > 30){
        new_amount = 30;
        this.amount -= 30;

    } else {
        new_amount = amount;
        this.amount = 0;
    }
    for (Block b: all_blocks){
        b.x += new_amount;
        b.update();
    }
    player.x += new_amount;
}

public void paint(Graphics g){
    for (Block block: all_blocks){
        block.paint(g);
    }
    hud.paint(g);
    player.paint(g);

}

@Override
public void keyPressed(KeyEvent e) {
    switch(e.getKeyCode()){

    case KeyEvent.VK_LEFT:
    case KeyEvent.VK_A:
        player.moveLeft();
        break;

    case KeyEvent.VK_RIGHT:
    case KeyEvent.VK_D:
        player.moveRight();
        break;

    case KeyEvent.VK_UP:
    case KeyEvent.VK_W:
        player.moveUp();
        break;



    }

}

@Override
public void keyReleased(KeyEvent e) {
    switch(e.getKeyCode()){

    case KeyEvent.VK_LEFT:
    case KeyEvent.VK_A:
        player.stopLeft();
        break;

    case KeyEvent.VK_RIGHT:
    case KeyEvent.VK_D:
        player.stopRight();
        break;

    case KeyEvent.VK_UP:
    case KeyEvent.VK_W:
        player.stopUp();
        break;


    }
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}



}

玩家和积木的绘制工作正常,他们的绘画方法与HUD相同,但他们绘制图像。需要帮助请叫我。谢谢!

1 个答案:

答案 0 :(得分:2)

问题......

有两个主要问题......

一...

您要在start方法中创建对象实例,而这些对象应在init方法中创建。

可以多次调用

start(通常在调用stop之后)以使applet继续。这不是尝试初始化基础applet状态的好地方,相反,它最好用于重新启动Thread(假设你在调用stop时也停止它)...

所以,而不是......

public void init(){

    setSize(700, 500);
    addKeyListener(this);

}

public void start(){

    player = new Player(100, 100);
    HUD hud = new HUD();

    for (int i = 0; i < 75; i++){

        new Grass(i*30, 400);

    }
    new Spikes(250, 370);


    Thread thread = new Thread(this);
    thread.start();

}

你应该使用像...这样的东西。

public void init(){

    setSize(700, 500);
    addKeyListener(this);

    player = new Player(100, 100);
    HUD hud = new HUD();

    for (int i = 0; i < 75; i++){

        new Grass(i*30, 400);

    }
    new Spikes(250, 370);

}

public void start(){

    Thread thread = new Thread(this);
    thread.start();

}

二...

您正在创建对象的本地实例,遮蔽您的实例字段,这会导致NullPointerException(对我来说,它位于paint ...

所以而不是

public void init(){

    setSize(700, 500);
    addKeyListener(this);

    player = new Player(100, 100);
    HUD hud = new HUD();

    for (int i = 0; i < 75; i++){

        new Grass(i*30, 400);

    }
    new Spikes(250, 370);

}

你应该使用......

public void init(){

    setSize(700, 500);
    addKeyListener(this);

    player = new Player(100, 100);
    hud = new HUD();
    // Notice that HUD has not been re-declared...

    for (int i = 0; i < 75; i++){

        new Grass(i*30, 400);

    }
    new Spikes(250, 370);

}

附注

  • AWT在15年前被Swing取代,通常不再使用API​​。建议使用JApplet来代替......
  • 众所周知,小程序很难开发,并且通常在非常严格的安全沙箱中运行。除非您非常特别需要使用它们,否则我建议您暂时不要使用它们,并使用JFrame作为核心顶级容器。
  • 直接绘制到顶级容器(如applet或框架)通常是一个坏主意,相反,通常鼓励您将核心逻辑移动到JPanel之类的内容并覆盖它#39; s paintComponent方法执行自定义绘画。推荐这是出于多种原因,包括绘画实际上非常复杂且很容易打破油漆链,顶级容器不能双重缓冲,这会导致闪烁。默认情况下,Swing组件(除顶级对应组件外)是双缓冲的。有关详细信息,请查看Painting in AWT and SwingPerforming Custom Painting
  • KeyListener以出现与焦点相关的问题而臭名昭着。如果您转到Swing API,最好使用Key Bindings API
  • 另外,Swing不是线程安全的,只要注意;)