【发布时间】: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