简体   繁体   中英

Java applet in JFrame graphics glitch?

So I am trying to make a pinball game made as a applet load into a JFrame. The applet loads into it no problem but when i run it flickers and glitch out. Thanks in advance!

Jframe class:

import javax.swing.*;
import java.awt.*;
import java.applet.*;
public class PinBallGUI extends JFrame 
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Pinball");
        frame.setVisible(true);
        frame.setSize(600,700);
        int height = 700;
        int width = 600;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PinBallGame temp = new PinBallGame();
        frame.add(temp);
        temp.init(width,height);
        temp.start();
        temp.run();
    }
}

Applet class:

import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class PinBallGame extends Applet implements Runnable
{    
    Thread runner; 
    private Image Buffer;
    private Graphics gBuffer;
    int width,height;
    PinBallBall B1;
    PinBallPaddle P1,P2;
    PinBallRectangle R1,R2,R3,R4;
    PinBallRectangle[] rArray;
    boolean leftKey;
    boolean rightKey;
    boolean downKey;
    boolean spaceKey;
    int points;
    Random gen = new Random();
    public void init(int W,int H)
    {  
        width=W;
        height=H;
        Buffer=createImage(width,height);
        gBuffer=Buffer.getGraphics();
        B1 = new PinBallBall(-1,width-57,645);
        P1 = new PinBallPaddle(180,625,255,640);
        P2 = new PinBallPaddle(300,640,375,625);
        R1 = new PinBallRectangle(1,width-35,60,15,600);
        R2 = new PinBallRectangle(1,width-80,60,15,600);
        R3 = new PinBallRectangle(1,0,625,180,40);
        R4 = new PinBallRectangle(1,375,625,140,40);
        rArray = new PinBallRectangle[5];
        for(int i=0; i<rArray.length; i++)
        {
             rArray[i] = new       PinBallRectangle(-1,gen.nextInt(450),gen.nextInt(450),gen.nextInt(60),gen.nextInt(60));
        }
        int count = 0;
        addKeyListener(new MyKeyListener());
        points = 0;
    }
    private class MyKeyListener extends KeyAdapter
    {
       public void keyPressed(KeyEvent e)
       {
           switch(e.getKeyCode())
           {
               case KeyEvent.VK_LEFT:
               leftKey = true;
               break;
               case KeyEvent.VK_RIGHT:
               rightKey = true;
               break;
               case KeyEvent.VK_SPACE:
               spaceKey = true;
               break;
               case KeyEvent.VK_DOWN:
               downKey = true;
               break;
           }
       }
       public void keyReleased(KeyEvent e)
       {
           switch(e.getKeyCode())
           {
               case KeyEvent.VK_LEFT:
               leftKey = false;
               break;
               case KeyEvent.VK_RIGHT:
               rightKey = false;
               break;
               case KeyEvent.VK_SPACE:
               spaceKey = false;
               break;
               case KeyEvent.VK_DOWN:
               downKey = false;
               break;
            }
        }
    }   
    public void start()
    { 
        if (runner == null)
        {   
            runner = new Thread (this);
            runner.start();
        }
    }
    public void stop()
    {  
        runner = null;
    }
    public void run() 
    {  
        while(true)
        {
            update(gBuffer);
            if(leftKey){P1.moveLeftPaddle();}
            if(rightKey){P2.moveRightPaddle();}
            if(downKey){B1.launchBall(width,height);}
            repaint();
            try {runner.sleep(25);}
            catch (Exception e) { }   
            repaint();
            gBuffer.setColor(Color.black);
            gBuffer.fillRect(0,0,width,height);
            if((B1.getX() != width-57)||(B1.getY() != 645))
            {
                B1.movePinBallBall(width,height);
                B1.update();
            }
            R1.ballCollision(B1);
            repaint();
            R2.ballCollision(B1);
            repaint();
            R3.ballCollision(B1);
            repaint();
            R4.ballCollision(B1);
            repaint();
            P1.ballCollision(B1);
            repaint();
            P2.ballCollision(B1);
            repaint();
            for(int i=0; i<rArray.length; i++)
            {
                rArray[i].ballCollision(B1);
                repaint();
            }
            for(int i=0; i<rArray.length; i++)
            {
                rArray[i].paint(gBuffer);
            }
            update(gBuffer);
            B1.paint(gBuffer);
            R1.paint(gBuffer);
            R2.paint(gBuffer);
            R3.paint(gBuffer);
            R4.paint(gBuffer);
            P1.paint(gBuffer);
            P2.paint(gBuffer);
            repaint();
            update(gBuffer);
        }//while(true)
    } //run    
    public void update(Graphics g)
    {  
        paint(g);
    } 
    public void paint(Graphics g)
    {
        g.drawImage(Buffer,0,0, this);
    }
}

I also have 3 other classes that are just for objects (ball, rectangle, paddle). Thanks again in advance!

You're going to want to either use JFrame and JPanel or Frame and Canvas instead of using a JFrame and an Applet. You can definitely mix and match components, but you'll probably encounter less bugs if you don't.

I generally find flickering to happen when you aren't using double buffering. Fro a JPanel you'll want to enable double buffering and for a Canvas there is a way to implement triple buffering if you really need to, but stick with a JPanel for most cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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