Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Moving away from a frame made of buttons(tic tac toe)  (Read 3671 times)
0 Members and 1 Guest are viewing this topic.
Offline smores

Junior Newbie





« Posted 2012-02-20 20:12:35 »

Hey what's up everyone I'm new to Java and programming in general and I would like to know if there is another way to do the following:

I followed a tutorial online to make a Tic Tac Toe game that is GUI based. The game composed of an array of JButton's and a JFrame - all held in the same class file. I know I can setIcon for the JButtons but I hope to eventually move into real graphics so I figure this is the simplest place I can start.

I wanted to expand my knowledge with the Java graphics library so instead of setting the text in the JButton to "X" or "O" I wanted to draw (on a JLabel maybe?) a Circles / X's.

Could anyone give me any pointers on what libraries I should look into or maybe any TicTacToe tutorial that does what I am hoping to do?

Thanks for any insight you can offer.





Offline UprightPath
« Reply #1 - Posted 2012-02-20 20:19:46 »

For Tic-Tac-Toe you probably don't need to use an outside Library to do the art. Java2D, which comes built into any Java SDK, will provide most of what you need. Basically, instead of putting a bunch of buttons onto the screen, you'd be painting directly onto a panel in your JFrame. That Panel would have a MouseListener attached to it, to catch where you're clicking, and some logic there to figure out which square in the game grid was clicked on.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #2 - Posted 2012-02-20 20:30:55 »

Instead of buttons, you could just extend JComponent and draw in its paintComponent(Graphics g) method. Then just check if it was pressed or not using a MouseListener.

EDIT: grrr UprightPath beat me to it......but I suggest JComponent instead of JPanel. I don't know where this nasty habit started but it has to end Cranky

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline smores

Junior Newbie





« Reply #3 - Posted 2012-02-20 21:01:11 »

Awesome thanks for the suggestions - i'll get to work right away!
Offline UprightPath
« Reply #4 - Posted 2012-02-20 21:57:52 »

For me? Habit honestly. JComponent is probably since JPanel is meant to hold something instead of be something.

As well, I'm pretty dang new to Graphics when it comes to Java. I've only done a little, silly, zombie simulator thinger to practice Factories, then an entry level fractal program and a connected component node-map. I don't know if there's anywhere listing bad habits like using JPanel instead of JComponent, if there's not it might be a good idea to compile the list of faux pas like that, so we newbies to the area of graphics know better next time.

Offline Z-Man
« Reply #5 - Posted 2012-02-20 22:25:37 »

If you don't need to add anything to it, why not just use a Canvas?
EDIT: Fixed the link.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-02-20 23:18:44 »

Because it's unwise to mix AWT and Swing components Smiley

Offline evilfrenchguy

Junior Member


Medals: 3



« Reply #7 - Posted 2012-02-20 23:47:58 »

Because it's unwise to mix AWT and Swing components Smiley

Isn't JComponent in swing too?
Offline UprightPath
« Reply #8 - Posted 2012-02-21 00:16:06 »

Well, he was going to have a JFrame, with something inside of it. JFrame is a Swing component (It's built on AWT, yes, but it's in the purview of Swing). And you don't want to have an AWT thinger inside of a Swing thinger.

Offline smores

Junior Newbie





« Reply #9 - Posted 2012-02-21 01:37:08 »

Eeek okay - I'm sorry for bug you guys again but I've spent about ~ 2 hours trying to figure this out and the Java API is still a bit confusing to me, I didn't want to come back and post again about the same issue but I am cvonfusing myself the further I investigate.

When I call paintComponent n my Board constructor - does this call only occur once? I ask because I use about ~15 lines of code to draw my Board (using 2DLine) and it seems like its a bit of a waste of memory/time if paintComponent is called every time I use repaint() - and if that is the case how would I be able to store freshly painted X's and O's if they are overridden each time by paintComponent(the method I am using to draw the board) ?

That being said... I can't figure out how to paint to only a particular area of the screen - I understand how to implement MouseListener and to get coordinates clicked but I don't understand how I can draw to only a specific region without being tedious.

