Saturday, April 13, 2013

Image display


This is a program to display an image on the program window. The image object is obtained from the image file. It is drawn on the Canvas drawing object. Then the drawing object is added to the JFrame to display.  The code to draw somethings on the drawing object has be placed in the paint method of the Canvas class.


ImageDisplay source code:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.geom.*;
class Display extends Canvas{
            Image img;
            Dimension ds;

            Display(){

1                      img=getToolkit().getImage("image02.jpg");
2                      ds=getToolkit().getScreenSize();
                       
                                    }
           
            public void paint(Graphics g){
                       
3                      displayImage(g);
            }

            public void displayImage(Graphics g){
4                      Graphics2D g2d=(Graphics2D)g;
5                      int iw=img.getWidth(this);
6                      int ih=img.getHeight(this);
7                      int mX=(int)ds.getWidth()/2;
8                      int mY=(int)ds.getHeight()/2;
9                      g2d.translate(mX,mY);
10                    g2d.drawImage(img,-img.getWidth(this)/2,-img.getHeight(this)/2,iw,ih,null);
                       
            }
           
           
}
11 class  ImageDisplayInterface extends JFrame{
            ImageDisplayInterface(){
12                    setTitle("Image Display");
13                    add(new Display(),BorderLayout.CENTER );                
14                    setDefaultCloseOperation(EXIT_ON_CLOSE);
15                    setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
16                    setVisible(true);
                        }

           

}

public class ImageDisplay{
 
public static void main(String args[]){
     new ImageDisplayInterface();
  
}


}


Code Explanation

1 Create an image object to capture the image file image02.jpg by using the getImage method of the Toolkit class.
2 Get the dimension (width and height) of the computer screen.
3 Invoke the displayImage method to display image at the specified location.
4 The Graphics2D is cast from the Graphics class. Graphics2D allows you to move coordinate, rotate, or scale image object.
5 Get the width of the image by invoking the getWidth method of the image img object.
6 Get the height of the image by invoking the getHeight method of the image img object.
7 Get the width of the computer screen by invoking the getWidth method of the dimension ds object. The width is divided by 2 to define the x-axis (mX) at the center of the screen.
8 Get the height of the computer screen by invoking the getHeight method of the dimension ds object.The height is divided by 2 to define the y-axis (mY) at the center of the screen.
9 The translate(mX,mY) method of the Graphics2D class is invoked to move the original coordinate (0,0) to the coordinate (mX,mY) at the center of the screen.
10 Draw the image on the Canvas drawing area so it is ready to show on the program window.
11 The ImageDisplayInterface class has code to show the interface of the Image Display program.
12 Set the title of the program (Image Display).
13 Add the drawing object to the center of the program window.
14 Allow the program to close when the user clicks the close button.
15 Maximize the program window when it opens.
16 Make sure the program window visible.

2 comments:

  1. i have used your code , but image does not display at the center, it tends to occupy whole screen

    ReplyDelete
  2. When I read such advice, programming seems so easy. Just the same when I read the blog https://grapeup.com/blog/read/reactive-service-to-service-communication-with-rsocket-abstraction-over-rsocket-66 I am also of the opinion that I could handle it. However, I prefer to be a user of native applications that run in the cloud.

    ReplyDelete