So far, we have covered the basic programming constructs such as variables, data types, decision, loop, array and method and introduced the important concept of Object-Oriented Programming OOP. You can create high-level abstract data types called classes to mimic real-life things. These classes are self-contained and are reusable. Writing your own graphics classes and re-inventing the wheels is mission impossible!

Author:Grorr Dizshura
Country:French Guiana
Language:English (Spanish)
Published (Last):26 November 2012
PDF File Size:2.74 Mb
ePub File Size:19.14 Mb
Price:Free* [*Free Regsitration Required]

So far, we have covered the basic programming constructs such as variables, data types, decision, loop, array and method and introduced the important concept of Object-Oriented Programming OOP.

You can create high-level abstract data types called classes to mimic real-life things. These classes are self-contained and are reusable. Writing your own graphics classes and re-inventing the wheels is mission impossible! These graphics classes, developed by expert programmers, are highly complex and involve many advanced design patterns. However, re-using them are not so difficult, if you follow the API documentation, samples and templates provided.

I shall assume that you have a good grasp of OOP, including composition, inheritance, polymorphism, abstract class and interface; otherwise, read the earlier articles. I will describe another important OO concept called nested class or inner class in this article. For 3D graphics, read my 3D articles. AWT is huge! It consists of 12 packages of classes Swing is even bigger, with 18 packages of classes as of JDK 8.

Fortunately, only 2 packages - java. AWT provides a platform-independent and device-independent interface to develop graphic programs that runs on all platforms, including Windows, Mac OS X, and Unixes. In the above figure, there are three containers: a Frame and two Panel s.

A Frame is the top-level container of an AWT program. A Panel is a rectangular area used to group related GUI components in a certain layout.

In the above figure, the top-level Frame contains two Panel s. There are five components: a Label providing description , a TextField for users to enter text , and three Button s for user to trigger certain programmed actions. In a GUI program, a component must be kept in a container.

You need to identify a container to hold the components. Every container has a method called add Component c. A container say c can invoke c. For example,. GUI components are also called controls e. Each GUI program has a top-level container. Secondary containers are placed inside a top-level container or another secondary container.

AWT provides these secondary containers:. As illustrated, a Container has a LayoutManager to layout the components in a certain pattern. A java. Label provides a descriptive text string. Take note that System. You could use a Label to label another component such as text field to provide a text description. These three constants are defined for specifying the alignment of the Label 's text, as used in the above constructor.

The getText and setText methods can be used to read and modify the Label 's text. Similarly, the getAlignment and setAlignment methods can be used to retrieve and modify the alignment of the text.

You can create a Label without specifying an identifier, called anonymous instance. In the case, the Java compiler will assign an anonymous identifier for the allocated object. You will not be able to reference an anonymous instance in your program after it is created. This is usually alright for a Label instance as there is often no need to reference a Label after it is constructed. Button is a GUI component that triggers a certain programmed action upon clicking.

The Button class has two constructors. The first constructor creates a Button object with the given label painted over the button. The second constructor creates a Button object with no label. The getLabel and setLabel methods can be used to read the current label and modify the label of a button, respectively. We will describe Swing later. Clicking a button fires a so-called ActionEvent and triggers a certain programmed action.

I will explain event-handling later. TextField is single-line text box for users to enter texts. There is a multiple-line text box called TextArea.

You can convert a String to a primitive, such as int or double via static method Integer. To convert a primitive to a String , simply concatenate the primitive with an empty String. Let's assemble a few components together into a simple GUI counter program, as illustrated. It has a top-level container Frame , which contains three components - a Label "Counter", a non-editable TextField to display the current count, and a "Count" Button.

The TextField shall display count of 0 initially. This is because we have yet to write the handler for the Frame 's close button. We shall do that in the later example.

It is interesting to inspect the GUI objects via the toString , to gain an insight to these classes. For example, if we insert the following code before and after the setvisible :. The output with my comments are as follows. You could have an insight of the variables defined in the class.

