Java 如何使图像优先于while循环绘图背景

Java 如何使图像优先于while循环绘图背景,java,multithreading,graphics,keylistener,thread-priority,Java,Multithreading,Graphics,Keylistener,Thread Priority,所以基本上几天来,我一直在试图找出我用箭头键移动我的“超人”物体的关键点。这仅在我/***/绘制“移动”道路的while循环时有效。一旦它被取消注释,我运行程序,超人就不会移动,我认为这是因为循环优先于图像。我相信我需要帮助来优先处理图像。非常感谢你的帮助 // The "CPT" class. import java.applet.*; import java.awt.*; import java.awt.Image; import java.awt.event.*; import javax

所以基本上几天来,我一直在试图找出我用箭头键移动我的“超人”物体的关键点。这仅在我/***/绘制“移动”道路的while循环时有效。一旦它被取消注释,我运行程序,超人就不会移动,我认为这是因为循环优先于图像。我相信我需要帮助来优先处理图像。非常感谢你的帮助

// The "CPT" class.
import java.applet.*;
import java.awt.*;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Runnable;
import java.lang.Thread;



public class CPT extends Applet implements KeyListener
{
    // Place instance variables here
    Image myImage;
    int x = 100, y = 200;

    public void init ()
    {

        myImage = getImage (getCodeBase (), "SuperMan.png");
        System.out.println (getCodeBase ());
        addKeyListener (this);
        // Place the body of the initialization method here
    } // init method





    public void paint (Graphics g)
    {




       //Drawing sky
        g.setColor (new Color (150, 150, 254));
        g.fillRect (0, 0, 1000, 500);
        //Drawing floor
        g.setColor (Color.black);
        g.fillRect (0, 500, 1000, 150);
        //Drawing road lines





        //g.fillRect (
        //Drawing Sun
        g.setColor (Color.yellow);
        g.fillOval (800, 50, 75, 75);

        //Drawing Birds
        g.setColor (Color.black);
        g.drawArc (100, 75, 25, 25, 0, 180);
        g.drawArc (125, 75, 25, 25, 0, 180);

        g.drawArc (175, 85, 25, 25, 0, 180);
        g.drawArc (200, 85, 25, 25, 0, 180);

        g.drawArc (250, 65, 25, 25, 0, 180);
        g.drawArc (275, 65, 25, 25, 0, 180);

        //Drawing Smaller Birds

        g.drawArc (125, 55, 15, 15, 0, 180);
        g.drawArc (140, 55, 15, 15, 0, 180);
        while (true)
        {

            g.setColor (Color.black);
            g.fillRect (-30, 575, 80, 25);
            g.fillRect (120, 575, 80, 25);
            g.fillRect (270, 575, 80, 25);
            g.fillRect (420, 575, 80, 25);
            g.fillRect (570, 575, 80, 25);
            g.fillRect (720, 575, 80, 25);
            g.fillRect (870, 575, 80, 25);
            //first blocks
            g.setColor (Color.yellow);
            g.fillRect (50, 575, 80, 25);
            g.fillRect (200, 575, 80, 25);
            g.fillRect (350, 575, 80, 25);
            g.fillRect (500, 575, 80, 25);
            g.fillRect (650, 575, 80, 25);
            g.fillRect (800, 575, 80, 25);
            g.fillRect (950, 575, 80, 25);





            for (int k = 0 ; k < 100000000 ; k++)
                ;
            //second blocks

            g.setColor (Color.black);
            g.fillRect (50, 575, 80, 25);
            g.fillRect (200, 575, 80, 25);
            g.fillRect (350, 575, 80, 25);
            g.fillRect (500, 575, 80, 25);
            g.fillRect (650, 575, 80, 25);
            g.fillRect (800, 575, 80, 25);
            g.fillRect (950, 575, 80, 25);

            g.setColor (Color.orange);
            g.fillRect (10, 575, 80, 25);
            g.fillRect (160, 575, 80, 25);
            g.fillRect (310, 575, 80, 25);
            g.fillRect (460, 575, 80, 25);
            g.fillRect (610, 575, 80, 25);
            g.fillRect (760, 575, 80, 25);
            g.fillRect (910, 575, 80, 25);

            for (int z = 0 ; z < 100000000 ; z++)
                ;


            //third blocks

            g.setColor (Color.black);
            g.fillRect (10, 575, 80, 25);
            g.fillRect (160, 575, 80, 25);
            g.fillRect (310, 575, 80, 25);
            g.fillRect (460, 575, 80, 25);
            g.fillRect (610, 575, 80, 25);
            g.fillRect (760, 575, 80, 25);
            g.fillRect (910, 575, 80, 25);


            g.setColor (Color.yellow);
            g.fillRect (-30, 575, 80, 25);
            g.fillRect (120, 575, 80, 25);
            g.fillRect (270, 575, 80, 25);
            g.fillRect (420, 575, 80, 25);
            g.fillRect (570, 575, 80, 25);
            g.fillRect (720, 575, 80, 25);
            g.fillRect (870, 575, 80, 25);

            for (int t = 0 ; t < 100000000 ; t++)
                ;



            g.drawImage (myImage, x, y, 100, 40, this);

        }
    } // paint method


    public void keyReleased (KeyEvent e)
    {


    }


