Java 如何在矩形中设置图像?
我有个小问题。我在互联网上到处搜索,却找不到如何在矩形中设置图像。这看起来相当简单,但我刚刚进入游戏编程,我似乎找不到关于这一点的解释。我是被Y的人介绍到这个网站的!安瑟斯,我想你们可以帮忙。我有以下代码:Java 如何在矩形中设置图像?,java,Java,我有个小问题。我在互联网上到处搜索,却找不到如何在矩形中设置图像。这看起来相当简单,但我刚刚进入游戏编程,我似乎找不到关于这一点的解释。我是被Y的人介绍到这个网站的!安瑟斯,我想你们可以帮忙。我有以下代码: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Racing extends JFrame { private static final long serialVersio
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Racing extends JFrame {
private static final long serialVersionUID = -198172151996959655L;
//makes the screen size
final int WIDTH = 900, HEIGHT = 650;
//keeps track of player speed
double plSpeed = .5, p2Speed = .5;
//numbers that represent direction
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3, STOP = 5;
//keeps track of player direction
int p1Direction = STOP;
int p2Direction = STOP;
//create rectangles
Rectangle left = new Rectangle( 0, 0, WIDTH/9, HEIGHT );
Rectangle right = new Rectangle( ( WIDTH/9 )*8, 0, WIDTH/9, HEIGHT );
Rectangle top = new Rectangle ( 0, 0, WIDTH, HEIGHT/9 );
Rectangle bottom = new Rectangle( 0, ( HEIGHT/9 ) * 8, WIDTH, HEIGHT/9 );
Rectangle center = new Rectangle( (int) ((WIDTH/9) * 2.5), (int) ((HEIGHT/9) * 2.5), (int) ((WIDTH/9) * 5), (int) ((HEIGHT/9) * 4));
//makes the obstacles
Rectangle obstacle = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 7), WIDTH/10, HEIGHT/9 );
Rectangle obstacle2 = new Rectangle ( WIDTH/3, (int) ((HEIGHT/9 ) * 5), WIDTH/10, HEIGHT/4 );
Rectangle obstacle3 = new Rectangle ( 2 * (WIDTH/3), (int) ((HEIGHT/9) * 5),WIDTH/10, HEIGHT/4 );
Rectangle obstacle4 = new Rectangle ( WIDTH/3, HEIGHT/9, WIDTH/30, HEIGHT/9 );
Rectangle obstacle5 = new Rectangle ( WIDTH/2, (int) ((HEIGHT/9) * 1.5), WIDTH/30, HEIGHT/4 );
//makes the finish line
Rectangle finish = new Rectangle ( WIDTH/9, (HEIGHT/2)-HEIGHT/9, (int) ((WIDTH/9) * 1.5), HEIGHT/70 );
//makes player 1's car
Rectangle p1 = new Rectangle ( WIDTH/9, HEIGHT/2, WIDTH/30, WIDTH/30 );
//makes player 2's car
Rectangle p2 = new Rectangle ((( WIDTH/9 ) + ((int) ((WIDTH/9) * 1.5) /2)),(HEIGHT/2) + (HEIGHT/10), WIDTH/30, WIDTH/30);
//constructor
public Racing() {
//define defaults for the JFrame
super ("Racing");
setSize( WIDTH, HEIGHT );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setBackground(Color.BLACK);
//start the inner class
Move2 m2 = new Move2();
m2.start();
Move1 m1 = new Move1();
m1.start();
}
//draws the cars and race track
public void paint(Graphics g) {
//make borders green
g.setColor(Color.GREEN);
//rectangles for start lines (lineO = outer player, lineI = inner player)
Rectangle lineO = new Rectangle( WIDTH/9, HEIGHT/2, (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
Rectangle lineI = new Rectangle( ((WIDTH/9)+((int) ((WIDTH/9) * 1.5)/2)), (HEIGHT/2) + (HEIGHT/10), (int) ((WIDTH/9) * 1.5)/2, HEIGHT/140 );
//fill the rectangles
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//make the starting line color white
g.setColor(Color.WHITE);
//draw starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
//make the finish line yellow
g.setColor(Color.YELLOW);
//draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);
//make player one red
g.setColor(Color.RED);
//draw player 1
g.fill3DRect(p1.x,p1.y,p1.width,p2.height,true);
//make player two blue
g.setColor(Color.BLUE);
//now draw player two
g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
}
private class Move1 extends Thread implements KeyListener {
public void run() {
//makes the key listener "wake up"
addKeyListener(this);
//should be done in an infinite loop, so it repeats
while (true) {
//make try block, so it can exit if it errors
try {
//refresh screen
repaint();
//check to see if car hits wall
//if so, slow down
if (p1.intersects(left) || p1.intersects(right) ||
p1.intersects(top) || p1.intersects(bottom) ||
p1.intersects(obstacle) || p1.intersects(obstacle2) ||
p1.intersects(obstacle3) || p1.intersects(obstacle4) ||
p1.intersects(obstacle5) || p1.intersects (center) ||
p1.intersects(p2)) {
plSpeed = -5;
Thread.sleep(128);
p1Direction = STOP;
}
//lets the car stop
if (plSpeed==0) {
p1Direction = STOP;
}
//moves player based on direction
if (p1Direction==UP) {
p1.y -= (int) plSpeed;
}
if (p1Direction==DOWN) {
p1.y += (int) plSpeed;
}
if (p1Direction==LEFT) {
p1.x -= (int) plSpeed;
}
if (p1Direction==RIGHT) {
p1.x += (int) plSpeed;
}
if (p1Direction==STOP) {
plSpeed = 0;
}
//delays refresh rate
Thread.sleep(75);
}
catch(Exception e) {
//if an error, exit
break;
}
}
}
//have to input these (so it will compile)
public void keyPressed(KeyEvent event) {
try {
//makes car increase speed a bit
if (event.getKeyChar()=='w' ||
event.getKeyChar()=='a' ||
event.getKeyChar()=='s' ||
event.getKeyChar()=='d') {
plSpeed += .2;
repaint();
}
} catch (Exception I) {}
}
public void keyReleased(KeyEvent event) {}
//now, to be able to set the direction
public void keyTyped(KeyEvent event) {
if (event.getKeyChar()=='a') {
if (p1Direction==RIGHT) {
p1Brake();
} else {
if (p1Direction==LEFT) {
} else {
plSpeed = .5;
p1Direction = LEFT;
}
}
}
if (event.getKeyChar()=='s') {
if (p1Direction==UP) {
p1Brake();
} else {
if (p1Direction==DOWN) {
} else {
plSpeed = .5;
p1Direction = DOWN;
}
}
}
if (event.getKeyChar()=='d') {
if (p1Direction==LEFT) {
p1Brake();
} else {
if (p1Direction==RIGHT) {
} else {
plSpeed = .5;
p1Direction = RIGHT;
}
}
}
if (event.getKeyChar()=='w') {
if (p1Direction==DOWN) {
p1Brake();
} else {
if (p1Direction==UP) {
} else {
plSpeed = .5;
p1Direction = UP;
}
}
}
if (event.getKeyChar()=='z') {
p1Brake();
}
}
public void p1Brake () {
try {
while (plSpeed != 0) {
plSpeed -= .2;
Thread.sleep(75);
}
} catch (Exception e) {
plSpeed = 0;
}
}
}
private class Move2 extends Thread implements KeyListener {
public void run() {
//makes the key listener "wake up"
addKeyListener(this);
//should be done in an infinite loop, so it repeats
while (true) {
//make try block, so it can exit if it errors
try {
//refresh screen
repaint();
//check to see if car hits outside wall
//if so, slow down
if (p2.intersects(left) || p2.intersects(right) ||
p2.intersects(top) || p2.intersects(bottom) ||
p2.intersects(obstacle) || p2.intersects(obstacle2) ||
p2.intersects(obstacle3) || p2.intersects(obstacle4) ||
p2.intersects(obstacle5) || p2.intersects(p1) ||
p2.intersects(center)) {
plSpeed = -.25;
Thread.sleep(128);
p2Direction = STOP;
}
//makes car increase speed a bit
if (p2Speed <= 5) {
plSpeed += .2;
}
//lets the car stop
if (p2Speed == 0) {
p2Direction = STOP;
}
//moves player based on direction
if (p2Direction==UP) {
p2.y -= (int) p2Speed;
}
if (p2Direction==DOWN) {
p2.y += (int) p2Speed;
}
if (p2Direction==LEFT) {
p2.x -= (int) p2Speed;
}
if (p2Direction==RIGHT) {
p2.x += (int) p2Speed;
}
if (p2Direction==STOP) {
p2Speed = 0;
}
//delays refresh rate
Thread.sleep(75);
}
catch(Exception e) {
//if an error, exit
break;
}
}
}
public void keyPressed(KeyEvent event) {
try {
//makes car increase speed a bit
if (event.getKeyChar()=='j' ||
event.getKeyChar()=='k' ||
event.getKeyChar()=='l' ||
event.getKeyChar()=='i') {
plSpeed += .2;
}
} catch (Exception I) {}
}
public void keyReleased(KeyEvent event) {}
//now, to be able to set the direction
public void keyTyped(KeyEvent event) {
if (event.getKeyChar()=='j') {
if (p2Direction==RIGHT) {
p2Brake();
} else {
p2Direction = LEFT;
p2Speed = .4;
}
}
if (event.getKeyChar()=='k') {
if (p2Direction==UP) {
p2Brake();
} else {
p2Direction = DOWN;
p2Speed = .4;
}
}
if (event.getKeyChar()=='l') {
if (p2Direction==LEFT) {
p2Brake();
} else {
p2Direction = RIGHT;
p2Speed = .4;
}
}
if (event.getKeyChar()=='i') {
if (p2Direction==DOWN) {
p2Brake();
} else {
p2Direction = UP;
p2Speed = .4;
}
}
if (event.getKeyChar()=='m') {
p2Brake();
}
}
public void p2Brake () {
try {
while (p2Speed != 0) {
p2Speed -= .5;
Thread.sleep(75);
}
} catch (Exception i) {
p2Speed = 0;
}
}
}
//finally, to start the program
public static void main(String[] args) {
Racing frame = new Racing();
frame.setVisible( true );
frame.setLocationRelativeTo( null );
frame.setResizable( false );
}
}
import javax.swing.*;
导入java.awt.*;
导入java.awt.event.*;
公共级赛车扩展JFrame{
私有静态最终长serialVersionUID=-198172151996959655L;
//使屏幕变大
最终内部宽度=900,高度=650;
//跟踪玩家的速度
双plSpeed=.5,p2Speed=.5;
//表示方向的数字
最终整数向上=0,右=1,向下=2,左=3,停止=5;
//跟踪玩家的方向
方向=停止;
方向=停止;
//创建矩形
左矩形=新矩形(0,0,宽度/9,高度);
右矩形=新矩形((宽度/9)*8,0,宽度/9,高度);
矩形顶部=新矩形(0,0,宽度,高度/9);
矩形底部=新矩形(0,(高度/9)*8,宽度,高度/9);
矩形中心=新矩形((int)((宽度/9)*2.5),(int)((高度/9)*2.5),(int)((宽度/9)*5),(int)((高度/9)*4));
//制造障碍
矩形障碍物=新矩形(宽度/2,(整数)((高度/9)*7),宽度/10,高度/9);
矩形障碍2=新矩形(宽度/3,(整数)((高度/9)*5),宽度/10,高度/4);
矩形obstacle3=新矩形(2*(宽度/3),(int)((高度/9)*5),宽度/10,高度/4);
矩形obstacle4=新矩形(宽度/3,高度/9,宽度/30,高度/9);
矩形障碍5=新矩形(宽度/2,(整数)((高度/9)*1.5),宽度/30,高度/4);
//到达终点线
矩形饰面=新矩形(宽度/9,(高度/2)-高度/9,(内部)(宽度/9)*1.5),高度/70);
//制造玩家1的汽车
矩形p1=新矩形(宽度/9,高度/2,宽度/30,宽度/30);
//制造玩家2的汽车
矩形p2=新矩形((宽度/9)+(int)((宽度/9)*1.5)/2),(高度/2)+(高度/10),宽度/30,宽度/30);
//建造师
公众赛马{
//定义JFrame的默认值
超级(“赛车”);
设置尺寸(宽度、高度);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
挫折背景(颜色:黑色);
//启动内部类
Move2 m2=新的Move2();
m2.start();
Move1 m1=新的Move1();
m1.start();
}
//绘制赛车和赛道
公共空间涂料(图g){
//使边框变绿
g、 setColor(Color.GREEN);
//起始线的矩形(lineO=外部播放器,lineI=内部播放器)
矩形线条o=新矩形(宽度/9,高度/2,(整数)((宽度/9)*1.5)/2,高度/140);
矩形线条i=新矩形((宽度/9)+(整型)((宽度/9)*1.5)/2),(高度/2)+(高度/10),(整型)((宽度/9)*1.5)/2,高度/140);
//填充矩形
g、 fillRect(left.x,left.y,left.width,left.height);
g、 fillRect(right.x,right.y,right.width,right.height);
g、 fillRect(top.x,top.y,top.width,top.height);
g、 fillRect(底部.x,底部.y,底部.宽度,底部.高度);
g、 fillRect(center.x,center.y,center.width,center.height);
g、 fillRect(障碍物.x,障碍物.y,障碍物.宽度,障碍物.高度);
g、 fillRect(obstacle2.x、obstacle2.y、obstacle2.width、obstacle2.height);
g、 fillRect(obstacle3.x、obstacle3.y、obstacle3.width、obstacle3.height);
g、 fillRect(obstacle4.x、obstacle4.y、obstacle4.width、obstacle4.height);
g、 fillRect(obstacle5.x、obstacle5.y、obstacle5.width、obstacle5.height);
//使起跑线颜色为白色
g、 setColor(Color.WHITE);
//画起跑线
g、 fillRect(线条x、线条y、线条宽度、线条高度);
g、 fillRect(lineI.x、lineI.y、lineI.width、lineI.height);
//把终点线变成黄色
g、 setColor(颜色为黄色);
//画终点线
g、 fillRect(finish.x,finish.y,finish.width,finish.height);
//把一号球员换成红色
g、 setColor(Color.RED);
//平局球员1
g、 fill3DRect(p1.x,p1.y,p1.width,p2.height,true);
//让球员2变成蓝色
g、 setColor(Color.BLUE);
//现在抽签第二名球员
g、 fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
}
私有类Move1扩展线程实现KeyListener{
公开募捐{
//使键侦听器“唤醒”
addKeyListener(此);
//应该在无限循环中完成,因此它会重复
while(true){
//创建try块,以便在出现错误时退出
试一试{
//刷新屏幕
重新油漆();
//检查汽车是否撞到墙上
//如果是这样,放慢速度
如果(p1.相交(左)| | p1.相交(右)||
p1.相交(顶部)| | p1.相交(底部)||
p1.交叉(障碍物)| p1.交叉(障碍物)||
p1.相交(障碍物3)| | p1.相交(障碍物4)||
p1.相交(障碍物5)| | p1.相交(中心)||
p1.相交(p2)){
plSpeed=-5;
睡眠(128);
p1方向=停止;
}
//让汽车停下来
如果(plSpeed==0){
p1方向=停止;
}
//根据方向移动玩家
如果(p1方向==向上){
p1.y-=(int)plSpeed;
}
如果(p1方向==向下){
p1.y+=(int)plSpeed;