Articles on Java programming language.

Drawing AWT Rectangle with Mouse
AWT

Rectangle via drawRect: AWT Drawing – Part 3

In this part of the AWT Drawing, we will see how to draw a rectangle using a mouse. The AWT API requires a corner point plus width & height of the rectangle. Graphics API method drawRect needs four parameters to draw a rectangle. Now, have a look at the below picture: First two params are for locating the rectangle in the Graphic sheet. In our case, the sheet is a AWT Frame Window. The width and height define the rectangle’s dimension from the specified location. So, x and y denote the top-left corner of the rectangle and Width and Height denote the size of the rectangle. The below picture shows how a rectangle is drawn in the Frame window with the passed in four parameters:

Java AWT Line Drawing Sequence
AWT

Line Drawing : AWT Drawing – Part 2

A line requires two points. The JAVA AWT Graphics API also require two points to perform Line Drawing. In our Example, we will use left mouse press to record the first point and left mouse release to record the second point. Then, the Graphics API uses these two points to draw the line. This means, after pressing the mouse button, the user will drag the mouse to the location where they want the second point. Once the second point location is reached, the user will release the mouse button. The below picture explains the sequence of operation on the mouse to draw a line:

Graphics paint and repaint methods
AWT

Graphics Basic – AWT Drawing Part 0

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.

KeyEvent and KeyListener
AWT

AWT 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: