该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
paint方法是写在这个panel类里
我想的是每次按下空格的时候应该要在TC里开启一个计数器 然后每10ms刷新一次画面
package function;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.*;
import ToolKit.imageGather;
import function.Player;
import java.awt.*;
import java.io.IOException;
public class GamePanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final int WINDOW_WIDTH = 1280;
private static final int WINDOW_HEIGHT = 720;
public static int gameState = 1; // 1 = running, 0 = ending
private Player p;
TimeControl tc; // main-controller
public GamePanel() throws FontFormatException, IOException { // 初始化
this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
this.setLayout(null);
p = new Player(this); // rund-mark
tc = new TimeControl(this, p);
// define background
JButton bg = new JButton();
ImageIcon bgImg = new ImageIcon(
ToolKit.imageGather.backgroundImg[4].getScaledInstance(WINDOW_WIDTH, WINDOW_HEIGHT, 0));
bg.setIcon(bgImg);
bg.setBounds(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
// space-listener
bg.addKeyListener(new KeyListener() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
System.out.println("space pressed");
if (!p.isJumping) { // if player is standing
p.state = 0;
p.isJumping = true;
tc.timestamp = 0;
tc.startJump();
System.out.println("jumped");
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
});
this.add(bg);
this.setVisible(false);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// to-do
if (p.state == 1) { // normal state
g.drawImage(imageGather.playerImg[0], (WINDOW_WIDTH / 2 - 50) - 200, WINDOW_HEIGHT / 2 - 50 + 200, this); // mid-point (440,520) // is
} else { // jumping
g.drawImage(imageGather.playerImg[1], (WINDOW_WIDTH / 2 - 50) - 200,
(int) (WINDOW_HEIGHT / 2 - 50 + 200 + (320 - 320 * Math.pow((tc.timestamp - 500), 2) / 2500)),
this);
System.out.println("drawing jumping player");
}
System.out.println("painting");
}
}
-----
TC在这里
----
package function;
public class TimeControl{
protected int timestamp = 0;
long startTime = 0;
long curTime = 0;
boolean countDown = false;
GamePanel gp;
Player p;
TimeControl(GamePanel gp, Player p) {
this.gp = gp;
this.p = p;
}
public void startJump() throws InterruptedException{
startTime = System.currentTimeMillis();
while (timestamp < 1000) {
curTime = System.currentTimeMillis();
timestamp = (int) (curTime - startTime);
Thread.sleep(10);
System.out.println("gprepaint");
gp.repaint();
System.out.println("main count: " + timestamp);
} // end of jumping
p.isJumping = false;
p.state = 1; // normal state
}
}