gpt4 book ai didi

java - 什么代码用于绘制单个对象,而不使用 repaint() 方法更新整个屏幕?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:56:45 30 4
gpt4 key购买 nike

我正在创建一个视频游戏,当我调用 repaint() 方法时,整个屏幕会不规则地更新和闪烁。我通过在这行代码中指定重绘的内容(charRect,它是一个矩形对象)稍微解决了这个问题: repaint(charRect.x, charRect.y, 20, 32);整个屏幕停止闪烁,但角色出现了令人难以置信的故障。我只需要帮助让动画从移动(a 和 d)和跳跃(w)顺利运行。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.Random.*;
import javax.swing.JApplet;
import java.awt.Graphics;

public class UltimaBlade extends JApplet implements KeyListener
{
public final int WIDTH = 900;
public final int HEIGHT = 650;

public int score = 0;
private boolean jumping = false;
private boolean moveRight = false;
private boolean moveLeft = false;
private boolean jumpRight = false;
private boolean jumpLeft = false;
private boolean moveRightWhileJumping = false;
private boolean moveLeftWhileJumping = false;

public JFrame JTitle = new JFrame("UltimaBlade");
Image characterImg = null;
Image blockImg1 = null;
Image floorImg1 = null;
Image wallImg1 = null;
JLabel scoreLabel = new JLabel("Score: " + score);

Rectangle charRect = new Rectangle(25, 450, 20, 32);
Rectangle blockRect1 = new Rectangle(25, 482, 30, 30);
Rectangle floorRect1 = new Rectangle(25, 482, 200, 1);CHANGE BACK TO 30
Rectangle wallRect1 = new Rectangle(55, 490, 1, 30);
//Rectangle tempGhost = new Rectangle(charRect.x, charRect.y, 20, 32);

public JMenuBar mb = new JMenuBar();
public JMenu menuFile = new JMenu("File");
public JMenuItem menuItemSave = new JMenuItem("Save");
public JMenuItem menuItemLoad = new JMenuItem("Load");
Container cont;
Runner runner;

public void init()
{
setSize(WIDTH, HEIGHT);

JTitle.setJMenuBar(mb);
menuFile.add(menuItemSave);
menuFile.add(menuItemLoad);
mb.add(menuFile);
JTitle.getContentPane().setLayout(new BorderLayout());
menuItemSave.addActionListener(new ListenMenuSave());
setJMenuBar(mb);
new UltimaBlade();
}

public UltimaBlade()
{
setLayout(null);
setSize(WIDTH, HEIGHT);
setVisible(true);

cont = getContentPane();
cont.setLayout(null);
addKeyListener(this);
cont.setBackground(Color.WHITE);

cont.add(scoreLabel);
scoreLabel.setBounds(50, 50, 100, 30);
scoreLabel.setForeground(Color.BLACK);
cont.setLayout(null);

repaint();
cont.validate();

runner = new Runner();
runner.start();
setContentPane(cont);
}

public class ListenMenuSave implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
new Save();
}
}

public void paint(Graphics g)
{
super.paint(g);

try
{
URL url1 = this.getClass().getResource("character.gif"); //Character
characterImg = Toolkit.getDefaultToolkit().getImage(url1);
URL url2 = this.getClass().getResource("block.gif"); //Block
blockImg1 = Toolkit.getDefaultToolkit().getImage(url2);
URL url3 = this.getClass().getResource("floor_line.gif"); //Floor Line
floorImg1 = Toolkit.getDefaultToolkit().getImage(url3);
URL url4 = this.getClass().getResource("wall_line.gif"); //Wall Line
wallImg1 = Toolkit.getDefaultToolkit().getImage(url4);
}
catch(Exception e)
{

}

g.drawImage(characterImg, charRect.x, charRect.y, this);
g.drawImage(floorImg1, floorRect1.x, floorRect1.y, this);
g.drawImage(blockImg1, blockRect1.x, blockRect1.y, this);
g.drawImage(wallImg1, wallRect1.x, wallRect1.y, this);
}

public void keyPressed(KeyEvent e)
{
if(e.getKeyChar() == 'd' || e.getKeyChar() == 'D')
{
moveRight = true;
if(jumping)
moveRightWhileJumping = true;
}
if(e.getKeyChar() == 'a' || e.getKeyChar() == 'A')
{
moveLeft = true;
if(jumping)
moveLeftWhileJumping = true;
}
}

