如何让java动作执行得更顺畅?

时间:2014-02-26 16:06:07

标签: java graphics timer refresh

我已经创建了一个简单的程序,可以根据按下按钮的次数填充一米,最终形成一个“Mash the button to win”风格的游戏。我已将它设置在每次按下空格键的位置,仪表填充10/440,如果达到最大值440,则获胜。我遇到的问题是如何使增量更平滑,更少突然;好像你正在抽自行车轮胎:连续,不那么增加。 这是有问题的代码:

import javax.swing.JOptionPane;
import javax.swing.*;               
import java.util.*;                             
import static java.lang.System.*;
import static java.lang.Math.*;
import java.awt.*;                              
import java.awt.event.*;                                
import java.io.*;   //for files                     
public class MyClicker
{
    public static void main(String args[])
    {
        new Clicker();  //Make a window
    }

}
class Clicker extends Frame   implements KeyListener, MouseListener
{
    // global variables
    private final static int SCHEIGHT=768,SCWIDTH=1024;
    // direction constants

    final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
    // movement change constants
    final  int X = 0,Y = 1,Z = 2;
    final int TITLE = 30, STATUS = 40;
    final static int size = 2;
    Image myPic;

    private boolean gameNotOver = true;
    private boolean keyPressed = false;
    private Image myScreen;
    private int whichScreen;
    private int timePassed;
    private int numScreen;
    private int maxAmt=450;
    private int timer;
    private int amt;

    public Clicker()
    {       
        setSize(SCWIDTH,SCHEIGHT);
        addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent e)
                    {
                      System.exit(0);
                    }
               });
       timer = -1;
       this.setVisible(true);
       out.println(amt);
       this.addKeyListener(this);
       gameLoop();
    }
public void gameLoop()
{   
    do   
     {
        if (keyPressed)
           {
             amt = amt + 50;
             out.println(keyPressed + " " +gameNotOver+" "+ "Time "+timer);
             this.repaint();
             keyPressed = false;
           }
        if(amt >= 400)
            gameNotOver = false;
        pause(1000);
        timer++;
        this.setVisible(true);
        //this.repaint();
        pause(0);
        if(amt > maxAmt - 10)   
            amt = maxAmt - 10;
     }
   while (gameNotOver);

   if(!gameNotOver)
    {
        pause(5000);
        System.exit(0);
    }

}
public int getAmt()
{
    return amt;
}
public void paint(Graphics pen)
 {    
        if(whichScreen==0)
            {
                pen.drawImage(myPic,100,200,506,279,this);   //Draws the image
                pen.setFont(new Font("Timesroman", Font.ITALIC, 50));
                pen.drawString("Welcome to the Clicker Test", 200, 75);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 40));
                pen.drawString("Created by Cody Coulter",150,150);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 25));
                pause(200); // 2000 final           
                whichScreen++;
                doubleBuffer(pen);
            }
        else
            {
                myScreen =createImage(getSize().width,getSize().height);
                Graphics o = myScreen.getGraphics();
                doubleBuffer(o);
                pen.drawImage(myScreen,0,0,this);
            }
 }   
