top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Graphics Programming using AWT (Part II)

0 votes
389 views

Window and Frame

A window represents any imaginary rectangular area on the screen without any borders or title bar. A frame represents a window with some title and borders.In any application, we create frames to represent various screens where the user can type some data for the application and output screens where the result may be displayed in a particular form. Such screens are nothing but frames only.

Creating a Frame

A frame becomes the basic component in AWT. The frame has to be created before any other component. The reason is that all other components can be displayed in

a frame. There are three ways to create a frame, which are as follows:

1) Create a Frame class object,

Frame f = new Frame();

2) Create a Frame class object and pass its title also,

  Frame f = new Frame("My Frame");

3) The third way is to create a subclass MyFrame to the Frame class and create and object to the subclass as:

class MyFrame extends Frame

MyFrame f= new MyFrame();

Since, MyFrame is a subclass of Frame class, its object f contains a copy of Frame class hence f represents the frame. In all these cases, a frame with initial size of 0 pixels width and 0 pixels height will be created, which is not visible on the screen, You may be wondering what these pixels are. A pixel (Short for picture element) represents any single point or dot on the screen. Any data or pictures which are displayed on the screen are composed of several dots called pixels. Nowadays, monitors can accommodate 800 pixels horizontally and 600 pixels vertically. So the total pixels seen on one screen would be 800x768 pixels resolution.

Since, the size of our frame would be 0px height, it is ot visible and hence we should increase its size so that it would be visible to us. This is done by setSize() method, as:

f.setsize(400,350);

Here, the frame's width is set to 400px and height to 350px. Then, we can display the frame, using setVisible() method, as:

f.setVisible(true);

Small Program to create a frame by creating an object to Frame class.

//Creating a frame 

import java.awt.*;

class MyFrame

{

            public static void main(String args[])

            {

                     //create a frame

                    Frame f = new Frame("My AWT frame");

                    //set the size of the frame  

                    f.setSize(300,250);  

                    //display the frame  f.setVisible(true);

            }

}

Output

posted Jul 13, 2018 by Jon Deck

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

                                                        Graphics Programming using AWT

Abstract Window Toolkit (AWT) represents a class library to develop applications using GUI. The java.awt package got classes and interfaces to develop GUI and let the users interact in a more friendly way with the applications.

Whenever an application (or software) is created, the user can work with it in two ways. The first way is where the user can remember some commands on which the application is built and type those commands to achieve the respective tasks. For example, the user may have to type a PRINT command to send a file contents to a printer. Here, the user should know the syntax and correct usage of the PRINT command. Then only he can interact with the application properly. This is called CUI (Character User Interface) since the user has to use characters or commands to interact with the application. The main disadvantage in CUI is that the user has to remember several commands and their use with correct syntax.

Hence, CUI is not user-friendly. A person who does not know any thing about computers will find this CUI very difficult.

The second way is where the user need no t remember any commands but interacts with any application by clicking on some images or graphics. For example, if the user want to print a file, he can click on a printer image and the rest of the things will be taken care of by the application. The user has to tell how many copies he wants and printing continues. This is very easy for the user since the user remembers only some symbols or images, like a magnifying glass symbol for searching, a briefcase symbol for a directory, etc. This environment where the user can interact with an application through graphics or images is called GUI (Graphics User Interface). 

GUI has the following advantages:

1) It is user-friendly. The user need not worry about any commands. Even a layman will be able to work with application developed using GUI.

2) It adds attraction and beauty to any application by adding pictures, colors, menus, animation, etc. We can observe almost all websites lure their visitors on Internet since they are developed attractively using GUI.

3) It is possible to simulate the real life objects using GUI. For example, a calculator program may actually display a real calculator on the screen. The user feels that he is interacting with a real calculator and he would be able to use it without any difficulty or special training. So, GUI eliminates the need of user training.

4) GUI helps to create graphical components like push buttons, radio buttons and check boxes use them effectively in our programs.

                                        Fig: Classes of AWT

Components

A  Component represents an object which is displayed pictorially on the screen. For example, we create an object of Button class as:

 Button b= new Button();

Now, b is the object of Button class. If we display this b on the screen, it displays a push button. Therefore, the object b, on going to the screen is becoming a component called 'push button',. In the same way, any component is a graphical representation of an object. Push button, radio buttons, check boxes, etc. are all components.

READ MORE

In a previous article, we discovered how to define and configure a Grid control using XAML. In this second article, I'll demonstrate how to do the same task using the code behind, I mean using C#.

