Java图形保持闪烁

时间:2016-12-04 17:11:45

标签: java graphics

我正在尝试为我的学校项目制作一个简单的马里奥游戏,到目前为止它还顺利,但出于某种原因,当我的蟾蜍gif移动时以及当我移动我的马里奥角色时,图形会一直闪烁。我怎么能阻止这个?

    import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;

//Questions:

//1. How do I get gravity?
//2. How do I get it to hit the higher block and then fall down?
//3. Sun as circle
//4. How do I get the background to move with the character
//5. Get Mario to move (as an animation)
//6. Think flashing entrance screen for name with polygon
//7. Figure out how to fit in an arc somewhere (Bird)

public class Mario extends Applet implements KeyListener,ActionListener 
{
    private Image lvlOne; //Level 1 Background
    private Image marioLeft;
    private Image marioRight;
    private Image toad;
    private int level;
    private int x;
    private int y;
    private int marioIncrements;
    private int marioLocation;
    private int upDown;
    private int toadLocation;
    private boolean rightLeft;
    private double scroll;
    private Timer timer;

    public void init()
    {
        level = -1; //Name and Assignment
        lvlOne = getImage(getDocumentBase(), "background.png"); //Level 1
        marioLeft = getImage(getDocumentBase(), "marioLeft.png"); //Mario Looking Left
        marioRight = getImage(getDocumentBase(), "marioRight.png"); //Mario Looking Right
        toad = getImage(getDocumentBase(), "toads.gif");
        addKeyListener(this); //Starts Key Listener
        marioIncrements  = 10;
        marioLocation = 100;
        upDown = 700;
        toadLocation = 2400;
        rightLeft = true;
        timer = new Timer(10,this); // timer sends a signal to our applet every 10 milliseconds
        timer.start();
        scroll = 0.5;
    }

    public void actionPerformed (ActionEvent ae){ // timer signals come here

    }



    public void keyReleased(KeyEvent ke){
    }

    public void keyPressed(KeyEvent ke){
        int zzz = ke.getKeyCode();
        switch(zzz){
            case(KeyEvent.VK_ENTER):
            level+= 1; //Enter Key Starts Game at Level 1
            break;

            case(KeyEvent.VK_RIGHT): //Move Right
            marioLocation = marioLocation + marioIncrements;
            break;

            case(KeyEvent.VK_LEFT): //Move Left
            marioLocation = marioLocation - marioIncrements;
            //rightLeft ^= true; //THIS IS TEMPORARY
            break;

            case(KeyEvent.VK_SPACE):
            upDown = upDown - 260; //Jump
            break;

            case(KeyEvent.VK_UP):
            upDown = upDown - 260; //Another Option to Jump
            break;

            case(KeyEvent.VK_DOWN):
            upDown = upDown  + 260; //Temp go Down I NEED GRAVITY
            break;

        }
        repaint();
    }

    public void keyTyped(KeyEvent ke){
    }

    public void paint(Graphics g)
    {
        //Determines Background to set based on level
        if(level == -1){
            setBackground(Color.BLACK);
            g.setColor(Color.RED);
            g.setFont(new Font("Comic Sans MS",Font.PLAIN,300));
            g.drawString("A PROJECT BY", 200,500); //Displays Project
            g.setColor(Color.WHITE);
            g.setFont(new Font("Comic Sans MS",Font.PLAIN,300));
            g.drawString("SUNNY GANDHI", 25,900);//Displays Name
            g.setFont(new Font("Comic Sans MS",Font.PLAIN,70));
            g.setColor(Color.GREEN);
            g.drawString("Press Enter to Skip All This Boring Stuff", 25,100);

        }
        else if(level ==0){
            g.drawImage(lvlOne, 0, 0, getWidth(), getHeight(),this);

            //Figure out HOW TO DRAW A SUN AS A CIRCLE
            //g.setColor(Color.YELLOW);
            //g.drawOval(10, 10, 5, 5);

            //Draw Rectangle Here
            g.setColor(Color.RED);
            g.setFont(new Font("Comic Sans MS",Font.PLAIN,150));
            g.drawString("MARIO BROS", 1000,500); //Displays Game Title
            g.setColor(Color.GREEN);
            g.setFont(new Font("Comic Sans MS",Font.PLAIN,70));
            g.drawString("Press Enter to Begin", 1050,600);//Displays Game Title

        }
        else if(level == 1){ //Level 1
            g.drawImage(lvlOne, 0, 0, getWidth(), getHeight(),this);
            if(rightLeft == true){ //Change Mario Direction/Location
                g.drawImage(marioRight, marioLocation, upDown, 100, 150,this); //Draws beginning Mario
            }
            else if(rightLeft == false){ //Change Mario Direction/Location
                g.drawImage(marioLeft, marioLocation, upDown, 100, 150,this);
            }
            //First Set of Blocks

            //First Block
            g.setColor(coloLibGandhi.BROWN());
            g.drawRect(700,590,100,100);
            g.fillRect(700,590,100,100);
            //Second Block
            g.setColor(Color.BLACK);
            g.drawRect(600,590,100,100);
            g.fillRect(600,590,100,100);
            //Third Block
            g.setColor(Color.GREEN);
            g.drawRect(500,590,100,100);
            g.fillRect(500,590,100,100);
            //Middle Higher Block
            g.setColor(Color.BLACK);
            g.drawRect(600,200,100,100);
            g.fillRect(600,200,100,100);

            //Second Set of Blocks
            //First Block
            g.setColor(coloLibGandhi.BROWN());
            g.drawRect(1500,590,100,100);
            g.fillRect(1500,590,100,100);
            //Second Block
            g.setColor(Color.BLACK);
            g.drawRect(1600,590,100,100);
            g.fillRect(1600,590,100,100);
            //Third Block
            g.setColor(Color.GREEN);
            g.drawRect(1700,590,100,100);
            g.fillRect(1700,590,100,100);

            //Toad
            g.drawImage(toad, toadLocation, 700, 100, 150, this);

        }
        else if(level == 2){
        }
        else if(level == 3){


         }

        }
    }

忽略我所做的一些评论,以及一些未使用的变量 - 我将在稍后介绍。

谢谢!

1 个答案:

答案 0 :(得分:0)

Image bck=createImage(getWidth(), getHeigh());
Graphics g2=bck.getGraphics();

在这个g2上做所有的绘画;那么

paint(graphics g) {
  g.draw(bck 0, 0, this);
}

public void update(Graphics g) {
  paint(g);
}