当前位置: 首页 > 面试题库 >

重涂树叶痕迹

章高朗
2023-03-14
问题内容

我知道这不是第一次问这个问题,但是回答并没有给我带来太大帮助,所以我正在帮助我最终将得到我的答案

我做了这个小游戏,我在轨道上开车(必须使用矩形)。当我使用repaint()方法时,代表汽车的矩形在新位置重新粉刷,但后面留下了痕迹。

我有这个代码

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class MAP extends JFrame
{
    //constant for the screen size and used for the drawing the terrain
    final int WIDTH = 900, HEIGHT = 650;
    //the URL and Img designed for the images
    URL cLeft,cRight,cUp;
    Image img1,img2,img3;

//these will keep track of each player’s speed:
double p1Speed =.5;



//these are ints that represent directions:
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
//these will keep track of the player’s directions (default = up)
int p1Direction = 0;
JPanel panel;

//draw the terrain
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH, HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*9,WIDTH,HEIGHT/9);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5),(int)((WIDTH/9)*5),(HEIGHT/9)*4);

//these obstacles will obstruct the path and make navigating harder
Rectangle obstacle = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);
Rectangle obstacle2 = new
Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle3 = new
Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle4 = new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
Rectangle obstacle5 = new Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),WIDTH/30,HEIGHT/4);
Rectangle finish = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
Rectangle lineO=new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
Rectangle lineI = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2, HEIGHT/140);


//this is the rectangle for player 1’s (outer) car:
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);
public MAP(){
    super("Radical Racing");
    setSize(WIDTH,HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setResizable(false);
    setVisible(true);
    setLocationRelativeTo(null);


    //the following code creates the JFrame

    try
    {
        cUp = this.getClass().getResource("carUp.jpg");
        cLeft = this.getClass().getResource("carLeft.jpg");
        cRight = this.getClass().getResource("carRight.jpg");
    }catch(Exception e)
    {}

    //attach the URLs to the images
    img1 = Toolkit.getDefaultToolkit().getImage(cUp);
    img2 = Toolkit.getDefaultToolkit().getImage(cLeft);
    img3 = Toolkit.getDefaultToolkit().getImage(cRight);

    panel=new JPanel(){
        public void paint(Graphics g)
        {
            //super.paint(g);
            //draw the background for the racetrack
            g.setColor(Color.DARK_GRAY);
            g.fillRect(0,0,WIDTH,HEIGHT);

            //when we draw, the border will be green
            g.setColor(Color.GREEN);

            //fill rectangle
            g.fillRect(left.x,left.y,left.width,left.height);
            g.fillRect(right.x,right.y,right.width,right.height);
            g.fillRect(top.x,top.y,top.width,top.height);
            g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
            g.fillRect(center.x,center.y,center.width,center.height);

g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
                                g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
            g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
            g.fillRect(obstacle4.x,obstacle4.y,obstacle3.width,obstacle4.height);
            g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);

            //set the starting line color to white
            g.setColor(Color.WHITE);

            //now draw the starting line
            g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
            g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);

            //set the color of the finish line to yellow
            g.setColor(Color.YELLOW);

            //now draw the finish line
            g.fillRect(finish.x,finish.y,finish.width,finish.height);

            //set the color to blue for p1
            g.setColor(Color.WHITE);

            //now draw the actual player
            g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);

            //draw the images for the player
            if(p1Direction==UP)
                g.drawImage(img1,p1.x,p1.y,this);
            if(p1Direction==LEFT)
                g.drawImage(img2,p1.x,p1.y,this);
            if(p1Direction==RIGHT)
                g.drawImage(img3,p1.x,p1.y,this);
        }
    };


    panel.setPreferredSize(new Dimension(950,600));
    this.add(panel);
    pack();
    Move1 move=new Move1();
    move.start();

}