For example : Player clicks @ (50,60) (the upper left spot) - is there a way to designate that spot (those upper left coordinates) to an object and then draw onto that object [by object I mean something to represent that upper left region on the jcomponent]? I'm trying to figure out how I would check win conditions etc if I have no way to organize the board. Should I use a JLabel or something of the sort and draw on there?

I'm not sure if I took your advice of JComponent to literally although I fear I have.

So far I have two classes - the TicTacToe class featuring main & extending frame - and the Board class which I hope to implement MouseListener, game logic and painting.


Well, he was going to have a JFrame, with something inside of it. JFrame is a Swing component (It's built on AWT, yes, but it's in the purview of Swing). And you don't want to have an AWT thinger inside of a Swing thinger.

Is there a way to do this without a JFrame ? I wasn't aware of there being other frames I could use.





Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline UprightPath
« Reply #10 - Posted 2012-02-21 01:49:34 »

It's a hint more complicated than you make it sound.

First, the act of painting is not done by you, it's handled by the Swing/AWT Gui thread. When you call 'Repaint', it will signal that a repaint is needed. If you call it, it guarantees that at some time in the future, the Component will be painted. That is, if you call repaint a hundred times before the GUI Thread gets priority, it will only paint once (Probably, correct me if I'm wrong).

However, fifteen lines to paint your Tic-Tac-Toe board? Probably not a big draw on your CPU.

As for only repainting one region? That gets a little more complicated, and is probably too much to worry about right now (Especially since you'll probably spend more time figuring out which portion to repaint, than repainting). Because if you only repaint when someone clicks, you're going to have several unplayable zones. Especially if you end up using anything else (Menus, etc.).

Since you sound like you're still trying to figure out how you're going to have your logic, you should leave it how it is (Since it works) and work on getting your logic working with what you've got. Especially if you're making a "play against the computer" version.

Offline smores

Junior Newbie





« Reply #11 - Posted 2012-02-21 01:53:38 »

It's a hint more complicated than you make it sound.

First, the act of painting is not done by you, it's handled by the Swing/AWT Gui thread. When you call 'Repaint', it will signal that a repaint is needed. If you call it, it guarantees that at some time in the future, the Component will be painted. That is, if you call repaint a hundred times before the GUI Thread gets priority, it will only paint once (Probably, correct me if I'm wrong).

However, fifteen lines to paint your Tic-Tac-Toe board? Probably not a big draw on your CPU.

