amhynaya
Marketplace Revenue Analyst
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.
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.
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.
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.
Step 4: Defining main() method
main() method is defined which set the different properties of the frame and execution begins from here.
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
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.
- JLabel
label;
- JTextField
name;
- JButton
display;
- 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.
- ImageDisplay(
)
- {
- super
(
"Image Demo"
)
;
- label=
new
JLabel
(
"Enter name of the Image you want to open"
)
;
- name=
new
JTextField
(
15
)
;
- display=
new
JButton
(
"Display Image"
)
;
- DrawPanel panel=
new
DrawPanel(
)
;
- display.addActionListener
(
panel)
;
- upperPanel=
new
JPanel
(
)
;
- setLayout(
new
BorderLayout
(
)
)
;
- upperPanel.add
(
label)
;
- upperPanel.add
(
name)
;
- upperPanel.add
(
display)
;
- add(
upperPanel,BorderLayout
.NORTH
)
;
- add(
panel,BorderLayout
.CENTER
)
;
- }
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.
- public
void
actionPerformed(
ActionEvent
e)
- {
- String
text=
name.getText
(
)
.trim
(
)
;
- image=
new
ImageIcon
(
text)
.getImage
(
)
;
- repaint(
)
;
- }
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.
- public
void
paintComponent(
Graphics
g)
- {
- setBackground(
Color
.WHITE
)
;
- g.setColor
(
Color
.WHITE
)
;
- g.fillRect
(
10
,10
,800
,600
)
;
- g.drawImage
(
image,10
,10
,this
)
;
- }
Step 4: Defining main() method
main() method is defined which set the different properties of the frame and execution begins from here.
- public
static
void
main(
String
[
]
args)
{
- ImageDisplay img=
new
ImageDisplay(
)
;
- img.setSize
(
800
,600
)
;
- img.setVisible
(
true
)
;
- img.setDefaultCloseOperation
(
EXIT_ON_CLOSE)
;
- img.setResizable
(
false
)
;
- }
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.