如何在用户赢得游戏时弹出新屏幕?

时间:2014-11-08 23:07:53

标签: java swing

我的比赛有3个目标和一个球。用户将输入移动球的程度,以便击中其中一个目标。我所希望的是弹出一个新屏幕,说“你赢了!”如果用户击中其中一个目标。

这是我的屏幕类,它将被绘制(其他3个类未显示):

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;


public class Screen extends JPanel 
{
    private BufferedImage buffered;
    private Ball b1;
    private Target t1;
    private Target t2;
    private Target t3;

    public Screen()
    {
            b1 = new Ball(130,340);
        t1 = new Target(300,120);
            t2 = new Target(600,300);
        t3 = new Target(400,400);
    }

    public Dimension getPreferredSize() 
    {
        //Sets the size of the panel
        return new Dimension(800,600);
    }

    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        Color rough = new Color(0,130,33);
        Color green = new Color(107,204,4);

        //Create Buffered
        if( buffered == null )
        {
            buffered = (BufferedImage)(createImage( getWidth(), getHeight() ) );
        }

        //Create a temporary graphics buffered to draw
        Graphics gBuff = buffered.createGraphics();

        //Clear everything
        //This draws the white box
        gBuff.setColor(rough);
        gBuff.fillRect(0, 0, 800, 600);

        gBuff.setColor(green);
        gBuff.fillOval(20,20,650,560);
        gBuff.fillOval(240,50,530,540);

        b1.drawMe(gBuff);
        t1.drawMe(gBuff);
        t2.drawMe(gBuff);
        t3.drawMe(gBuff);

        //draw the buffered image
        g.drawImage(buffered, 0, 0, null); 
    }

    public void setVariables(int degreeInput)   
    {
        b1.setDegrees(degreeInput);
    }

    public void animate()
    {
        int count = 0;
        while(count < 100000)
        {
            //wait for .01 second
            try {
                Thread.sleep(10);
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }

            b1.move();

            if( t1.checkCollision( b1.getX(), b1.getY(), b1.getWidth(), b1.getHeight() ) )
            {
                break;
            }
            if( t2.checkCollision( b1.getX(), b1.getY(), b1.getWidth(), b1.getHeight() ) )
            {
                break;
            }
            if( t3.checkCollision( b1.getX(), b1.getY(), b1.getWidth(), b1.getHeight() ) )
            {
                break;
            }
            count++;
            repaint();
        }
    }
}

由于我是初学者,如果答案可以用简单的方式表达,那就太棒了!

1 个答案:

答案 0 :(得分:1)

你是说你想要某种类型的弹出窗口?类似的东西:

JOptionPane.showMessageDialog(frame, "You Win!", "Winner winner chicken dinner", JOptionPane.PLAIN_MESSAGE);