2
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;

public class CountingSheep extends JApplet
{

    private Image sheepImage;
    private Image backgroundImage;
    private GameBoard gameBoard;
    private scoreBoard scoreBoard;

    public void init()
    {
        loadImages();
        gameBoard = new GameBoard(sheepImage, backgroundImage);
        scoreBoard = new scoreBoard();
        getContentPane().add(gameBoard);
        getContentPane().add(scoreBoard);
    }

    public void loadImages()
    {
        sheepImage = getImage(getDocumentBase(), "sheep.png");
        backgroundImage = getImage(getDocumentBase(), "bg.jpg");
    }
}

The program works correctly when nothing but the GameBoard class is added to the JApplet, however, when I try to add the ScoreBoard class, both Panel classes do not show on the Applet. I'm guessing this is now down to positioning? Any ideas?

EDIT: Gone back to the previously asked question Hovercraft, and found it was due to the layout of the contentPane and the order at with the components were added.

Mat
  • 202,337
  • 40
  • 393
  • 406
JohnW
  • 345
  • 1
  • 7
  • 15
  • 1
    Please reduce the code to an [SSCCE](http://sscce.org/). I'd like to see what's wrong with your code, I don't want to debug it. – Christoph Walesch Jun 10 '12 at 00:45
  • 2
    @JohnW `JApplet` by default uses a [`BorderLayout`](http://docs.oracle.com/javase/6/docs/api/java/awt/BorderLayout.html). You may want to explore using a different layout manager. I use [`GridBagLayout`](http://docs.oracle.com/javase/6/docs/api/java/awt/GridBagLayout.html) the most. See the Java tutorial ["A Visual Guide to Layout Managers."](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) Also Hovercraft Full Of Eels spoke of key bindings. See the Java tutorial ["How to Use Key Bindings."](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – creemama Jun 10 '12 at 04:38

2 Answers2

5

Some suggestions:

  • Don't draw in the paint method of a JApplet as that is a top-level window and should not be drawn directly on. Instead draw in the paintComponent(Graphics g) method of a JPanel or other JComponent, and then add that JPanel to the JApplet's contentPane.
  • Similar to his advice about the super call, your first method call in this method should be the super.paintComponent(g); which will refresh the JPanel's graphics.
  • The flicker is from your drawing directly in the JApplet's paint method. If you do as I suggest, you'll take advantage of Swing's use of double buffering.
  • Since this is a Swing application, you should avoid using KeyListeners and instead use Key Bindings.
  • Don't get the Graphics object of a component by calling `getGraphics(). The Graphics object obtained will be short-lived and thus will not persist after any repaint.

The code you've posted above is somewhat confusing to me. What are you trying to do with it? You've added components to the JApplet, and these components should handle their own graphics, and then you're painting on the JApplet as well. What kind of behavior exactly are you trying to achieve?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @JohnW: Again, you need to get rid of your paint method, again, you should not try to call `paint()` directly on any component except in exceptional circumstances (such as sometimes when printing). Finally, as I think we've discussed before, you must read the Swing graphics tutorials as your code is way off the mark. I cannot give any more specific help as your posted code is not an [sscce](http://sscce.org). Please read the link for details. – Hovercraft Full Of Eels Jun 10 '12 at 03:29
  • 1
    Edit: in fact, I've already warned you previously not to draw directly on the JApplet, but you seem to be ignoring this and my other advice, which begs the question -- why? – Hovercraft Full Of Eels Jun 10 '12 at 03:31
  • 2
    Congratulations on your membership of the AABHC (Applet Answer Badge Holder's Club)! It is a very new club in that it wasn't a 'club' until there were 2 of us. BTW - 'long established' traditions dictate that the new member buys the first round at our monthly meeting. See you Friday night at the 'Red Lion'. ;) – Andrew Thompson Jun 10 '12 at 06:05
  • 1
    @Andrew: thank you, thank you. I will gladly buy the *first* round. I suggest that the first order of business should be to elect officers. I nominate myself to be chief strategist and strike force commander. You can be club mascot. – Hovercraft Full Of Eels Jun 10 '12 at 11:43
  • 1
    *"You can be club mascot."* Do I get to wear a funny animal costume? If so, it's a done deal. :) – Andrew Thompson Jun 10 '12 at 11:44
1

In your paint method, make sure to call super.paint(g) since it is a method inherited from Container, a superclass of JApplet.

@Override
public void paint(Graphics g)
{
    super.paint(g);
    ...
}
creemama
  • 6,559
  • 3
  • 21
  • 26
  • 2
    @JohnW You have two options for that, either buffer the image (performance loss unless you're updating most of the screen anyway), or repaint only the parts of the screen that are necessary (see [the Java swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html)). – Michael Jun 10 '12 at 02:04
  • 1
    @JohnW Since your individual components are drawing themselves, there is no need to override `paint`. Removing the `paint` method in your `CountingSheep` class may fix some things. – creemama Jun 10 '12 at 03:18