public void doubleBuffer(Graphics pen)  // Draws the window
 {     
          pause (500);
          numScreen++;
          if (numScreen > 0)          
               {      
                    setBounds(0,0,SCWIDTH,SCHEIGHT);
                    Color HPRed = new Color(213, 0, 0);
                    pen.setColor(Color.BLACK);
                    setTitle("Clicker Test  -- Cody Coulter."+
                             "To click, press the space bar");
                    pen.setFont(new Font("Timesroman",Font.PLAIN,48));
                    pen.drawString("Clicker Test ",350,75);
                    pen.setFont(new Font("TimesRoman",Font.BOLD,33));
                    pen.fillRect((SCWIDTH/4), (SCHEIGHT/3), 50, maxAmt);
                    pen.drawString(""+timer, 350, 150);

                    pen.setColor(HPRed);
                    pen.fillRect((SCWIDTH/4)+5, (SCHEIGHT/3)+5, 40, amt);


                    if(!gameNotOver)
                     {
                        if(amt >= maxAmt-10)
                        {
                            pen.setColor(HPRed);
                            pen.fillRect(SCWIDTH/4-75, SCHEIGHT/3, SCWIDTH/2+150, SCHEIGHT/3-60);
                            pen.setColor(Color.GRAY);
                            pen.fillRect(SCWIDTH/4-65, SCHEIGHT/3+10, SCWIDTH/2+130, SCHEIGHT/3-80);
                            pen.setColor(Color.YELLOW);
                            pen.setFont(new Font("Verdana", Font.BOLD, 40));
                            pen.drawString("You got it!", SCWIDTH/2-290, SCHEIGHT/2-20);
                        }
                     }
                    this.repaint();
               }
}    
public void keyPressed(KeyEvent e)
{
    keyPressed = true;
    setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));  
    System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode())+ " " + keyPressed);
    switch(e.getKeyCode())
       {
          case KeyEvent.VK_SPACE: if(amt < maxAmt - 10)
                                      amt= amt + 10;
                                  break; 
          case KeyEvent.VK_A: if(amt < maxAmt - 10)
                                      amt = amt + 10;
                                  break;
       }
}
public void mouseClicked(MouseEvent m)
{
}   
public void mouseEntered(MouseEvent m)
{
}   
public void mouseExited(MouseEvent m)
{
}   
public void mousePressed(MouseEvent m)
{
}   
public void mouseReleased(MouseEvent m)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}

public void update(Graphics G)
{
      paint(G);
}       
public static void pause (long r)
{
    try
       {
           Thread.sleep(r);
       }
    catch (Exception e) 
       {
           out.println(" sleep error " + e);
        }          
}
}  //end class         

以下是完整的最新代码:

import javax.swing.JOptionPane;
import javax.swing.*;               
import java.util.*;                             
import static java.lang.System.*;
import static java.lang.Math.*;
import java.awt.*;                              
import java.awt.event.*;                                
import java.io.*;   //for files

public class MyClicker
{
    public static void main(String args[])
    {
        new Clicker();  //Make a window
    }

}
class Clicker extends Frame   implements KeyListener, MouseListener
{
    // global variables
    private final static int SCHEIGHT=768,SCWIDTH=1024;
    // direction constants

    final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
    // movement change constants
    final  int X = 0,Y = 1,Z = 2;
    final int TITLE = 30, STATUS = 40;
    final static int size = 2;
    Image myPic;

    private boolean gameNotOver = true;
    private boolean keyPressed = false;
    private Image myScreen;
    private int whichScreen;
    private int timePassed;
    private int numScreen;
    private int maxAmt=450;
    private int dispAmt;
    private int actAmt;
    private double timer;

    public Clicker()
    {       
        setSize(SCWIDTH,SCHEIGHT);
        addWindowListener(new WindowAdapter()
              {
                  public void windowClosing(WindowEvent e)
                    {
                      System.exit(0);
                    }
               });
       timer = -1;
       this.setVisible(true);
       this.addKeyListener(this);
       gameLoop();
    }
public void gameLoop()
{   
    do   
     {
        if (keyPressed)
           {
             out.println(keyPressed + " " +gameNotOver+" "+ "Time "+timer);
             keyPressed = false;
           }
        if(dispAmt > maxAmt-10)
            dispAmt = maxAmt;
        if(actAmt >= maxAmt-10 || dispAmt >= maxAmt-10)
            gameNotOver = false;
        if(dispAmt < actAmt)
            dispAmt++;
        timer++;
        this.setVisible(true);
//          this.repaint();
//          pause(0);
//          if(actAmt > maxAmt - 10 || dispAmt > maxAmt - 10)   
//              actAmt = maxAmt - 10;

        this.repaint();
     }
   while (gameNotOver);

   if(!gameNotOver)
    {
        pause(5000);
        System.exit(0);
    }

}
public int getAmt()
{
    return actAmt;
}
public void paint(Graphics pen)
 {    
        if(whichScreen==0)
            {
                pen.drawImage(myPic,100,200,506,279,this);   //Draws the image
                pen.setFont(new Font("Timesroman", Font.ITALIC, 50));
                pen.drawString("Welcome to the Clicker Test", 200, 75);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 40));
                pen.drawString("Created by Cody Coulter",150,150);
                pen.setFont(new Font("Timesroman", Font.ITALIC, 25));
                pause(200); // 2000 final           
                whichScreen++;
                doubleBuffer(pen);
            }
        else
            {
                myScreen =createImage(getSize().width,getSize().height);
                Graphics o = myScreen.getGraphics();
                doubleBuffer(o);
                pen.drawImage(myScreen,0,0,this);
            }
 }   
