Showing posts with label repaint. Show all posts
Showing posts with label repaint. Show all posts

Multithreading

I am just starting to learn multithreading. My second exercise was to put 20 balls on the screen. Each one is moved using a different thread. On the surface I was surprised what little computer resources the 20 threads used. My machine is dedicating 3 percent of its CPU to the bouncing balls app.

I have some nice and smooth animation too. Every frame update causes the whole screen to be repainted. Right now I am using the basic Thread class to start up and Runnable. My main thread, which I assume is the event dispatch thread, updates the GUI on a timer (which happens to have its own thread).

Next I should try a whole lot more threads. Or I could make the graphics more intense. I need to figure out where the performance bottleneck is, if any.

Button Behavior

You size an applet in the HTML page that hosts it. I find that you need to size the applet area carefully to allows your controls to be layed out as expected on the screen.

The general pattern I use when user input drives the display is to have a member variable of my JApplet derived class. There is conditional logic in the paint() method that depends on this variable. When a user action changes the variable state, repaint() is called to redraw the screen.

I've been doing a lot with buttons that have no text on them. They only have an icon image on them. I have encountered some problems with transparency. I would like the button background color to bleed through any white portion of my image. However does not seem to be the default behavior of the button class. Maybe I need to set some unknown options. I hope I do not need to code or draw my own button face.

Wish Upon a Star

I am moving on to writing more applets. These are simple programs to get my bearings. The most important methods in JApplet are init() and paint(). You add items or objects to the screen in the init() function. And you draw things such as labels and buttons in the paint() function.

If you handle some GUI events like button clicks, the pattern is to call repaint() to get the screen updated based on the user input. It is funny how quickly I forget my Java basics when I do not code for a long time. These rules have not been committed to heart.

For example, I had to rediscover how to convert a String to an int. You create an Integer object, passing the String into the constructor. Then you can assign the resulting Integer to a variable of type int. Also you need to qualify Math package functions with "Math." to get your Java to compile.

Some topics are universal. Break down complex routines into multiple subroutines (functions). Make use of constants instead of magic numbers. In Java this is done with public static final variables. I will say I am starting to get some bearings on how to do drawing on the Canvas. The upper left hand corner of the screen has coordinates (0,0). Increasing x goes to the right. Increasing y goes down.