Java 在不使用repaint()方法更新整个屏幕的情况下,哪些代码用于绘制单个对象?

Java 在不使用repaint()方法更新整个屏幕的情况下,哪些代码用于绘制单个对象?,java,swing,graphics,Java,Swing,Graphics,我正在创建一个视频游戏,当我调用repaint()方法时,整个屏幕会不规则地更新和闪烁。通过在这行代码中指定重新绘制的内容(charRect,这是一个矩形对象),我稍微解决了这个问题:重新绘制(charRect.x,charRect.y,20,32);整个屏幕都停止闪烁,但这个角色令人难以置信地闪烁。我只需要帮助让动画从移动(a和d)和跳跃(w)平稳运行 import javax.swing.*; import javax.swing.event.*; import java.awt.*; im

我正在创建一个视频游戏,当我调用repaint()方法时,整个屏幕会不规则地更新和闪烁。通过在这行代码中指定重新绘制的内容(charRect,这是一个矩形对象),我稍微解决了这个问题:重新绘制(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;
            }
        }
    }
}
import javax.swing.*;
导入javax.swing.event.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.awt.geom.*;
导入java.util.*;
导入java.io.*;
导入java.net。*;
导入java.lang.*;
导入静态java.lang.Math.*;
导入静态java.lang.System.out;
导入java.util.Random.*;
导入javax.swing.JApplet;
导入java.awt.Graphics;
公共类UltimaBlade扩展JApplet实现KeyListener
{
公共最终整数宽度=900;
公共最终内部高度=650;
公共智力得分=0;
私有布尔跳跃=假;
私有布尔moveRight=false;
私有布尔moveLeft=false;
private boolean jumpRight=false;
private boolean jumpLeft=false;
私有布尔值moveRightWhileJumping=false;
私有布尔moveLeftWhileJumping=false;
公共JFrame JTitle=新JFrame(“最终许可”);
图像特征mg=null;
图像块img1=null;
图像floorImg1=null;
图像wallImg1=null;
JLabel scoreLabel=新的JLabel(“分数:+分数”);
矩形charRect=新矩形(25450,20,32);
矩形块rect1=新矩形(25482,30,30);
矩形地板修正1=新矩形(254822001);更改回30
矩形墙rect1=新矩形(55490,1,30);
//矩形tempGhost=新矩形(charRect.x,charRect.y,20,32);
public JMenuBar mb=new JMenuBar();
公共JMenu菜单文件=新JMenu(“文件”);
public JMenuItem menuItemSave=新建JMenuItem(“保存”);
public JMenuItem menuItemLoad=新JMenuItem(“加载”);
集装箱运输;
跑步者;
公共void init()
{
设置尺寸(宽度、高度);
JTitle.setJMenuBar(mb);
menuFile.add(menuItemSave);
menuFile.add(menuItemLoad);
mb.add(菜单文件);
JTitle.getContentPane().setLayout(新的BorderLayout());
menuItemSave.addActionListener(newlistenmenusave());
setJMenuBar(mb);
新的UltimaBlade();
}
公共最终许可证()
{
setLayout(空);
设置尺寸(宽度、高度);
setVisible(真);
cont=getContentPane();
cont.setLayout(空);
addKeyListener(此);
接地(颜色为白色);
继续添加(分数标签);
分数标签。挫折(50,50,100,30);
scoreLabel.setForeground(颜色:黑色);
cont.setLayout(空);
重新油漆();
继续验证();
runner=新runner();
runner.start();
setContentPane(续);
}
公共类ListenMenuSave实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
新保存();
}
}
公共空间涂料(图g)
{
超级油漆(g);
尝试
{
URL url1=this.getClass().getResource(“character.gif”);//字符
characteristmg=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”);//墙线
wallImg1=Toolkit.getDefaultToolkit().getImage(url4);
}
捕获(例外e)
{
}
g、 drawImage(characteristimg,charRect.x,charRect.y,this);
g、 drawImage(floorImg1,FloorCorrect1.x,FloorCorrect1.y,this);
g、 drawImage(blockImg1,blockRect1.x,blockRect1.y,this);
g、 drawImage(wallImg1,wallRect1.x,wallRect1.y,this);
}
按下公共无效键(按键事件e)
{
如果(e.getKeyChar()='d'| | e.getKeyChar()=='d')
{
moveRight=true;
如果(跳跃)
moveRightWhileJumping=真;
}
如果(e.getKeyChar()='a'| | e.getKeyChar()=='a')
{
moveLeft=true;
如果(跳跃)
moveLeftWhileJumping=true;
}
}
公共无效密钥已释放(密钥事件e)
{
如果(e.getKeyChar()='d'| | e.getKeyChar()=='d')
{
moveRight=false;
如果(!跳跃)
moveRightWhileJumping=false;
}
如果(e.getKeyChar()='a'| | e.getKeyChar()=='a')
{
moveLeft=false;
如果(!跳跃)
moveLeftWhileJumping=false;
}
}
public void keyTyped(KeyEvent e)
{
如果(e.getKeyChar()='w'| | e.getKeyChar()=='w')
{
跳跃=正确;
}
}
公共类运行程序扩展线程//实现可运行
{
公开募捐
{
while(true)
{
尝试
{
int j=5;//起始速度
双t=0;
双和=j/5;
边跳
{
重新喷漆(charRect.x、charRect.y、20、32);
继续验证();
int跳跃=(int)(-4.9*((t*t)/10))+(j*t));
if(跳跃时向右移动)
{
charRect.x=charRect.x+j;//跳跃时向右移动。
if(字符相交(wallRect1))
{
charRect.x=wallRect1.x-21;
moveRightWhileJumping=false;
}