【问题标题】:Layer multiple components in JFrame在 JFrame 中分层多个组件
【发布时间】:2015-08-03 10:29:42
【问题描述】:

我目前正在创建一个包含 Level 和 Robot 类的平台游戏。类不完整,但基本结构如下:

/**
 * @(#)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 无法添加两个组件。有谁知道将机器人组件分层放置在舞台顶部的方法?

【问题讨论】:

    标签: java swing jframe jpanel components


    【解决方案1】:

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

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

    您需要类似以下代码的地方:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2016-07-06
      相关资源
      最近更新 更多