Results 1 to 8 of 8
  1. #1
    Otakon is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    Otakon is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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:
    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");
    
        }
    
    
    }
    It's a bit sloppy but i think it could work after a few fixes.
    So, can you help?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  4. #4
    Otakon is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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
    Thanks, that did the trick.
    However, is there a way to change the size of the buttons?
    Right now they are all a bunch of lines and I would like to make them square and centered.

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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).

  7. #7
    Otakon is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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.

  8. #8
    Otakon is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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

    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)
        {
            
        }
    
    }
    The part left blank is not important for now.

    Also, I apologize if my requests seem a bit selfish; please bear with me.
    Thanks

    EDIT: nevermind, I found the problem
    Last edited by Otakon; 01-28-2010 at 11:26 PM.

Similar Threads

  1. Need Project Ideas
    By javaesh in forum New To Java
    Replies: 9
    Last Post: 05-24-2013, 03:56 PM
  2. School project: network program via java any ideas?
    By Sernomicus in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:46 PM
  3. Ideas anyone?
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-03-2009, 08:17 AM
  4. Ideas for Java Project
    By mrbharatmehta in forum Advanced Java
    Replies: 11
    Last Post: 03-28-2009, 07:42 AM
  5. Any Ideas for a Project?
    By quddusaliquddus in forum Advanced Java
    Replies: 19
    Last Post: 12-19-2008, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •