Tic Tac Toe With GUI Using JButtons That Will Hold X And O Pictures
Jan 8, 2015
I am in the process of creating a Tic Tac Toe game with a GUI using JButtons that will hold the X and O pictures. I have the picture appearing when i click it once but it jumbles up all the other buttons which is why i have to run the method resetAllButton() to set everything back. Once i click the x again it puts everything back how it is supposed to be and works normal. How can i get this to work in one click.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicTacToeGui extends JFrame implements ActionListener {
I'm making a Pacman look-a-like game, now I'm making the map out of an 2D array (as a grid). I already initialized the 2d array spots with "0" and "1". I managed to do this but now I'm stuck.
I want to use the "0" and "1" to print the map. For example the "0" are grass and the "1" are walls. The map should be printed within a JFrame. And I would like to make the width and height 32x32 pixels.
I searched on the internet and i found a couple of example codes but non of it seems to work properly. This is the code I'm using to make the 2D array and initialize the spots.:
public class Level1 extends javax.swing.JFrame{ final int ROWS = 17; final int COLS = 17; int[][] field = new int[ROWS][COLS]; public Level1() { initComponents(); setLocationRelativeTo(null);
I want to add a picture to my buttons. The end statement will be a path finding algorithm that will with show the picture on all the buttons on the shortest path. I am using a constructor at the moment passing a picture object to it. It is suppose to work but its not on line 30...
Java Code:
import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.*; public class Screen implements ActionListener { public JButton[][] b=new JButton[20][20];
I am trying to make a japplet that has open, zoom in and zoom out buttons for pictures. I am not sure how to go about an open button that will read the file. I have the picture url hard coded in the file to open but would like an open button with a file/folder dialogue.
At the end of my main method, I want it to print out the input given that should have been stored in my sandwich class fields, but it didn't.
import java.util.*; import java.lang.*; public class SandwichBuilder { public static void main(String[] args) { Scanner inputDevice = new Scanner(System.in);
I am able to get the integer in the array printed but I am lost on how to hold and print the distinct numbers.
This program will read in 10 numbers from the user via the console. It will display on the console only the distinct numbers, i.e. if a number appears multiple times in the input it is displayed only once.
Here is a sample test run: Enter an integer: 2 Enter an integer: 6 Enter an integer: 1 Enter an integer: 8 Enter an integer: 6 Enter an integer: 1 Enter an integer: 2 Enter an integer: 4 Enter an integer: 7 Enter an integer: 5 The number of distinct values is 7 2 6 1 8 4 7 5
I am a beginning programmer and was learning how to make an XML. It was for a simple game and only needs to hold the highscores of three different levels. Here is what I coded:
try { OutputStream fout= new FileOutputStream("highScores.xml"); OutputStream bout= new BufferedOutputStream(fout); OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1");
[code]....
My issue is that I run this code every time I create the program and the scores are reset to 0. How can I make a small change so that I only run this block of code and create the XML the first time the program is run?
I am trying to write a TreeMap that can hold a max of 20 colors and a minimum of 8. I have a while loop using pollLastEntry to limit the max but I can't figure out how to set the minimum. The hex number is the map's key and the color name is the value. I tried to use entrySet() and iterator to just double the size of the map but map can't have multiple keys with the same value. It also seems that to set a minimum would require some kind of further input(which I'm trying to avoid) of colors and their hex numbers.
//Method to hard code the colors into the map public TreeMap<String, String> cm() { //Color Map <Hex number, Color name> //Uses a TreeMap to keep all the colors organized by key TreeMap<String, String> cMap = new TreeMap<String, String>(); cMap.put("FFFF00", " Yellow");
At this point I know how to utilize Google Maps within Android but it always seems to take up the full window, there is an image below which shows what I'm attempting to accomplish (having a box below the Google maps where I can store text i.e. "Hello World"
How do I add box below Google Maps, so to store text i.e. "Hello World"
Code so far:
ActivityMain:
Java Code:
public class MainActivity extends Activity { private GoogleMap googleMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
-Create a main method declares and creates an integer array called nums that can hold 15 integers.
-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.
-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.
-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.
Part III: More Array Methods
The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.
If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sumArray Method
The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315
The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142
Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.
I have a class with static ArrayLists to hold objects such as Members,Players etc.I want to save the class with the arrays so as to reload them again and hold onto the list of objects within those ArrayLists.
The ArrayClass
import java.io.Serializable; import java.util.ArrayList; public class ArrayClass implements Serializable {
[code]....
The arrays within the ArrayClass are empty when i reload the application.I cant tell if the arrays are being properly saved or is it in the reloading from file???
My friends and me are trying to make online Test taking system. We got a very basic doubt.
We have developed classes and relationship between classes for example : Online Test Taking system will have admin and student class. And Admin will have list of students.. etc.
But question troubled me was: if we use database to store all data for example student details then I can perform all sorts of operations writing sql query and store result in some other database then what is the need of "ArrayList<Student> field in Admin".??
Question is: We can put everything in database and manipulate using database(sql) functions to manipulate it.Then what is the need of Arraylist of anything which is just used to store object details for example student details....??
import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Screen implements ActionListener { public JButton[][] b=new JButton[200][200];
[Code] ....
I am trying to create the A* Algorithm and I REALLY need a 2D array to handle this.
This is the error:
Java Code:
at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) at java.awt.AWTEventMulticaster.componentShown(AWTEventMulticaster.java:162) mh_sh_highlight_all('java');
I am trying to create a game in java for my final project for schhol but am having problems with my jframe. I want my 3 Jbuttons to be in the center of the jframe and be vertical but can not seem to get the 3 buttons in the center of the jframe ....
import javax.swing.*; import java.awt.Container; import java.awt.*; public class SnakeObject extends JFrame { public SnakeObject() { createframe();
I have an application that reads through a ResultSet... while I'm reading that ResultSet, I am creating new JButtons..... Now I need to add an ActionListener to those JButtons...... but they are not visible to the actionPerformed method. So I thought I'd try to add an ActionListener as I created the buttons with the following code:
final File file = new File(rs.getString(3)); SKPictureButton spb = new SKPictureButton(url,file.getName()); spb.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent e )
[Code] ....
However this seems a bit excessive to me..... I don't think I really want to be creating a new ActionListener class for each JButton do I ? Is there a better way to make the JButtons created in this method visible to the actionPerformed method ? I'm just gettin' back into the "Swing" of things in Java.... haven't coded it for a while.
I am in the middle of creating a chess application, and am currently working on laying out a list of moves on the right of the actual board. Specifically, I want to have a JLabel that says "moves" on top of a JList that contains the moves, on top of two buttons, labeled "<" and ">" respectively, to which I will eventually add functionality to take back and un-take back moves. Currently I have the first two parts down, but when I add my buttons,the other components are getting messed up.
I have all of these things in a vertical BoxLayout inside of a JPanel with a fixed width of 70, and a height that varies according to the size of the window (I overrode the getPreferredSize method of the JPanel). The two buttons are within their own JPanel with a grid layout with one row and two columns. Then I basically just add the moves list label, the movesList, and the panel housing the buttons to the larger panel, in that order.
However, I am encountering two problems. One, the buttons display the text "... " instead of "<" and ">" as if there is not enough room, although I'm pretty sure three periods take up more space than one less than or more than sign. The next is that, without the buttons at the bottom, the label reading "moves" is appropriately centered (or left aligned, I can't tell which) over the movesList, and you can read its whole text. However, when I add in the buttons at the bottom, the label of the top shifts over to the right and then reads "mov..." because it has run out of room.
Here is my code:
//move list movesListLabel = new JLabel("Moves"); //the components for the actual movesList come from another class with an object called pen, but I don't think this is the problem listModel = pen.getListModel(); JList movesList = pen.getMovesList(); JScrollPane listScroller = pen.getMovesListScrollPane();
In my code 0,1,2 work fine an present my prompts an use my listener correctly but for some reason 3-6 is not an I don't know why? Here's my attached code.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class Guess extends JFrame
I am designing/coding a blackjack GUI program and I'm trying to add function to my JButtons. Problem is, I now have 2 NullPointerExceptions.
public class GameConsole { Player user; Player dealer; Deck playingDeck; ValidateInput validate = new ValidateInput();
[Code] .....
I am trying to do. I am getting the following exception/stack trace at line 14 in userTurn()
Exception in thread "main" java.lang.NullPointerException at blackjackControls.GameConsole.userTurn(GameConsole.java:163) at blackjackControls.GameConsole.gamePlay(GameConsole.java:87) at blackjackControls.Main.main(Main.java:7)
and then the program continues and after I click the button I get :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at blackjackControls.GameConsole.userTurn(GameConsole.java:163) at blackjackControls.ResultPanel$1.actionPerformed(ResultPanel.java:34) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028) //and then about 30 more "at" lines
Why am I getting these exceptions and how do I fix it?
I am trying to create a code which allows the users to create JButtons to a an existing frame.So what I want is that users can create buttons wihch opens a URL. The user need to be able to click on an Existing JButton called "Add Favorite", insert the name of the favorite and URL and the button is added with the functionality to open the URL in Internet Explorer.
I already have a beginning but have no clue how to add that functionaly to create JButton by pressing on button "Add Favorites" with the needed functionality The functionality needes to be added to Button4 ("add Favorite" ). As you can see, Button4 ("add Favorite") opens two inputShowDialog which allows the users to insert the name of the favorite and URL.
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JButton.*; import java.awt.Dimension.*; import java.lang.RuntimeException.*; import javax.swing.JOptionPane; public class Favorites extends JFrame implements ActionListener{ //private JPanel panel1 = new Jpanel ("Add Favorite");