As for only repainting one region? That gets a little more complicated, and is probably too much to worry about right now (Especially since you'll probably spend more time figuring out which portion to repaint, than repainting). Because if you only repaint when someone clicks, you're going to have several unplayable zones. Especially if you end up using anything else (Menus, etc.).

Since you sound like you're still trying to figure out how you're going to have your logic, you should leave it how it is (Since it works) and work on getting your logic working with what you've got. Especially if you're making a "play against the computer" version.

Aw bummer - alright. I was planning on implementing AI into the first version of my game - I guess I'll have to wait til I learn more about the Swing/Awt in next semester's course(this semester is a bit of a joke but it's a pre-req to the second level Java Programming) - unless you may be able to recommend a specific book ? I find the Sun tutorials to be thorough but not very noobie friendly - its more like straight information being thrown at me.
Offline UprightPath
« Reply #12 - Posted 2012-02-21 02:04:43 »

I can't recommend anything really. Well, except to look online for Java2D tutorials and the like. A bit of advice would be to look up KevGlass's site ( http://cokeandcode.com/ ) and look at the tutorials he has up. They'll probably help with the learning about drawing and the like. Other than that, just mess around with it. That's really how I learned (Spending hours and hours programming Maths and the like to figure out the line between two boxes and where the box's edges are to paint arrows). And it's probably how a lot of people learn.

But really, whatever works is what's best for you. If you spend hours trying to do it fancily, and you can't get it work, then while it was probably informative (in what not to do) the other way was probably better for you.

As for AI? There are some things you can do there that are fun. And many learning experiences: http://en.wikipedia.org/wiki/Game_tree . Which answers a lot of the questions about how you'd go about doing the AI for your T-T-T game. And, you'll get a jump on your competition (Class Mates) by learning something that comes up in most Algorithm classes.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-02-21 02:55:39 »

You could create a "Tile" class that represents one of the 9 seconds of the board, give it an X and Y and then cycle through all the Tiles and call their draw(Graphics2D) method from your paintComponent method. Smiley

For example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
public class Main extends JComponent {
    public static void main(String[] args) {
        //setup JFrame
       frame.add(new Main());
    }

    private Tile[][] board;
   
    public Main() {
        board = new Tile[3][3];
        for(int y = 0; y < board.length; y++)
            for(int x = 0; x < board[y].length; x++)
                board[y][x] = new Tile(x,y);
       
        //add mouse listeners
       //mouse listeners call repaint() when the mouse is clicked.
   }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
       
        for(int y = 0; y < board.length; y++)
            for(int x = 0; x < board[y].length; x++)
                board[y][x].draw((Graphics2D)g);
    }
}

public class Tile {
    private static final int SIZE = 20; //each tile's width and height is equal SIZE
   private int x, y, type; //type: 0 - blank, 1 - X, 2 - O
   
    public Tile(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getType() {
        return type;
    }

    //call this to set X or O if this Tile has been clicked.
   public void setType(int type) {
        this.type = type;
    }
   
    public void draw(Graphics2D g) {
        g.setColor(Color.black);
        g.drawRect(x*SIZE,y*SIZE,SIZE,SIZE);
       
        switch(type) {
            case 0: return; //it's blank, don't draw anything
           case 1:
                //draw an X from (x*SIZE,y*SIZE) to (x*SIZE+SIZE,y*SIZE+SIZE)
               break;
            case 2:
                //draw an O from (x*SIZE,y*SIZE) to (x*SIZE+SIZE,y*SIZE+SIZE)
               break;
        }
    }
}

Offline smores

Junior Newbie





« Reply #14 - Posted 2012-02-21 03:25:22 »

Thank you for being so helpful, both of you.

Ra4king that is pretty much what I was trying to get at I just wasn't sure what to do :X

I'm stoked to check out Glass' site , I have 7 hours of free time between my first and second class tomorrow - what a great way to spend it! Hopefully I can become a bit more active around here - seriously I am loving this soooooooooooo much - maybe a bit to much. Staying in on Friday/Saturday nights so I can work through a java book ? Who needs to go out Tongue
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2012-02-21 04:26:51 »

7 Hours?!?! Shocked I'm jealous Tongue

And yeah....screw social life! persecutioncomplex

Offline UprightPath
« Reply #16 - Posted 2012-02-21 04:40:57 »

You mean you don't spend class time reading books or knocking out code!?

Offline smores

Junior Newbie





« Reply #17 - Posted 2012-02-21 04:50:34 »

I don't think my pre-calculus teacher would approve of me programming in class so that's a no-no Tongue

As for the Java course, I've already finished the work for this semester (we are only heading up to chapter 9 - which introduces how to use JComponent and a Frame to draw a circle - i've decided to move on and am approaching the end of chapter 12) That was the pinnacle of this course - took me 3 weeks to get there and now I'm bored (hence why I hopped on here - game programming is something I'm very interested in). The first 9 chapters are pretty much - the jvm, syntax, how to declare variables, an array and touching on class design. Pretty lame course.

The assignment due last week was something along he lines of storing two names and variables and outputting which person had the greater age - spent the entire time reading ahead in the book (it goes from 17:00-23:00) - Also have been looking into C++ to familiarize myself with the syntax of that language since I am required to take it next semester Tongue
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-02-21 05:38:59 »

Well I'm still in high school (I'm 16) so no access to any computers during classes, which go from 8:20 to 15:30 every day Smiley

Offline UprightPath
« Reply #19 - Posted 2012-02-21 05:39:44 »

Dear lord, you people is young. I'm already starting my SE masters. xD

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!