public void doubleBuffer(Graphics pen)  // Draws the window
 {     
          numScreen++;
          if (numScreen > 0)          
               {      
                    setBounds(0,0,SCWIDTH,SCHEIGHT);
                    Color HPRed = new Color(213, 0, 0);
                    pen.setColor(Color.BLACK);
                    setTitle("Clicker Test  -- Cody Coulter."+
                             "To click, press the space bar");
                    pen.setFont(new Font("Timesroman",Font.PLAIN,48));
                    pen.drawString("Clicker Test ",350,75);
                    pen.setFont(new Font("TimesRoman",Font.BOLD,33));
                    pen.fillRect((SCWIDTH/4), (SCHEIGHT/3), 50, maxAmt);
                    pen.drawString(""+timer/1000, 350, 150);
                    pen.drawString(""+dispAmt+"  /  "+actAmt, 350, 200);

                    pen.setColor(HPRed);
                    pen.fillRect((SCWIDTH/4)+5, (SCHEIGHT/3)+5, 40, dispAmt);


                    if(!gameNotOver)
                     {
                        if(actAmt >= maxAmt-10)
                        {
                            pen.setColor(HPRed);
                            pen.fillRect(SCWIDTH/4-75, SCHEIGHT/3, SCWIDTH/2+150, SCHEIGHT/3-60);
                            pen.setColor(Color.GRAY);
                            pen.fillRect(SCWIDTH/4-65, SCHEIGHT/3+10, SCWIDTH/2+130, SCHEIGHT/3-80);
                            pen.setColor(Color.YELLOW);
                            pen.setFont(new Font("Verdana", Font.BOLD, 40));
                            pen.drawString("You got it!", SCWIDTH/2-290, SCHEIGHT/2-20);
                        }
                     }
                    this.repaint();
               }
}    
public void keyPressed(KeyEvent e)
{
    keyPressed = true;
    setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));  
    System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode())+ " " + keyPressed);
    switch(e.getKeyCode())
       {
          case KeyEvent.VK_SPACE:actAmt = actAmt+10;
                                  break; 
          case KeyEvent.VK_A: actAmt = actAmt+10;
                                  break;
       }
    this.repaint();
}
public void mouseClicked(MouseEvent m)
{
}   
public void mouseEntered(MouseEvent m)
{
}   
public void mouseExited(MouseEvent m)
{
}   
public void mousePressed(MouseEvent m)
{
}   
public void mouseReleased(MouseEvent m)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}

public void update(Graphics G)
{
      paint(G);
}       
public static void pause (long r)
{
    try
       {
           Thread.sleep(r);
       }
    catch (Exception e) 
       {
           out.println(" sleep error " + e);
        }          
}
}  //end class

1 个答案:

答案 0 :(得分:0)

  • 摆脱暂停(1000)你可以让它暂停(10)或周围的东西。

  • 每次在循环中调用this.repaint()(在if语句之外)。

  • 将amt拆分为两个变量:实际金额和显示金额。仅绘制显示的数量,并且按键仅增加实际数量。

  • 每一帧,如果显示的数量小于实际数量,则增加1。

...更新

  • 从双缓冲程序中删除暂停(500),并从绘画中暂停(200)。一般情况下,除非您需要“无所事事”,否则不要暂停。游戏永远不会“无所事事”,它们几乎总是有一个至少渲染的核心循环。