java调用repaint无效_为什么我调用repaint方法后paint方法并没有被调用到

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值