简体   繁体   English

带有绘画方法的Java Applet麻烦

[英]Java Applet Troubles w/ Paint Method

So far I have gotten this applet to a working stage; 到目前为止,我已经使这个applet进入了工作阶段。 The only problem is that it only works if the mouse is moving; 唯一的问题是它仅在鼠标移动时才起作用。 If the mouse is not moved, then the entire thing grinds to a halt. 如果不移动鼠标,则整个过程都会停止。 It starts working again if the mouse is moved. 如果移动鼠标,它将重新开始工作。

The way that it works is the paint method is called by mouseMoved method. 它的工作方式是paint方法,由mouseMoved方法调用。 Each time the the mouse is moved, it goes threw the paint method, and the game continues. 每次移动鼠标时,鼠标都会抛出绘画方法,然后游戏继续。 The problem arises from when the mouseMoved method isn't called, so the paint method does not get called, and the entire thing just pauses until the mouse is moved. 问题是由于未调用mouseMoved方法而导致的,因此未调用paint方法,并且整个过程只是暂停,直到移动鼠标为止。

I have tried the following: 我尝试了以下方法:

Having the paint method be recursive so it will call itself. 具有paint方法是递归的,因此它将自行调用。 The problem with this is that then the mouselistener does not work, because the program is to busy painting a bunch of stuff moving around. 问题在于鼠标侦听器不起作用,因为该程序正忙于绘制一堆移动的东西。

I also tried using threads so that the mouselistener would interrupt the paint method. 我还尝试使用线程,以便mouselistener会中断paint方法。 This did not work, although this could have been because I do not understand how threads worked. 尽管这可能是因为我不了解线程是如何工作的,但这没有用。 If anyone knows how to implement this, it seems like it would work. 如果有人知道如何实现这一点,那似乎就可以了。

Here is the snip-it of code of the problematic areas; 这是问题区域的代码片段;

  public void paint( Graphics gr) {
     if( Earth != null){

        displayWorld(Earth);
        for(int a =0; a < 100; a++){      
           Earth.run();
           Earth.Worlds.get(2).forceMove(x,y);
          }
        try
        {
           Thread.sleep(100);  
        }
           catch (InterruptedException ie)
           {}   
     }

  }

public void mouseMoved( MouseEvent e ) {
     x = e.getX();
     y = e.getY();
     Graphics gr = getGraphics();      
    paint(gr);
  }  

I'd suggest taking another look at threads. 我建议再看看线程。 This is a pretty standard problem in game programming (and in other areas of programming too) - you have one thread, looping forever, containing your game logic. 这是游戏编程(以及其他编程领域)中的一个非常标准的问题-您拥有一个永久循环的线程,其中包含您的游戏逻辑。 It updates your player's position, calculates, health, whatever, or it sleeps if there's no work to do. 它会更新您球员的位置,进行计算,保持健康,或者在没有工作要做的情况下进入睡眠状态。 If work is done which requires the applet's graphics to be redrawn then your thread calls repaint() which sends a repaint request to the event dispatching thread (a special thread that gets created automatically in every Java program). 如果完成了需要重绘applet图形的工作,则您的线程调用repaint() ,它将重绘请求发送到事件分配线程(在每个Java程序中都会自动创建的特殊线程)。

When the player moves their mouse or provides keyboard input, your event listeners update your program's data structures (and pokes the main thread in case it's sleeping). 当播放器移动鼠标或提供键盘输入时,事件侦听器将更新程序的数据结构(并在睡眠状态下戳入主线程)。

These links to previous StackOverflow questions involving Java games might be a good jumping-off point: 这些指向以前涉及Java游戏的StackOverflow问题的链接可能是一个很好的起点:

Best way to implement game loop without freezing UI thread 在不冻结UI线程的情况下实现游戏循环的最佳方法

game loop - threads 游戏循环-线程

good luck! 祝好运!

So, your paint process is reliant on the mouse moving?? 因此,您的绘制过程取决于鼠标的移动? So you need some kind of way to tell the applet to update itself?? 因此,您需要某种方式来告诉小程序自己更新?

private javax.swing.Timer paintTimer;

public void start() {
    // Your normal setup...

    paintTimer = new javax.swing.Timer(250, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Graphics gr = getGraphics();      
            paint(gr);
        }
    });
    paintTimer.setRepeats(true);
    paintTimer.setCoalesce(true);
    paintTimer.start();
}

public void mouseMoved( MouseEvent e ) {
    x = e.getX();
    y = e.getY();

    // You could wait for the timer to trigger the repaint for you...
    repaintTimer.restart();

    Graphics gr = getGraphics();      
    paint(gr);
}  

You're going to need to play around with the timing though 不过,您将需要把握时机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM