在JApplet中使用具有不断重新绘制的JPanel的组件

时间:2012-06-09 12:20:06

标签: java swing applet japplet

我的学校任务存在重大问题;幸运的是我早早就开始了。我们被要求使用JApplet制作儿童数学游戏。到现在为止还挺好。我设法创建了一个JPanel,然后将其添加到JApplet并保存所有图纸(JPanel内容不断重绘)。但是,每当我尝试将JLabel等Swing组件添加到JApplet内容窗格时,它都不会显示或显示已存在的迹象。我是JApplet的新手,所以如果显而易见,请不要太苛刻。

以下是代码:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.Timer;

public class CountingSheep extends JApplet
{

    final int BOARDWIDTH = 800;
    final int BOARDHEIGHT = 500;
    final int SCREENWIDTH = 800;
    final int SCREENHEIGHT = 800;
    Dimension boardDim = new Dimension(BOARDWIDTH, BOARDHEIGHT);
    Dimension screenDim = new Dimension(SCREENWIDTH, SCREENHEIGHT);
    Graphics bufferGraphics;
    Image offScreen;
    Image backgroundImage;
    Image[] sheepImage = new Image[2];
    JPanel gameBoard = new JPanel(true);
    List<Sheep> sheepArray = new ArrayList<>();
    Timer myTimer;

    public void init()
    {
        loadImages();
        initScreen();
        initBufferGraphics();
        initBoard();
        initTimer();
        sheepArray.add(new Sheep(sheepImage));
        myTimer.start();
    }

    private void loadImages()
    {
        sheepImage[0] = getImage(getDocumentBase(), "sheep.png");
        sheepImage[1] = getImage(getDocumentBase(), "sheep2.png");
        backgroundImage = getImage(getDocumentBase(), "bg.jpg");
    }

    private void initScreen()
    {
        setSize(800, 600);
        setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    }

    private void initBoard()
    {
        gameBoard.setPreferredSize(new Dimension(BOARDWIDTH, BOARDHEIGHT));
        getContentPane().add(gameBoard);
    }

    private void initBufferGraphics()
    {
        offScreen = createImage(BOARDWIDTH, BOARDHEIGHT);
        bufferGraphics = offScreen.getGraphics();
    }

    private void initTimer()
    {
        myTimer = new Timer(80, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                timerTick(e);
            }
        });
    }

    private void timerTick(ActionEvent e)
    {
        repaint();
    }

    public void paint(Graphics g)
    {
        bufferGraphics.clearRect(0, 0, BOARDWIDTH, BOARDHEIGHT);
        bufferGraphics.drawImage(backgroundImage, 0, 0, null);
        drawSheepHerd();
        moveSheepHerd();
        gameBoard.getGraphics().drawImage(offScreen, 0, 0, this);
    }

    public void drawSheepHerd()
    {
        for (Sheep s : sheepArray)
        {
            s.draw(bufferGraphics);
        }
    }

    public void moveSheepHerd()
    {
        for (Sheep s : sheepArray)
        {
            s.move();
        }
    }
}

先谢谢,希望你们能解决这个问题,因为我很难过。

1 个答案:

答案 0 :(得分:3)

总结一些我的建议:

  • 创建自己的扩展JPanel的ContentPane类,该类覆盖paintComponent(...)并绘制背景图像并显示动画。
  • 在init方法中调用JApplet上的setContentPane(...),传入此类的对象。
  • 尝试使用ContentPane的不同布局和定位。
  • 确保paintComponent(Graphics g)方法的第一行是:super.paintComponent(g),以便每次绘制时都会重置绘图。
  • JPanels默认是不透明的,你应该保留它,因为contentPanes必须是不透明的。如果您在contentPane之上添加组件并希望查看添加的组件后面的图像,则可能必须使它们不透明。