In this example, the top-level container is again the typical java. The components are arranged in FlowLayout. The program shall accumulate the number entered into the input TextField and display the sum in the output TextField.

Java adopts the so-called "Event-Driven" or "Event-Delegation" programming model for event-handling, similar to most of the visual programming languages, such as Visual Basic. In event-driven programming, a piece of event-handling codes is executed or called back by the graphics subsystem when an event was fired in response to an user input such as clicking a mouse button or hitting the ENTER key in a text field.

In the above examples, the method actionPerformed is known as a call back method. In other words, you never invoke actionPerformed in your codes explicitly. The actionPerformed is called back by the graphics subsystem under certain circumstances in response to certain user actions. Three kinds of objects are involved in the event-handling: a source , listener s and an event object. The source object such as Button and Textfield interacts with the user.

Upon triggered, the source object creates an event object to capture the action e. This event object will be messaged to all the registered listener object s , and an appropriate event-handler method of the listener s is called-back to provide the response. In other words, triggering a source fires an event to all its listener s , and invoke an appropriate event handler of the listener s.

To express interest for a certain source's event, the listener s must be registered with the source. In other words, the listener s "subscribes" to a source's event, and the source "publishes" the event to all its subscribers upon activation. This is known as subscribe-publish or observable-observer design pattern.

A source fires an event when triggered. For example, clicking a Button fires an ActionEvent , clicking a mouse button fires MouseEvent , typing a key fires KeyEvent , and etc. How the source and listener understand each other? The answer is via an agreed-upon interface. For example, if a source is capable of firing an event called XxxEvent e. Firstly, we need to declare an interface called XxxListener e. For example, the MouseListener interface is declared as follows with five operational modes.

Secondly, all XxxEvent listeners must implement the XxxListener interface. That is, the listeners must provide their own implementations i. In this way, the listener s can response to these events appropriately. Thirdly, in the source, we need to maintain a list of XxxEvent listener object s , and define two methods: addXxxListener and removeXxxListener to add and remove a XxxEvent listener from this list.

The signature of the methods are:. Take note that the addXxxListenser takes a XxxListener object as its sole parameter. In other words, it can only add objects of the type XxxListener , and its sub-type. Since XxxListener is an interface , you cannot create instance of XxxListener , but need to create instance of a subclass implementing the XxxListener interface. In summary, we identify the source, the event-listener interface, and the listener object.

The listener must implement the event-listener interface. The source object then registers listener object via the addXxxListener XxxListener lis method. In summary, triggering a source fires an event to all its registered listeners, and invoke an appropriate handler of the listener. An ActionEvent listener must implement the ActionListener interface, which declares one abstract method called actionPerformed as follow:.

Note that addActionListener takes an argument of the type ActionListener. A WindowEvent is fired to all its WindowEvent listeners when a window e. The source of WindowEvent shall be a top-level window-container such as Frame.


Java2D: Increasing line width

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. So, first of all this is my first time posting and I am trying to make a top-down game with java 1. I have all of my code good and done for making the sprite show up , I test it and it keeps giving me error messages saying that certain parts are invalid. It is supposed to display an image character on the screen in the top right here is all code and please keep in mind that I do not have this fully finished:. It's because you never call the init method and, therefore the player and the spriteheet never gets created.


Java Programming Tutorial

As Java 1. Note that although Java 2D has been developed somewhat independently of the other parts of the JFC, it is nonetheless a core part of the 1. We will make the distinction and point out 2D-specific features for discussion, but you should remember that this functionality is just as central to 1. Java 2D extends the previous AWT mechanisms for drawing 2D graphics, manipulating text and fonts, loading and using images, and defining and dealing with colors and color spaces. We will be exploring these new mechanisms in this and future columns. I hope to provide other platform-specific tips and tricks where possible, but I will focus on Windows since that's where I will be spending most of my time. When I write a method name, it should always be of the form methodname.


Getting started with Java 2D



Subscribe to RSS


Related Articles