Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)


Graphics Basic – AWT Drawing Part 0

Graphics paint and repaint methods

In Java, Graphics objects take care of drawing works. The Graphics object draws the components of AWT components. In other words, all the AWT Components are linked with an AWT Graphics object which takes care of how to draw them. Note, java containers like Frame and Dialogs are also a component and hence they also contain the Graphics object.

The Graphics object holds the drawing factors like Color, Font, Line Thickness. It also exposes methods to do drawings like Line, rectangle, Circle. When the program coder invokes the drawing methods on the Graphics objects, it applies the properties like Color, Fonts etc. to the drawing.

Continue Reading →

AWT KeyEvent and KeyListener

KeyEvent and KeyListener

Java AWT tracks Keyboard Event through KeyListener. The KeyListener will get the KeyEvent which discloses data of user interaction with the keyboard. For example, it holds what key is typed by the user. The KeyEvent is raised when the user pressed a keyboard key and it also raised when he/she released it. The Event also tells what key is typed by the user. When these actions take place, AWT reports the KeyEvent to KeyListener. The KeyListener receives the KeyEvent on exposed function which tells what action is done by the user. The handler functions exposed by the KeyListener are below:

(1) keyPressed(KeyEvent e)
(2) keyReleased(KeyEvent e)
(3) keyTyped(KeyEvent e)

Below picture shows the relation between KeyEvent and KeyListener:

Continue Reading →

Java AWT Scrollbar Example

About AWT Scrollbar Example

In our example, we will have three panels and the AWT Frame hosts them via BorderLayout manger. The top panel hosts three AWT Scrollbar controls to denote red, blue, green color values. Middle panel will live in the centre part of the Frame Window. Gray Scale check box will sit in the South part of the Frame window’s border layout.

All three AWT Scrollbar together represent an RGB color value. So, each Scroll can produce a range of values which falls between 0 to 255. When the user adjusts the Scrollbar, we will create RGB Color and apply that to the panel in the middle of the Frame. When Gray Scale is in checked state, we produce the grey-scaled color and apply that to the middle panel. This example helps you to learn how to use AWT Scrollbar and how to handle the AdjustmentEvent raised by it.

Continue Reading →