private class Move1 extends Thread implements KeyListener
{
    public void run()
    {
        //now, this should all be in an infinite loop, so the process
        //repeats
        addKeyListener(this);
        while(true)
        {
            //now, put the code in a try block. This will let the
            //program exit
            //if there is an error.
            try
            {
                //increase speed a bit
                //  if(p1Speed<=5)
                //  p1Speed+=.2;
                //p1.y-=(int) p1Speed;
                if(p1.intersects(left) || p1.intersects(right) || p1.intersects(top) ||
                        p1.intersects(bottom) || p1.intersects(obstacle) || p1.intersects(obstacle2)||
                        p1.intersects(obstacle3) || p1.intersects(obstacle4) || p1.intersects(obstacle5))
                {
                    p1Speed = -5;
                }
                //if the car hits the center, do the same as above
                //but make the speed -2.5.
                if(p1.intersects(center))
                {
                    p1Speed = -2.5;
                }
                //increase speed a bit
                if(p1Speed<=5)
                    p1Speed+=.2;
                //these will move the player based on direction
                if(p1Direction==UP)
                {
                    p1.y-=(int)p1Speed;
                }
                if(p1Direction==DOWN)
                {
                    p1.y+=(int)p1Speed;
                }
                if(p1Direction==LEFT)
                {
                    p1.x-=(int)p1Speed;
                }
                if(p1Direction==RIGHT)
                {
                    p1.x+=(int)p1Speed;
                }
                panel.repaint();
                //this delays the refresh rate:
                Thread.sleep(200);
            }
            catch(Exception e)
            {
                //if there is an exception (an error), exit the loop.
                break;
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent event) {
        if(event.getKeyChar()=='a')
        {
            p1Direction = LEFT;

        }
        if(event.getKeyChar()=='s')
        {
            p1Direction = DOWN;
        }
        if(event.getKeyChar()=='d')
        {
            p1Direction = RIGHT;
        }
        if(event.getKeyChar()=='w')
        {
            p1Direction = UP;
        }
    }

}
//this starts the program by calling the constructor:
public static void main (String[ ] args)
{
    new MAP();
}
}

问题答案:

在绘制方法中取消注释super.paint(g)[第87行]。

它负责清除画布上的任何陈旧对象。



 类似资料:
  • 问题内容: 我正在使用Java Swing编写游戏。我想在每次循环执行时绘制一下,并在之间稍加延迟以在屏幕上创建级联效果。我相信系统中的效率例程会将调用折叠为一个调用。无论如何,所有更改都在总延迟后立即发生。是否有某种方法可以强制系统立即重新绘制,然后在循环的每次迭代中延迟? 我的代码: 问题答案: 您可以用来强制立即重绘。 编辑:再次阅读您的问题后,对我来说,您可能正在事件分发线程上执行逻辑。这

  • 问题内容: 对不起,我进行了大量搜索,以查找这3个功能(绘画,重绘,paintComponent)之间如何相互作用,但我不知道。您能准确解释一下它们何时被调用(因为有时java会在没有我问他的情况下调用它),它们到底在做什么,它们之间有什么区别。谢谢 问题答案: 我不确定“ paint”,但是我可以解释repaint()和paintComponent()之间的关系。 根据我在Java方面的有限经验

  • 问题内容: 我正在研究模仿Paint的程序。问题是当我绘制新形状时,以前的形状会被删除。我试图注释掉我的paintComponents的超级调用,该调用可以工作,但是留下了太多的绘图。 问题答案: 正如您所发现的,您需要调用,否则背景不会被绘制,并且一切都是一团糟。问题在于,由于该字段只能是单个值,因此一次只能绘制一个形状。一种解决方案是创建一个形状,然后在in中绘制每个形状。

  • 问题是:计算所有根到叶数的总和。例如:如果树是(1,2,3),1是根,2是左子,3是右子,两条路径:1- 这是我正确的递归解决方案。在助手方法中,返回总和: 但是当我在helper方法中添加作为参数时,我总是得到0。 我相信我对递归一定有一些误解。提前感谢您给我一些解释,为什么的值没有“转移”回方法中的。

  • 这是一个相当简单的问题,我注意到当我表示一棵树时,无论我用哪种方式(后排序,按顺序,前排序)树叶总是以相同的顺序出现,从左到右。 我只是想知道为什么,这是有原因的吗? 我刚开始研究它们,就想到了这个。 编辑: 我有一棵这样的树: 叶节点为:D、E和F 预购顺序为:A、B、D、C、E、F 顺序是:D,B,A,E,C,F 后序是:D,B,E,F,C,A 叶子节点总是从左到右出现,不管我选择哪个顺序,问

  • (已经在Github上打开了一个案例#2250,但这里可能有人有解决方案?)他讲故事。我们有一个数据集,它只是一个具有 级别为0的单个根节点(有时它的ID(root)=0,因为我们从空数据库开始) 我们正在使用LOAD CSV加载数据,其中每一行创建一个节点和与前一级节点的关系。Neo4j是一个2.1.0-M01 Enterprise for Startups,集群有3个节点,每个实例有8Gb内存