如何添加开始按钮Simon说Java游戏?

时间:2015-04-20 06:26:36

标签: java geometry

我想绘制一个内切圆。一个空的圆圈,没有填充但是有一个完整的笔划。我的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simon;
import java.awt.*;
import java.awt.event.*; //This allows us to detect user input
import java.awt.image.BufferedImage;
import javax.swing.*; //This allows us to use graphical elements, colors, etc.

/**
 *
 * @author User
 */
public class SimonA {
    //First, we create all the elements of our program
    //Here are the variables...
    public static int score = 0;
    public static int color = 0; //0 = yellow, 1 = red, 2 = blue, 3 = green
    public static Boolean flash = false; //This is used to make the panel blink
    public static Boolean running = false;
    //Here are the widgets (objects)....
    public static JFrame frame = new JFrame();
    public static JPanel buttons = new JPanel();
    public static JPanel buttons1 = new JPanel();//These buttons will all be square (the default). Different packages can be used to change the shape
    public static JPanel controls = new JPanel();
    public static JButton red = new JButton();
    public static JButton yellow = new JButton();
    public static JButton green = new JButton();
    public static JButton blue = new JButton();
    public static JButton toggle = new JButton("Start"); //Click this button to see a sample flash
    public static JLabel scoreTxt = new JLabel("Score: " + score, SwingConstants.CENTER); //This object (a label element) displays the score variable's value
    public static Timer blink = new Timer(600,new Ticker()); //This is used to time the duration of the flash

    public static BufferedImage img = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
    public static Graphics2D g = img.createGraphics();

    public static JLabel space = new JLabel();


    JPanel panelBgImg = new JPanel() {
        public void paintComponent(Graphics g){
                g.setStroke(new BasicStroke(8));
                g.fillOval(0, 0, 400, 400);
                 }
        };
    /*Timers are important for any program in which something "moves" at set durations.  In this case, every tenth of
      a second, the timer will generate an event.  In this case, we are using it to determine that the active tile
      will flash for 600ms, or 6/10 of a second. Obvously, then, 1000 makes the timer generate an event once-per-second. */

    public static void main (String[] args)
    {
        frame.setBackground(Color.gray);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //This means that when we close the window with [x] the program ends
        frame.setSize(new Dimension(400,400));
        frame.setForeground(Color.black);
        frame.setTitle("Simon");
        frame.setLayout(new BorderLayout()); //Remember this from the LayoutManager test?


        buttons.setLayout(new GridLayout(1,2));
        buttons1.setLayout(new GridLayout(1,2));//The buttons are placed in this panel, which is set as a 2x2 grid
        yellow.setBackground(Color.yellow);
        yellow.addActionListener(new YellowPressed()); //This triggers the "procedure" that runs when the yellow button is pressed
        yellow.setPreferredSize(new Dimension(200,200));
        //Note, the other buttons will take their cue for size from the above statement, since they are all in the same grid
        //We do not need to specify dimensions again
        red.setBackground(Color.red);
        red.addActionListener(new RedPressed());
        red.setPreferredSize(new Dimension(200,200));
        blue.setBackground(Color.blue);
        blue.addActionListener(new BluePressed());
        green.setBackground(Color.green);
        green.addActionListener(new GreenPressed());
        green.setPreferredSize(new Dimension(200,200));

        //Adding the four buttons to the panel called "buttons"
        buttons1.add(yellow);
        buttons.add(red);
        buttons.add(green);
        buttons1.add(blue);



        //The control panel on the bottom is a gride of one row and two columns
        controls.setLayout(new GridLayout(2,1));
        controls.add(scoreTxt);
        controls.add(toggle);
        toggle.addActionListener(new ToggleOn());

        //We now add the panels to the frame according to the border layout
        frame.add(buttons,BorderLayout.NORTH);
        frame.add(controls,BorderLayout.CENTER);
        frame.add(buttons1,BorderLayout.SOUTH);
        //This .pack() method removes any excess whitespace around your elements
        //Sometimes it results in a better look, and sometimes not.
        frame.pack();
        frame.setVisible(true);

    }




    public static class ToggleOn implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            //This toggles the main button between Start and Stop
            //If it's running, stop it from running
            //If it's not running, start it running
            running = !running;
                if (running)
                {
                    toggle.setText("Stop");
                    //To demonstrate how the code might work, a sample flash
                    color = 3; //Change the color to "active"
                    blink.start(); //Starts the 6/10 second timer
                    score += 10;
                    scoreTxt.setText("Score: " + score);
                }
                else
                {
                    toggle.setText("Start");
                }
        }
    }





    public static class YellowPressed implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            //Right now, the buttons just print to the screen
            //The "real" program would implement other instructions here
            System.out.println("Yellow");
        }
    }

    public static class RedPressed implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            System.out.println("Red");
        }
    }

    public static class BluePressed implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            System.out.println("Blue");
        }
    }

    public static class GreenPressed implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            System.out.println("Green");
        }
    }

    public static class Ticker implements ActionListener {
        public void actionPerformed(ActionEvent event)
        {
            //When the timer triggers, if a button is active,
            //set it back to its original color 6/10 of a second later
            flash = !flash;
            if (flash) //If the button is to be lit, turn it white
            {
                if (color == 0)
                    yellow.setBackground(Color.white);
                else if (color == 1)
                    red.setBackground(Color.white);
                else if (color == 2)
                    blue.setBackground(Color.white);
                else if (color == 3)
                    green.setBackground(Color.white);
            }
            else //Otherwise, change it back to its original color
            {
                if (color == 0)
                    yellow.setBackground(Color.yellow);
                else if (color == 1)
                    red.setBackground(Color.red);
                else if (color == 2)
                    blue.setBackground(Color.blue);
                else if (color == 3)
                    green.setBackground(Color.green);
                blink.stop();
            }
        }
    }
}

以下是我用于创建圆圈的内容,由于某种原因,它不会创建圆圈:

JPanel panelBgImg = new JPanel() {
            public void paintComponent(Graphics g){
                    g.setStroke(new BasicStroke(8));
                    g.fillOval(0, 0, 400, 400);
                     }
            };

1 个答案:

答案 0 :(得分:0)

首先,你永远不会(在提供的代码中)将panelBgImg添加到任何组件,因此不会显示任何内容(您必须将其添加到某个框架或其他面板)。

您还应该更改paintComponent方法......

JPanel panelBgImg = new JPanel() {
     public void paintComponent(Graphics g){
         //Call super method to draw background
         super.paintComponent(g);

         //Cast to Graphics2D to use setStroke
         Graphics2D g2d = (Graphics2D) g;

         //Set the color of the circle you want to draw
         g2d.setColor(Color.RED);
         g2d.setStroke(new BasicStroke(8));
         //g2d.fillOval(0, 0, 400, 400);
         //Use drawOval instead of fillOval (otherwise it will be filled with the color)
         g2d.drawOval(0, 0, 400, 400);
     }
};