Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 Ideas on program layout
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message

PostPosted: Fri Jun 27, 2014 7:53 pm   Post subject: Ideas on program layout

So last semester I started using Java for the first time. I was already comfortable with Processing, so most of the concepts transferred over. While I was working with Processing, I had worked on creating a couple custom applications for people by just using graphics and creating my own widgets. Once I got into java and started really understanding the power of polymorphism and using swing, I got interested in making my own graphics and application creator software. Swing is very god, but I didn't really like the style of it, and I had lots of free time so I decided why not? I've built most of it and I'm at the point now where I'm just adding new features and tweaking things, so I'm curious about what you guys think of the style.

Basically your program runs off of a panel. In the library there is a ScreenPanel class which handles all the background and Swing stuff for you, so all you really have to focus on is the program itself. Your main class has a constructor, which should only have one line in it (nothing should be initialized there) to set the window size. There's three other methods that are the core of the application:

initialize() - This is where you can initialize all your wdgets and stuff, and the rest of your program.

Update() - This is where all your updating happens. You call your widgets update function (which can be shortened into one line if you wish), and all the updating stuff for your program should go here as well.

Draw() - same as the Update(), but you draw widgets, clear the screen, delay the program and draw the background. Widget drawing can be shortened into one line as well.

The widgets all come from a main widget class which has a few features and many fields such as border colour, fill colour, text colour, position, size, etc. All the things ou'd like in a widget. It also keeps track of the mouse relative to it, telling you if it's hovering, clicking or clicked. This class also contains abstract methods Update() and Draw() which is necessary for all widgets. Practically all fields have getters and most have setters.

The mouse is easily accessible but with some glitches because it's not in sync with the program, I think it runs on an alternate thread or something (I'm not sure how a bunch of the background stuff works yet, I got it off a tutorial). The keyboard sort of is too but I'm planning on making it much more user friendly. This also runs on an alternate thread I think using KeyListeners. The keyboard class has a list of KeyListeners and alerts everything in the list when a key is pressed, and you can easily add a widget to that list via the widgets constructor.

You can make your own widgets too if you want by extending widget. Drawing is pretty easy too. All widgets share an object called DrawObject which contains the graphics2D that the ScreenPanel is using, and drawing a circle is as easy as calling draw.oval(50, 50, 10, 10). It can also do other shapes, images and text.

Here's an example hello world program:


public class MainProgram extends ScreenPanel {
        // This is necessary to start the program and add the ScreenPanel to the window.
        public static void main(String[] args) {
                GUIRunWindow window = new GUIRunWindow(new MainProgramTemplate(), "Program Name");
        public MainProgram() {
                // Set the size of the screen and frames per second
                super(500, 500, 30);

        Button helloButton;
        Label helloLabel;
        Panel screen;

        public void initializeProgram() {
                helloButton = new Button(50, 50, 100, 50, "Say \"Hello World!\"", null) // button 100 wide, 50 high, with no image
                helloLabel = new Label(50, 110, 100, 50, "");

                screen = new Panel(0, 0, WIDTH, HEIGHT) // WIDTH and HEIGHT are part of screen panel. In this program they are both 500
        public void Update() {

                if (helloButton.isClicking()) {
                        helloLabel.setText("Hello World!");
                } else {

        public void Draw() {
                DrawToScreen(); // This draws everything in the shared graphics object to a BufferedImage which is rendered to the screen
                Timer.correctedDelay(DELAY_TIME); // DELAY_TIME is a variable representing the delay based on the frames per second.
                // Corrected delay takes into account the time passed to process the program in each frame.

What do you guys think?

PostPosted: Sun Jun 29, 2014 7:28 pm   Post subject: RE:Ideas on program layout

Also if anyone's curious I could provide source code, although I think it's a little messy right now.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to: