gpt4 book ai didi

java - 不知道怎么重画

转载 作者:行者123 更新时间:2023-12-01 17:16:11 25 4
gpt4 key购买 nike

所以我有一个让马里奥跳跃的小项目。但我不知道如何重新绘制它。如果我在点击后在Main类中执行,那么整个跳跃会很生涩。

我尝试在跳转函数结束时执行此操作,但这也不起作用。

这是我的代码:

主要:

package klassid;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.Timer;


public class Main extends JComponent implements KeyListener, ActionListener{
static Hero hero;
Timer t = new Timer(500,this);

public static void main(String[] args) {
JFrame aken = new JFrame("Simple jumping simulator");
aken.setSize(600, 600);
aken.getContentPane().setBackground(new Color(255,255,255));
aken.getContentPane().add(new Main());
aken.setVisible(true);
aken.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hero.the_jump();
}

public Main(){
addKeyListener(this);
setFocusable(true);
t.start();
hero = new Hero(0, 320);
}

public void paintComponent(Graphics g){
hero.render(g, this);
g.setColor(Color.GREEN);
g.fillRect(0, 550, 600, 2);
}

@Override
public void keyPressed(KeyEvent e) {
hero.move(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
hero.move2(e.getKeyCode());
}
public void keyTyped(KeyEvent e) {}
public void actionPerformed(ActionEvent e) {}
}

还有我的英雄类:

package klassid;

import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Graphics;

public class Hero {
static Main main;
int y;
Image pilt = Toolkit.getDefaultToolkit().getImage("../mario.png");
private double height = 0, speed = 4;
public static final double gravity = 9.81;
private double x = 25;
private boolean left = false, right = false, up = false;


public Hero(int x, int y){
this.x = x;
this.y = y;
}

public void render(Graphics g, Main pohiKlass){
g.drawImage(pilt, (int) (x), (int) (500-(height*100)), 50, 50, pohiKlass);
}

public void the_jump() {
long previous = 0, start = 0;

while(true){
start= System.nanoTime();
if(previous != 0 && up){
double delta = start - previous;

height += (delta/1000000000) * speed;
speed -= (delta/1000000000) * gravity;
}
if(left)
x -= 3;
if(right)
x += 3;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(height < 0){
height = 0;
speed = 4;
up = false;
}
previous = start;
repaint();
}
}
public void move(int i){
if(i == 38)
up=true;
if(i == 37)
left=true;
if(i == 39)
right=true;
}
public void move2(int i){
if(i == 37)
left=false;
if(i == 39)
right=false;
}
}

我还尝试访问 the_jump 函数中的paintComponent,但它不起作用,因为我不知道它需要什么样的参数。

如何解决这个困惑?

最佳答案

paintComponent 方法中的第一行应该是:

super.paintComponent(g);

JComponent 会为您做很多事情,但是您需要显式调用父类(super class)方法来执行此操作。查看 Oracle 文档 here .

如果将计时器值减小到非常低的值,则可以在 actionPerformed 方法中调用 repaint()。这将为您提供“连续”重新绘制(每秒尽可能多的次数)。

关于java - 不知道怎么重画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com