How to add start button to Simon Says Game in Java?

I want to draw an inscribed circle. A circle that is empty with no fill but making it have a full stroke. My code :

/* * 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(); } } } }

Here is what I am using to create the circle, for some reason it does not create the circle:

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

-------------Problems Reply------------

First of all, you're never (in the code provided) adding the panelBgImg to any component, so nothing is going to show it (you have to add it to a frame or another panel somewhere).

You should also change your paintComponent method though...

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);
}
};

Category:java Views:0 Time:2018-02-13
Tags: java geometry

Related post

  • how to add a button in Android Notifications 2011-06-02

    I am trying to add a button in each notification... and user can click the button to delete individual notification, I saw many people say just refer to "Creating a Custom Expanded View" and using RemoteViews,but is it possible to modify the official

  • How to add a button in android? 2010-04-21

    Can anybody tell how to add a button in android? --------------Solutions------------- Check this Android Button tutorial; this simple example creates a Close Button. All you need to do is: 1.Add Button widget to your Layout <Button android:id="@+i

  • How to add "pause" button in this jquery slider? 2010-10-29

    How to add pause button in this jQuery image slider? I want to add Pause on click button at last after 4 ? Like this: see example pic http://www.sohtanaka.com/web-design/examples/image-slider/ I want add another link button to pause the slider on cur

  • how to add 2 buttons horizontally with 50% width for each in android? 2010-11-19

    how to add 2 buttons horizontally with 50% width for each ? --------------Solutions------------- Put them in a LinearLayout Give them each android:layout_width="0px" Give them each android:layout_weight="1" Add both the buttons in linearlayout and gi

  • How to add twitter button on my website? 2011-01-31

    how to add twitter button on my website? --------------Solutions------------- Please state more clear questions. What kind of button do you want?! Check out the official Twitter developer sites that might offer what you want: http://dev.twitter.com/

  • how to add a button in last cell in Tableview 2011-02-07

    if any one knows how to add a button in last cell in table view then let me know thanks in advance.... --------------Solutions------------- Just like any cell but have it do what you intended your button to do in cellforrowatindexpath . you can also

  • how to add action buttons to jqgrid like shown in demo? 2011-06-08

    how to add icon buttons to jqgrid rows like shown in this demo? demo Thanks --------------Solutions------------- Sorry, but I don't understand your question. The demo is from my old answer. You can just open in web browser the source code and see all

  • How to add new button to order view in Magento admin panel? 2011-07-10

    How to add custom button to order view page near "Back" and "Edit"? --------------Solutions------------- config.xml: <global> <blocks> <adminhtml> <rewrite> <sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_

  • How to add multiple buttons to a row dynamically in Android 2011-07-29

    Does anybody know how to add multiple buttons to a table row dynamically in Android? Cheers --------------Solutions------------- You can try this and see if it's what your are looking for. main.xml: <?xml version="1.0" encoding="utf-8"?> <Li

  • How to add a button in a listcontrol mfc? 2011-08-09

    How to add a button in a listcontrol mfc ,i have created a mfc listcontrol and would like to have a button in a cell when it is clicked ? --------------Solutions------------- Check these out http://www.codeproject.com/KB/list/MFCCListCtrlDerClass.asp

  • How to add a Button (like 'Clear History') below the expanded SearchView (showing 10 recent searches) in Android? 2011-08-30

    Can anyone please help in how to add a button to the expanded SearchView (placed in ActionBar) below the list of recent search suggestions the SearchView is showing? Since SearchView extends LinearLayout, I have tried to add a button in the layout, b

  • How to add a Button as a child of a DrawingVisual 2011-11-20

    I have a DerivedCanvas class which inherits from Canvas. I use it to draw several custom drawing visuals, but can't figure out how to add a Button object as a child of a DrawingVisual object. Below is the code I am using within the DrawingVisual, but

  • How to add the button in uitableviewcell in ipad 2012-01-25

    I am new in ipad development i want to add the button in uitableviewcell but it is not displaying. how to add this button in tableview cell. I am writing this code in cellforRowAtIndexPath My code is: UIButton *cellImgButton = [[UIButton alloc]initWi

  • How to add custom button and new features in TinyMCE in wordpress3.3 2012-02-10

    Note my wordpress version is 3.3.1 some hooks and function is not the same anymore, and the info on wordpress.org is not actually helping. all I want to do is adding and customize the default behavior about, so any post to provide? --------------Solu

  • I did a Clean Install on Windows 7 onto my PC and there is no games in the Games Explorer, Also when I click on the start button, there is no games button there at all under where where my computer and my documents are at. 2014-03-18

    I did a Clean Install on Windows 7 onto my PC and there is no games in the Games Explorer, Also when I click on the start button, there is no games button there at all under where where my computer and my documents are at. --------------Solutions----

  • How do i start telnet for hosting terminal games on a mac os x 10.6 Snow leopard 2011-12-11

    How do i start telnet for hosting terminal games on a mac os x 10.6 Snow leopard. My aim is to host terminal applications like games (terminal based) on specific port of my system connected through a static IP.These games are ruby based. ------------

  • Wordpress - How to add a button into a Wordpress' post and make it work? 2011-05-07

    I've just started writing plugins on Wordpress and I'm struggling with adding a small button on every post that helps translate the whole post into another language. I tried to get the post's content, then add a button into it and return. But this ce

  • How to add share button in android? 2011-04-06

    Possible Duplicate: Android facebook/twitter share button I am creating an app wherein i want to add share button , on clicking the button my app can be shared on facebook and twitter. Please let me know if anyone has a suitable answer which i can im

  • Android:How to add a button in surface view 2011-04-25

    I'm drawing some graphics and i would like to add a couple of buttons to it. But with the surface view how do we add these buttons programatically ? --------------Solutions------------- Enclose your surfaceView with a FrameLayout in your xml Layout.

  • How to add a button to the last row of a tableview? 2011-08-23

    I'm unable to find the solution how can I add this "Load More" button in a UITableViewCell. Like "More Results..." option in this app http://itunes.apple.com/in/app/zdnet/id425580940?mt=8 Hope someone can help me... --------------Solutions-----------

  • How to add a button to the main toolbar in Eclipse programmatically 2012-01-31

    I've a question. I cannot find the way, how to add buttons to main toolbar programmatically. My problem is, that I've the task to dynamically (based on XML configuration file) build menus and toolbar. I found how to add a menu item programmatically,

  • How to add Done button to the keyboard? 2012-04-09

    UPDATE: I also tried implementing UITextViewDelegate delegate and then doing in my controller: - (BOOL)textViewShouldEndEditing:(UITextView *)textView { [textView resignFirstResponder]; return YES; } I also set the delegate of the text view to be sel

  • how to add a button without using custom cell on a UITableView? 2009-05-12

    How do I add a custom button on a UITableViewCell, and then delete the cell with that button without using Interface Builder and Custom Cell? --------------Solutions------------- If you really want to add a custom button WITHOUT subclassing, just add

  • UIToolbar-how to add two button item in two line? 2009-06-18

    as title how to?the class method of UIToolbar is add the button all in single line [contentToolBar setItems:items animated:NO]; but i want it can add in different line or multiple line, thanks --------------Solutions------------- That's not exactly t

Copyright (C) dskims.com, All Rights Reserved.

processed in 2.343 (s). 12 q(s)