编程语言
首页 > 编程语言> > java-在具有不断重新绘制的JPanel的JApplet中使用组件

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

作者:互联网

我在做这个学校作业时遇到了主要问题;幸运的是我很早就开始了.我们被要求使用JApplet制作儿童数学游戏.到现在为止还挺好.我设法创建了一个JPanel,然后将其添加到JApplet中并保存所有图形(JPanel的内容不断被重绘).但是,每当我尝试将JLabel之类的Swing组件添加到JApplet内容窗格时,它都不会显示或显示不存在的迹象.我对JApplets完全陌生,因此,如果显而易见,请不要太苛刻.

下面是代码:

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();
        }
    }
}

在此先感谢您,希望大家能弄清楚,因为我很沮丧.

解决方法:

总结一下我的一些建议:

>创建您自己的ContentPane类,该类扩展了JPanel,覆盖paintComponent(…)并绘制背景图像并显示动画.
>通过init方法在JApplet上调用setContentPane(…),以传入此类的对象.
>试用ContentPane的不同布局和位置.
>确保paintComponent(Graphics g)方法的第一行是:super.paintComponent(g),以便每次绘制时都会重置您的图形.
>默认情况下,JPanel是不透明的,您应该保留它不变,因为contentPanes必须是不透明的.如果您在contentPane顶部添加组件,并且想要查看添加的组件后面的图像,则可能必须使它们不透明.

标签:japplet,swing,applet,java
来源: https://codeday.me/bug/20191201/2079211.html