• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Image Viewer in Java

amhynaya

Marketplace Revenue Analyst
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
115
Likes
99
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Introduction

In this tutorial we will learn how to display picture in Java Application. There is difference between how a picture displayed in application and applet.
For application we require drawImage() method which draws the image to specific component and not the entire frame.
Implementation

Step 1: Creating the components


We require a label, text field where user will enter name or address of the image and a button which when clicked will display the image. A panel to keep all these components.

  1. JLabel

    label;
  2. JTextField

    name;
  3. JButton

    display;

  4. JPanel

    upperPanel;


Step 2:Defining Constructor


In the Constructor of the class, we will create the label , text field and button object and add those object to the Panel. The layout for the main Frame is Border Layout, we will add upperPanel in the NORTH of the frame and other panel for displaying the image in the center of the panel as follows.
  1. ImageDisplay(

    )
  2. {
  3. super

    (

    "Image Demo"

    )

    ;
  4. label=

    new

    JLabel

    (

    "Enter name of the Image you want to open"

    )

    ;
  5. name=

    new

    JTextField

    (

    15

    )

    ;
  6. display=

    new

    JButton

    (

    "Display Image"

    )

    ;

  7. DrawPanel panel=

    new

    DrawPanel(

    )

    ;
  8. display.addActionListener

    (

    panel)

    ;
  9. upperPanel=

    new

    JPanel

    (

    )

    ;
  10. setLayout(

    new

    BorderLayout

    (

    )

    )

    ;
  11. upperPanel.add

    (

    label)

    ;
  12. upperPanel.add

    (

    name)

    ;
  13. upperPanel.add

    (

    display)

    ;


  14. add(

    upperPanel,BorderLayout

    .NORTH

    )

    ;
  15. add(

    panel,BorderLayout

    .CENTER

    )

    ;
  16. }


Step 3: Drawing the Image


For drawing the image, we will create a separate class inside the main class. This class will be a panel and action listener for the button will be in this class.
Data enter by user in the text field is stored in String object and Image is created using that object. Image is created by ImageIcon's getImage() method.
  1. public

    void

    actionPerformed(

    ActionEvent

    e)
  2. {
  3. String

    text=

    name.getText

    (

    )

    .trim

    (

    )

    ;
  4. image=

    new

    ImageIcon

    (

    text)

    .getImage

    (

    )

    ;
  5. repaint(

    )

    ;

  6. }

paintComponent() method is used to repaint the panel every time user clicks the display button. For clearing the background, Current color is set as white and then fillRect() method is used to clear the Area. drawImage() method draws the Image from pixel location specified. Image's top left corner will be placed on that pixel location. this keyword specifies that image should be drawn to the same panel.
  1. public

    void

    paintComponent(

    Graphics

    g)
  2. {
  3. setBackground(

    Color

    .WHITE

    )

    ;
  4. g.setColor

    (

    Color

    .WHITE

    )

    ;
  5. g.fillRect

    (

    10

    ,10

    ,800

    ,600

    )

    ;
  6. g.drawImage

    (

    image,10

    ,10

    ,this

    )

    ;
  7. }


Step 4: Defining main() method


main() method is defined which set the different properties of the frame and execution begins from here.
  1. public

    static

    void

    main(

    String

    [

    ]

    args)

    {


  2. ImageDisplay img=

    new

    ImageDisplay(

    )

    ;
  3. img.setSize

    (

    800

    ,600

    )

    ;
  4. img.setVisible

    (

    true

    )

    ;
  5. img.setDefaultCloseOperation

    (

    EXIT_ON_CLOSE)

    ;
  6. img.setResizable

    (

    false

    )

    ;
  7. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top