public void keyReleased(KeyEvent e)
{
if(e.getKeyChar() == 'd' || e.getKeyChar() == 'D')
{
moveRight = false;
if(!jumping)
moveRightWhileJumping = false;
}
if(e.getKeyChar() == 'a' || e.getKeyChar() == 'A')
{
moveLeft = false;
if(!jumping)
moveLeftWhileJumping = false;
}
}

public void keyTyped(KeyEvent e)
{
if(e.getKeyChar() == 'w' || e.getKeyChar() == 'W')
{
jumping = true;
}
}

public class Runner extends Thread// implements Runnable
{
public void run()
{
while(true)
{
try
{
int j = 5; //Beginning velocity
double t = 0;
double sum = j/5;
while(jumping)
{
repaint(charRect.x, charRect.y, 20, 32);
cont.validate();
int jump = (int)((-4.9 * ((t * t)/10)) + (j * t));
if(moveRightWhileJumping)
{
charRect.x = charRect.x + j; // Move Right while jumping.
if(charRect.intersects(wallRect1))
{
charRect.x = wallRect1.x - 21;
moveRightWhileJumping = false;
}
}
if(moveLeftWhileJumping)
{
charRect.x = charRect.x - j; //Move Left while jumping.
if(charRect.intersects(wallRect1))
{
charRect.x = wallRect1.x + 1;
moveLeftWhileJumping = false;
}
}
if(jump > 0)
{
charRect.y = charRect.y - Math.abs(jump);
if(charRect.intersects(floorRect1))
{
charRect.y = floorRect1.y - 32;
jumping = false;
moveRightWhileJumping = false;
moveLeftWhileJumping = false;
break;
}
}
else if(jump < 0)
{
charRect.y = charRect.y + Math.abs(jump);
if(charRect.intersects(floorRect1))
{
charRect.y = floorRect1.y - 32;
jumping = false;
moveRightWhileJumping = false;
moveLeftWhileJumping = false;
break;
}
}
else
{
if(charRect.intersects(floorRect1))
{
charRect.y = floorRect1.y - 32;
jumping = false;
moveRightWhileJumping = false;
moveLeftWhileJumping = false;
break;
}
}

t++;
Thread.sleep(30);
}


while(moveLeft)
{
repaint(charRect.x, charRect.y, 20, 32);
cont.validate();
charRect.x = charRect.x - j; //Move Left speed.
if(charRect.intersects(wallRect1))
{
charRect.x = wallRect1.x + 1;
}
if(charRect.intersects(floorRect1))
{
charRect.y = floorRect1.y - 32;
jumping = false;
moveRightWhileJumping = false;
moveLeftWhileJumping = false;
break;
}
t++;
Thread.sleep(30);
}


while(moveRight)
{
repaint(charRect.x, charRect.y, 20, 32);
cont.validate();
charRect.x = charRect.x + j; //Move Left speed.
if(charRect.intersects(wallRect1))
{
charRect.x = wallRect1.x + 1;
}
if(charRect.intersects(floorRect1))
{
charRect.y = floorRect1.y - 32;
jumping = false;
moveRightWhileJumping = false;
moveLeftWhileJumping = false;
break;
}
t++;
Thread.sleep(30);
}
}
catch(Exception e)
{
break;
}
}
}
}

最佳答案

您的主要问题之一是您直接在 JApplet 本身的 paint(...) 方法中绘制,而不是在 paintComponent(...) 方法中绘制小程序持有的 JPanel 或 JCompnent 的。如果您选择后者,您将获得 Swing 的默认双缓冲的好处,这可能会产生很大的不同。此外,您可以使用 repaint(...) 重载之一重新绘制 GUI 的较小矩形,但这是次要的。首先你应该解决你缺乏双缓冲的问题。

请从本教程开始学习基础知识:Lesson: Performing Custom Painting

然后继续阅读本文,了解有关在 Swing 中绘图的更多高级信息:Painting in AWT and Swing

编辑
此外,您不应该从 paint(...)paintComponent(...) 方法中读取文件,因为这会减慢绘制速度,从而降低感知响应能力您的 GUI 数倍。为什么不简单地在小程序启动时读入一次文件,然后将它们的内容存储到一个变量中?

关于java - 什么代码用于绘制单个对象,而不使用 repaint() 方法更新整个屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405178/

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