    public void keyPressed (KeyEvent e)
    {
        if (e.getKeyCode () == KeyEvent.VK_RIGHT)
        {
            // System.out.println ("Right Key Pressed");
            x += 10;
        }

        if (e.getKeyCode () == KeyEvent.VK_LEFT)
        {
            // System.out.println ("Left Key Pressed");
            x -= 10;
        }

        if (e.getKeyCode () == KeyEvent.VK_UP)
        {
        y -= 10;
        }

        if (e.getKeyCode () == KeyEvent.VK_DOWN)
        {
        y+=10;
        }
        repaint ();
    }



    public void keyTyped (KeyEvent e)
    {

    }
} // CPT class
//“CPT”类。
导入java.applet.*;
导入java.awt.*;
导入java.awt.Image;
导入java.awt.event.*;
导入javax.swing.*;
导入java.lang.Runnable;
导入java.lang.Thread;
公共类CPT扩展小程序实现KeyListener
{
//在此处放置实例变量
图像我的图像;
int x=100,y=200;
公共void init()
{
myImage=getImage(getCodeBase(),“SuperMan.png”);
System.out.println(getCodeBase());
addKeyListener(此);
//将初始化方法的主体放在这里
}//初始化方法
公共空间涂料(图g)
{
//画天
g、 setColor(新颜色(150150254));
g、 fillRect(0,0,1000,500);
//客厅
g、 设置颜色(颜色为黑色);
g、 fillRect(0,500,1000,150);
//画道路线
//g、 fillRect(
//画太阳
g、 setColor(颜色为黄色);
g、 圆形(800,50,75,75);
//画鸟
g、 设置颜色(颜色为黑色);
g、 牵引弧(100,75,25,25,0,180);
g、 牵引弧(125,75,25,25,0,180);
g、 牵引弧(175,85,25,25,0,180);
g、 牵引弧(200,85,25,25,0,180);
g、 牵引弧(250,65,25,25,0,180);
g、 牵引弧(275,65,25,25,0,180);
//画小鸟
g、 牵引弧(125,55,15,15,0,180);
g、 牵引弧(140,55,15,15,0,180);
while(true)
{
g、 设置颜色(颜色为黑色);
g、 fillRect(-30575,80,25);
g、 fillRect(120575,80,25);
g、 fillRect(270575,80,25);
g、 fillRect(420、575、80、25);
g、 fillRect(570575,80,25);
g、 fillRect(720575,80,25);
g、 fillRect(870575,80,25);
//第一个街区
g、 setColor(颜色为黄色);
g、 fillRect(50575,80,25);
g、 fillRect(200575,80,25);
g、 fillRect(350575,80,25);
g、 fillRect(500575,80,25);
g、 fillRect(650、575、80、25);
g、 fillRect(800575,80,25);
g、 fillRect(950575,80,25);
对于(int k=0;k<100000000;k++)
;
//第二街区
g、 设置颜色(颜色为黑色);
g、 fillRect(50575,80,25);
g、 fillRect(200575,80,25);
g、 fillRect(350575,80,25);
g、 fillRect(500575,80,25);
g、 fillRect(650、575、80、25);
g、 fillRect(800575,80,25);
g、 fillRect(950575,80,25);
g、 setColor(颜色为橙色);
g、 fillRect(10575,80,25);
g、 fillRect(160575,80,25);
g、 fillRect(310575,80,25);
g、 fillRect(460575,80,25);
g、 fillRect(610575,80,25);
g、 fillRect(760575,80,25);
g、 fillRect(910575,80,25);
对于(int z=0;z<100000000;z++)
;
//第三街区
g、 设置颜色(颜色为黑色);
g、 fillRect(10575,80,25);
g、 fillRect(160575,80,25);
g、 fillRect(310575,80,25);
g、 fillRect(460575,80,25);
g、 fillRect(610575,80,25);
g、 fillRect(760575,80,25);
g、 fillRect(910575,80,25);
g、 setColor(颜色为黄色);
g、 fillRect(-30575,80,25);
g、 fillRect(120575,80,25);
g、 fillRect(270575,80,25);
g、 fillRect(420、575、80、25);
g、 fillRect(570575,80,25);
g、 fillRect(720575,80,25);
g、 fillRect(870575,80,25);
对于(int t=0;t<100000000;t++)
;
g、 drawImage(myImage,x,y,100,40,this);
}
}//绘制方法
公共无效密钥已释放(密钥事件e)
{
}
按下公共无效键(按键事件e)
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
//System.out.println(“按下右键”);
x+=10;
}
if(e.getKeyCode()==KeyEvent.VK_左)
{
//System.out.println(“按下左键”);
x-=10;
}
if(e.getKeyCode()==KeyEvent.VK\u UP)
{
y-=10;
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
y+=10;
}
重新油漆();
}
public void keyTyped(KeyEvent e)
{
}
}//CPT类

while loop
将冻结整个应用程序。它将继续在图形上绘制内容,但不允许Swing事件调度线程使用图形刷新屏幕

一些建议:

1) 将while循环移到
paint
方法之外,并移到单独的线程中。更好的方法是阅读如何在JavaSwing中制作正确的动画

2) 了解如何正确覆盖回转组件
paint
方法

3) 阅读什么是事件分派线程以及为什么必须避免阻塞它