如何使用Java代码实现人物在整个窗口中间不动,场景进行移动?类似于超级玛丽那样的
1个回答
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WalkMian extends JFrame implements KeyListener {
Image img;
Image back;
int x = 80, y = 450, dir = 0, num = 0;
// Main
public static void main(String args[]) {
new WalkMian();
}
// Constructor
WalkMian() {
super("Character");
img = getToolkit().getImage("c:\\IMAGE\\walk.png");
back = getToolkit().getImage("c:\\IMAGE\\back.jpg");
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.gray);
setSize(800, 610);
setVisible(true);
}
// Paint
public void paint(Graphics g) {
int yoff;
g.drawImage(back, 0, 0, this);
if (img != null) {
yoff = 144;
g.drawImage(img, x, y, x + 144, y + 144, 144, -144+144*dir, yoff + 144, 144*dir,
this);
}
}
import javax.swing.*;
import java.awt.event.*;
public class WalkMian extends JFrame implements KeyListener {
Image img;
Image back;
int x = 80, y = 450, dir = 0, num = 0;
// Main
public static void main(String args[]) {
new WalkMian();
}
// Constructor
WalkMian() {
super("Character");
img = getToolkit().getImage("c:\\IMAGE\\walk.png");
back = getToolkit().getImage("c:\\IMAGE\\back.jpg");
addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.gray);
setSize(800, 610);
setVisible(true);
}
// Paint
public void paint(Graphics g) {
int yoff;
g.drawImage(back, 0, 0, this);
if (img != null) {
yoff = 144;
g.drawImage(img, x, y, x + 144, y + 144, 144, -144+144*dir, yoff + 144, 144*dir,
this);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询