2

Edit: uses awt...

I am trying to display random numbers in an applet based maths game, and have run into an issue. Either one of two things happen depending on where I call the method:

  • The numbers generate and display properly, without automatically updating each frame but get drawn over by the background and artwork each frame as they're being updated at runtime, or...
  • The number displayed on-screen appear above the background elements but are redrawn fresh every frame.

Quick illustration:

private int setNumberOne() {

    return rand.nextInt(11) + 2;
}

private int setNumberTwo() {

    return rand.nextInt(11) + 2;
}

private int setAnswer() {

    return setNumberTwo() * setNumberOne();
}

private void displayOutput() {

    Graphics2D g2d = graphics();

    int one = setNumberOne();
    int ans = setAnswer();

    setNumberTwo();

    g2d.setColor(Color.WHITE);
    g2d.drawString(one + " x ? = " + ans, 480, 480);
}

Calling this function in the initialisation method displays a static question that I can update elsewhere when specific triggers are met, however everything else gets drawn on top of it by the update event, rendering it invisible. The only way I've managed to see this working is to remove the other images from the game for testing.

Is there a way to set the "precedence" of GUI elements in an applet?

I'm currently looking at attempting to include the displayOutput() method in it's own thread, but I'm not so experienced with Java and its proving very hard to resolve.

I also tried to not allow the background to update at runtime, but the result was that the moving game objects left trails all over the screen.

What am I missing? If anyone has any suggestions as to how this can be correctly implemented, I'd be delighted to hear them.

update: "...draw in a [...] Component's paint(...) method if AWT, and use the Graphics object passed in by the JVM" -

Resolved, Thank you!

Solace
  • 45
  • 5

1 Answers1

5

Don't use getGraphics() for your Graphics component as this will return a non-persisting object that will get drawn over with the next repaint. Instead draw in a JComponent's paintComponent(...) method if this is a Swing app (you don't say) or a Component's paint(...) method if AWT, and use the Graphics object passed in by the JVM. Mostly read the tutorials on how to draw with Java as this has been well explained there.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Solace: Applets also need to use [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) correctly. Also consider a [hybrid](http://stackoverflow.com/a/12449949/230513) applet/application. – trashgod Dec 01 '12 at 13:21
  • Ah oversight on my part sorry; its AWT. I'll take a look for more tutorials. – Solace Dec 01 '12 at 13:42
  • 2
    @Solace It is the 3rd millennium. Time to start using Swing. – Andrew Thompson Dec 01 '12 at 13:54
  • As soon as this project is finished, I'll look into that advice. I started blind with Java in September so been playing it by ear. I can't find these tutorials Hovercraft mentioned anywhere - is it a reference to a particular site? – Solace Dec 01 '12 at 14:58
  • @Solace: please have a look here: [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) – Hovercraft Full Of Eels Dec 01 '12 at 15:06