Results 1 to 8 of 8
- 12-22-2009, 05:40 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Need ideas and help with my project
Hello,
I just started using Java and I was given an assignment to make a simple Java program/applet but the problem is, I have no idea what to do or where to start.
My knowledge of Java is minimal so there are a lot of things I do not understand.
After browsing the forum for a while I found some interesting ideas but like I said, I have minimal knowledge and don't know anything.
I thought about making a code for Hangman or Memory game but those seemed a bit too hard so I found a pretty simple code for Blackjack but the user is offline for several years now so there is no way to ask him.
So, can I post his code (or after I modify it a bit) or simply put a link?
Also, if possible, can someone post a few example codes (whole codes) or PM them so I can get the general idea for a simple program (like Blackjack, Hangman...)?
Thanks and sorry for the trouble.
- 12-22-2009, 07:37 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
I decided to try a "manual" Tic-Tac-Toe (TTT) but I'm stuck.
The idea was to create 9 buttons (3 in each line) to make it look like a TTT grid and after a player clicks a button (button would be disabled) either an O or an X would appear on that spot (depending on the turn player) and using the "New game" button, all buttons would be enabled; the problem is I don't know how to stretch the buttons and one line of them is missing.
I imported a few random stuff in case I will need them.
Here is the code I have so far:
It's a bit sloppy but i think it could work after a few fixes.Java Code:import java.applet.Applet; import javax.swing.JApplet; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class KK extends JApplet { public KK() { Container aC=getContentPane(); JButton b1; b1=new JButton(); b1.setEnabled(true); JButton b2; b2=new JButton(); b2.setEnabled(true); JButton b3; b3=new JButton(); b3.setEnabled(true); JButton b4; b4=new JButton(); b4.setEnabled(true); JButton b5; b5=new JButton(); b5.setEnabled(true); JButton b6; b6=new JButton(); b6.setEnabled(true); JButton b7; b7=new JButton(); b7.setEnabled(true); JButton b8; b8=new JButton(); b8.setEnabled(true); JButton b9; b9=new JButton(); b9.setEnabled(true); JPanel pan1; pan1= new JPanel(); aC.add(pan1,BorderLayout.NORTH); pan1.add(b1); pan1.add(b2); pan1.add(b3); pan1.setLayout(new GridLayout(1,1)); JPanel pan2; pan2= new JPanel(); aC.add(pan2,BorderLayout.CENTER); pan2.add(b4); pan2.add(b5); pan2.add(b6); pan2.setLayout(new GridLayout(1,1)); JPanel pan3; pan3= new JPanel(); aC.add(pan2,BorderLayout.SOUTH); pan3.add(b7); pan3.add(b8); pan3.add(b9); pan3.setLayout(new GridLayout(1,1)); JButton b01; b01=new JButton("New Game"); } }
So, can you help?
- 12-22-2009, 07:45 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,407
- Blog Entries
- 7
- Rep Power
- 17
Make the layout manager for your top level component a GridLayout(3, 3) and add those nine buttons to it; a BorderLayout is not suitable for this purpose.
kind regards,
Jos
- 12-22-2009, 10:09 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
- 12-22-2009, 10:25 PM #5
I believe there's a button.setSize() method. It's been a while since I messed around with layouts but look at the layout tutorial supplied by sun. I think you have to change your gridlayout to have a certain size and all this stuff as well.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
-
Avoid using setSize when working with layout managers. Most will respect setPreferredSize more than setSize. If you use a GridLayout (which is the proper thing to do here), your choice is to set the preferredSize of one of the buttons (all components held by a GridLayout-using Container are sized the sized the same), or set the preferredSize of the JFrame's contentPane, your choice. If it were me, I'd play around with different settings to see which worked best.
Much luck.
edit: I see that you are making a JApplet, not a JFrame. In this situation all bets are off as I believe that it's the HTML code that is used to display the applet that sets its size (I'm not 100% sure as I don't use applets).
- 01-02-2010, 08:59 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Yo, sorry it took so long; i was away for obvious reasons.
Anyway, thanks for the help, setPreferredSize did the trick.
Now, what I want to know; is it possible to "connect" my applet to a server?
The idea was that the button changes itself to setEnabled(false) when either player click's a button.
Is that possible and if so, is there a way to "set" one player to "X" and the other to "O" , or is a counter enough?
Thanks.
- 01-28-2010, 09:49 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Well, I'm back to this problem again; I made a working TicTacToe with AI (it has 7000 lines lol) but now I have to make it shorter by using "For" and "Arrays".
I wanted to see if the Panel will "materialize" but no such luck, I've been getting the usual "Error. Click for details" message and it says "...Caused by: java.lang.NullPointerException..."
The code SEEMS okay but maybe I'm missing something
The part left blank is not important for now.Java Code:import java.applet.Applet; import javax.swing.JApplet; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import java.util.*; public class KK8 extends JApplet implements ActionListener { JButton[] but; JButton b01; int[] free; int[] freeA; int[] freeB; int counter=0; int rnd=0; int c2=0; int i; Random generator = new Random(); public KK8() { Container aC=getContentPane(); for(i=0;i<9;i++) { but[i]= new JButton(); but[i].setPreferredSize(new Dimension(50, 50)); but[i].setEnabled(true); but[i].addActionListener(this); } JPanel pan1; pan1= new JPanel(); aC.add(pan1,BorderLayout.NORTH); for(i=0;i<9;i++) { pan1.add(but[i]); } pan1.setLayout(new GridLayout(3,3)); b01=new JButton("New Game"); b01.addActionListener(this); b01.setBackground(Color.yellow); JPanel pan2; pan2= new JPanel(); aC.add(pan2,BorderLayout.SOUTH); pan2.add(b01); } public void actionPerformed(ActionEvent e) { } }
Also, I apologize if my requests seem a bit selfish; please bear with me.
Thanks
EDIT: nevermind, I found the problemLast edited by Otakon; 01-28-2010 at 11:26 PM.
Similar Threads
-
School project: network program via java any ideas?
By Sernomicus in forum New To JavaReplies: 2Last Post: 12-07-2009, 03:46 PM -
Ideas anyone?
By SwEeTAcTioN in forum New To JavaReplies: 6Last Post: 12-03-2009, 08:17 AM -
Need Project Ideas
By javaesh in forum New To JavaReplies: 8Last Post: 08-25-2009, 06:59 PM -
Ideas for Java Project
By mrbharatmehta in forum Advanced JavaReplies: 11Last Post: 03-28-2009, 07:42 AM -
Any Ideas for a Project?
By quddusaliquddus in forum Advanced JavaReplies: 19Last Post: 12-19-2008, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks