在JFrame中分层多个组件

时间:2015-05-21 16:11:52

标签: java swing jframe jpanel components

我目前正在创建一个平台级别和机器人平台游戏。这些类是不完整的,但这里是基本结构:

/**
 * @(#)Robot.java
 *
 * Robot application
 *
 * @author 
 * @version 1.00 2015/5/15
 */

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Robot extends JComponent {

    private int xPos;
    private int yPos;
    private boolean[] skills;
    private boolean[][] collision;
    final private int xa = 1; // Subject to Change
    final private int ya = 1; // Subject to Change
    private final int BLOCK_SIZE = 16;

    public Robot() {
        xPos = BLOCK_SIZE*5; // variables for sample level
        yPos = BLOCK_SIZE*21 - 28;
    }

    /* Creates the Robot at the beginning of each stage
    *  Gives the robot skills it can use
    *  Robot receives map of where each block is for collision
    *  @startX and startY: block value
    *  @accessable and map: boolean arrays that are filled
    */
    public Robot(int startX, int startY, boolean[] accessable, boolean[][] map) {
        xPos = startX;
        yPos = startY;
        skills = accessable;
        collision = map;
    }

    // Robot moves laterally right one block
    public void moveRight() {
        if (collision[yPos][xPos + 1] == false) {
            xPos += xa;
        }
    }

    // Robot moves laterally left one block
    public void moveLeft() {
        if (collision[yPos][xPos - 1] == false) {
            xPos -= xa;
        }
    }

    // Robot moves vertically up certain distance
    public void Jump() {
        if (skills[0] == true && collision[yPos - 1][xPos] == false) {
            yPos -= ya;
        }
    }

    // Returns x-position of the robot in block number
    public int getX() {
        return xPos;
    }

    // Returns y-Position of the robot in block number
    public int getY() {
        return yPos;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        Image robot = Toolkit.getDefaultToolkit().getImage("RobotRight.png"); // robot
        g2.drawImage(robot, 28, 28, this);
    }

}

阶段如下:

/**
 * @(#)SampleLevel.java
 *
 *
 * @author 
 * @version 1.00 2015/5/15
 */

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class SampleLevel extends JComponent /*implements Environment*/ {
    // variables to determine size of blocks being used
    private final int BLOCKS_IN_ROW = 40;
    private final int BLOCKS_IN_COLUMN = 30;
    private final int BLOCK_SIZE = 16;

    // the y-value when the black meet the tiles 
    private final int TILE_BORDER = 21;

    public SampleLevel() {

    }

    // work in progress, method may not be needed
    public void fill() {
        ;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        Image img1 = Toolkit.getDefaultToolkit().getImage("BrickTile.jpg"); // the floor tile
        Image img2 = Toolkit.getDefaultToolkit().getImage("SampleBackground1.jpg"); // black background

        for (int i = 0; i < BLOCKS_IN_ROW; i++) {
            for (int j = 0; j < TILE_BORDER; j++) {
                g2.drawImage(img2, BLOCK_SIZE*i, BLOCK_SIZE*j, this);
                g2.finalize();
            }
        }

        for (int i = 0; i < BLOCKS_IN_ROW; i++) {
            for (int j = TILE_BORDER; j < BLOCKS_IN_COLUMN; j++) {
                g2.drawImage(img1, BLOCK_SIZE*i, BLOCK_SIZE*j, this);
                g2.finalize();
            }
        }

        // testing overlapping images
        g2.drawImage(img1, BLOCK_SIZE*36, BLOCK_SIZE*16, this);
        g2.drawImage(img1, BLOCK_SIZE*24, BLOCK_SIZE*13, this);

    }

}

但是,我想将这两个组件添加到主Viewer类中,但JFrame无法添加两个组件。有没有人知道在舞台上层叠机器人组件的方法?

1 个答案:

答案 0 :(得分:1)

首先,您需要修复机器人代码。基本上机器人图像应始终绘制在(0,0)。然后你需要像任何Swing组件一样对待你的机器人,这意味着你需要给出一个大小和位置。尺寸将是图像的大小,当您在屏幕上移动机器人时,位置可以变化。

然后你需要将机器人添加到舞台上。

某处您需要的代码类似于:

Robot robot = new Robot();
SampleLevel stage = new SampleLevel();
stage.add(robot);
frame.add(stage);

现在框架将绘制舞台,状态将绘制机器人。

此外,您不应该在paintComponent()方法中读取Robot图像。应该在构造函数中读取一次图像。阅读完图像后,您可以根据图像尺寸设置机器人的大小。