Results 1 to 10 of 10
  1. #1
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default Organizing (GUI-)Code

    Hey there,

    I finished my first introductory Java book and completed a video course on YouTube too, and right now I am working on "Java ist auch eine Insel", which is one of the most popular and complete German book on Java and can be regarded the German standard book for learning Java.

    While working through the book I want to start my first own project, which will be much more complex than the typical examples from books, but will still be a very very simple dice game.

    So far I understood most of the concepts of Java roughly; I still have to look up a lot of techniques because I did not yet memorize them, but in general I have a basic understanding of how things work and why.

    One of my biggest problems with Java, however, is organizing my code, especially when it comes to GUI programming. That's something that is hardly ever explained in detail in introductory books (since GUI introductions are usually limited to an introduction of central classes and methods), and I have great difficulties figuring out a useful way for me.

    I am aware of the fact that there are tons of specialized books on this very subject, but still I was hoping that some of you experienced people could at least provide me with some general ideas of how you organize your code.

    For my own little project I thought of something like this: I will have a separate class for the dice, one class for the human player, one class for the AI (i.e. the computer players), one class for the game points table and the current game status, one class for all the GUI stuff and a separate class to cover main().

    The first classes are supposed to generally control the gameplay, the UI class is supposed to provide the interaction with the GUI Elements, and the game class should just contain main() and put everything "in line".

    Does this sound like a useful way?

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    A usefull paradigm for such examples is the MVC style. It means Model, View, Control. In a short example, if we look at one of your dice, the model class would define the dice (variables, states,...), the view class would be responsible for drawing the dice, and the control class would define changes to the model (rolling the dice). Look it up, there are many explanations that make it more understandable than what I'd be able to convey.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Sounds like you hit it on the head there, that sounds like a good distribution of classes. You may find that the UI class will get large, and you may want to split it up (I can't see your program nor your full idea in your head, so I can't say for sure--but some UI elements are unique from others). But you're definitely on the right track--your classes are not too small or too large.

    If, when you're working on the project, you find a class getting too long, figure out exactly what you want that class to do, then split off unrelated functions. If a class has only one or two methods, consider whether or not they could be better put in an existing class.

    For me, determining classes is a bit of a gut feeling; do your classes feel natural? Are they bulky or underdone? You'll know when you write it.

  4. #4
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    I started working on my dice game now and I already ran into some (minor) problems. Seems that some things I thought were a great idea didn't even work (at least not with my knowledge of Java so far), not to speak of working good. :-)

    Haha, but I guess that's what programming is all about. :-)

    Thanks for your comments so far, but still, I'd appreciate some general hints & clues. How do you people organize (especially) your GUI code?

    Would it make sense to make attributes and methods in a GUI class static?

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

    Default

    Quote Originally Posted by jDennis79 View Post
    Would it make sense to make attributes and methods in a GUI class static?
    Nope. Just because one is coding a GUI doesn't mean that one throws OOP principles out the window.

  6. #6
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Well, my idea behind that question was that I don't have to instantiate my GUI if there's only one main window. What would I need an object for if I don't create more than one window?

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

    Default

    For one: Say you have a fairly complex GUI, and that later you want to refactor your program, to clean up your MVC or to just split up the GUI or "view" functionality. If you haven't started out with a clean OOPs paradigm this could get quite messy fast.

    Next: say you want to change your app to a client-server app, you'll again need a decent OOPs structure.

    One of the reasons for the OOP paradigm is that it allows us to more easily extend, adapt and debug our programs.
    Last edited by Fubarable; 08-13-2010 at 11:00 PM.

  8. #8
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    Okay, that makes sense to me. In my example above, where would I instantiate the GUI class? Within main()?

  9. #9
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    My way for GUI is:

    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class ExampleGUI {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				ExampleFrame frame = new ExampleFrame();
    				frame.createAndShowGUI();
    			}
    		});
    	}
    }
    
    class ExampleFrame extends JFrame {
    	public ExampleFrame() {
    		// here I put code for components Panels, Labels, Buttons, etc.
    	}
    	
    	public void createAndShowGUI() {
    		// here I put code for initial Frame values size, position, etc.
    	}
    	
    	// add listeners in your own class and make them private :-)
    	// do not use for example "class ExampleFrame extends JFrame implements ActionListener
    	private class MyActionListener implements ActionListener {
    
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    		
    	}
    }
    
    class ExamplePanel extends JPanel {
    	public ExamplePanel() {
    		// components in panel
    	}
    	
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		// code for painting here
    	}
    }
    p.s. cselic® :D
    Last edited by cselic; 08-13-2010 at 11:41 PM.

  10. #10
    jDennis79 is offline Member
    Join Date
    Aug 2010
    Posts
    23
    Rep Power
    0

    Default

    That looks well-arranged. I'll have a closer look at it when I get to the actual GUI designing of my project.

Similar Threads

  1. Organizing Java code
    By PeterTaps in forum New To Java
    Replies: 2
    Last Post: 06-11-2010, 10:39 PM
  2. Traffic Organizing In JAVA2D Problems
    By tamer in forum Java 2D
    Replies: 5
    Last Post: 04-26-2010, 12:41 AM
  3. organizing elements in the jpanel/jbox
    By okabeer in forum AWT / Swing
    Replies: 7
    Last Post: 04-15-2009, 01:42 PM
  4. organizing larger application code / modules
    By zenner in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2008, 10:18 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •