[Non-game] Some questions about making a program

I have been tasked to make a check in/out program for my job. While I have worked with games before, I have never really done just a simple program, and I’m sure I have made a couple of mistakes.

First of all, I believe what I’m using is called passive rendering. The program doesn’t use any keyboard, or mouse input. So I decided to make it look nice with a full screen window, that have a background and a good looking list of all the employees that have checked in. (We use a card scanner to check in/out)


@Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        //Rendering code
        repaint();
    }

But then I come to the thing I’m most questionable about, I have used a game loop to check in with a “in house” website now and then, to collect information. I have been thinking of adding a sleep, yield, or another kind of timer function.

public void processing(){
        while(isRunning){
            long now = System.currentTimeMillis();
            if(now-lastHttpCheck > getSoftwareRefreshRate()){
                lastHttpCheck = now;
                //Resive information from a in house apache server
            }
        }
    }

First: don’t put repaint() in paintComponent(). Probably also remove the super.paintComponent().

For timing, why not just Thread.sleep()? For instance, to refresh every 30 seconds:

while(running) {
    // retrieve information
    // do whatever with it
    Thread.sleep(30 * 1000); // repeat in 30 seconds
}

This doesn’t seem like it needs to be very precise, esp. since it’s (seems to be) fetching data over the network, so that will already be unpredictable.

From what I know repaint just schedules the next time the program need to render again, and without repaint everything just render once. But I will take your advice about sleep, since that was what I was thinking of doing, just wanted to check if it was the right way.

Make sure you’re not putting your game loop on the event dispatch thread. Best advice would be to get rid of the loop completely and use a Timer and TimerTask to fetch data from the website. This will keep the GUI responsive (even with no input, speedy repaints when needed are always nice). One other note, set the Timer as a Daemon to ensure it closes when the rest of the application closes.

repaint() does indeed only schedule a repaint, but in paintComponent() you are already painting. Put repaint() in the main loop, right before sleep().

It took some time, but I have now made the program use TimerTask and Timer when it needs to execute something with a delay, and it’s regular check up with the website. I still uses repaint in the paintComponent, I could possibly move it to a TimerTask and use “scheduleAtFixedRate” every 1000 ms (the window have a clock that need to update each second.)

But I have encountered one other problem, the java program encrypts some data before it send it to the server. But now the website need to do that too. So I wonder can I pass some arguments into a jar, and get a string out directly? or do I need to make the jar save the result into a file which it put into a temp folder, which the website can read?

I’d look into using HTTP to send data to the web, e.g. POST. Then I’d look into something other than my own design when it comes to security.

You mean you want to launch your program like: java YemtosProgram Argument1, Argument2

Well, you can get the arguments in your main like this:

main(String[] args){
   if(args[0].equals("JGO") && args[1].equals("Overlord") ){
      System.out.println("Riven");
   }
}

So, if you launch this program now like: java YemtosProgram JGO Overlord
It returns “Riven” immediately.

It returns ArrayIndexOutOfBoundsException. “Overlord” is args[1].

Woops, already modified. In C++ or whatever it was has an empty first parameter, nvm.

So I just need to use System.out.print to pass the string into a variable in php?

Ehh, not really, if you want to use it with other programs you need to write it to a file/database, and read it with your php program.

Thanks, that was the answer it was looking for.