Walkthrough:

1. To do so, create a new WPF project

Figure1

2. Right click on the window and select view code, then replace the existing code by this one

public partial class Window1 : Window

    {

        public Window1() {

            InitializeComponent();

            InitializeGrid();

        }

        private void InitializeGrid() {

            Grid oGrid = new Grid();

            oGrid.Width = 200;

            oGrid.Height = 200;

            oGrid.Background = System.Windows.Media.Brushes.Bisque;

            //Set the rows

            RowDefinition Row0 = new RowDefinition();

            RowDefinition Row1 = new RowDefinition();

            RowDefinition Row2 = new RowDefinition();

            //Set the columns

            ColumnDefinition Col0 = new ColumnDefinition();

            ColumnDefinition Col1 = new ColumnDefinition();

            ColumnDefinition Col2 = new ColumnDefinition();          

            //Add the columns and rows to the Grid control

            oGrid.ColumnDefinitions.Add(Col0);

            oGrid.ColumnDefinitions.Add(Col1);

            oGrid.ColumnDefinitions.Add(Col2);

            oGrid.RowDefinitions.Add(Row0);

            oGrid.RowDefinitions.Add(Row1);

            oGrid.RowDefinitions.Add(Row2);

            //Show the grid lines

            oGrid.ShowGridLines = true;

            this.Content = oGrid;

        }
    }


3. Run the project and observe, a window like this will appear, as you see the grid is visible now

 

The Star definition

it means that the related size could be expressed as weighted proportion of available space, for example, if a size of a given first row is double of a second given row size, then the first one will receive two units of the entire grid size, meanwhile, the second one will have one unit as size. Rows and columns sizes are expressed by this symbol * that represents a unit of size. To do so using C# code, use this code snippet. It should be copied and pasted within the scope of InitializeGrid().

private void InitializeGrid(){

            Grid oGrid = new Grid();

            oGrid.Width = 200;

            oGrid.Height = 200;

            oGrid.Background = System.Windows.Media.Brushes.Bisque;

            //Set the rows

            RowDefinition Row0 = new RowDefinition();

            RowDefinition Row1 = new RowDefinition();

            RowDefinition Row2 = new RowDefinition();

            //Set the columns

            /* This code add the star option to resize the

            First row as double of the rest of columns*/

            ColumnDefinition Col0 = new ColumnDefinition();

            Col0.Width = new GridLength(2, GridUnitType.Star);

    

            ColumnDefinition Col1 = new ColumnDefinition();

            ColumnDefinition Col2 = new ColumnDefinition();          

            //Add the columns and rows to the Grid control

            oGrid.ColumnDefinitions.Add(Col0);

            oGrid.ColumnDefinitions.Add(Col1);

            oGrid.ColumnDefinitions.Add(Col2);

            oGrid.RowDefinitions.Add(Row0);

            oGrid.RowDefinitions.Add(Row1);

            oGrid.RowDefinitions.Add(Row2);

            //Show the grid lines

            oGrid.ShowGridLines = true;

            this.Content = oGrid;

        }

Run the project and the result will be

 

The Pixel definition

It means that the size is defined in terms of pixels such as in the ASP applications. This bellow C# code illustrates how to define a dimension of a given column or row based on pixels.

private void InitializeGrid() {

            Grid oGrid = new Grid();

            oGrid.Width = 200;

            oGrid.Height = 200;

            oGrid.Background = System.Windows.Media.Brushes.Bisque;

            //Set the rows

            RowDefinition Row0 = new RowDefinition();

            RowDefinition Row1 = new RowDefinition();

            RowDefinition Row2 = new RowDefinition();

            //Set the columns

            /* This code add the pixel option to resize the

            First row as double of the rest of columns*/

            ColumnDefinition Col0 = new ColumnDefinition();

            Col0.Width = new GridLength(50, GridUnitType.Pixel);    

            ColumnDefinition Col1 = new ColumnDefinition();

            ColumnDefinition Col2 = new ColumnDefinition();          

            //Add the columns and rows to the Grid control

            oGrid.ColumnDefinitions.Add(Col0);

            oGrid.ColumnDefinitions.Add(Col1);

            oGrid.ColumnDefinitions.Add(Col2);

            oGrid.RowDefinitions.Add(Row0);

            oGrid.RowDefinitions.Add(Row1);

            oGrid.RowDefinitions.Add(Row2);

            //Show the grid lines

            oGrid.ShowGridLines = true;

            this.Content = oGrid;

        }

READ MORE
...