Professional Documents
Culture Documents
Dogstopper
Java
Image Manipulation Part 1
~ Dogstopper
Image manipulation is something that is very necessary
to do in Java for various reasons, whether that reason be
so that you can have images on icons for Jbuttons or
Jlabels, or have images for games. Either way, they both
start the same way: you have to load them. Now, I hope
that in today's lesson, I can also demonstrate the
importance of packages when using resources (not solely
for images). With correct packaging, one does not have to
worry about JAR files behaving differently than just
standard class files do.
Loading ImageIcons
1 of 7
package com.thousandcodes.imagespart1;
General Discussion
Caffeine Lounge
Corner Cubicle
Student Campus
Software Development
Java Tutorials
Industry News
Introduce Yourself
View-Presenter in 5
Nightmare.In.Code
minutes.
Book Review: Murach's
Programming Help
C and C++
Phobos - A JavaFX
Games Engine: Part 2 -
public Tutorial() {
VB.NET
Java
C#
ASP.NET
.NET Framework
VB6
PHP
Started
Ruby
Phobos - A JavaFX
Python
ColdFusion
Databases
DP
Other Languages
Swing to JavaFX
Game Development
Swing, Top-Down 2
Mobile Development
52 Weeks Of Code
GridBagLayout)
Basic Java: Types,
Web Development
Variables, Operators
Web Development
Tutorials...
JavaScript
Reference Sheets
Graphic Design
Flash & ActionScript
Blogging
SEO & Advertising
Web Servers & Hosting
33
34
35
FAQ36
37
38
39
40
41
42
43
Site Check
About Us
Code Snippets
Now, the image is called i2.png and is located in the
package com.thousandcodes.imagespart1.images. With
this setup, the image can be accessed assuming it is all
packaged together into the same JAR file. The URL is
essentially a file location that makes file access easy
because one doesn't have to worry about forward or
backslashes based on the operating system. With a URL,
it is always a forward slash.
Then it was a really simple matter of making a new JLabel
with the image, and adding the JLabel to the JFrame.
C++ Snippets
Java Snippets
Visual Basic Snippets
C# Snippets
VB.NET Snippets
PHP Snippets
SQL Snippets
10
11
12
13
14
15
16
17
18
Python Snippets
Ruby Snippets
ColdFusion Snippets
Assembly Snippets
Functional
Programming Snippets
Perl Snippets
HTML/CSS Snippets
Javascript Snippets
Flash/ActionScript
Snippets
Other Languages
BufferedImage img;
public PicturePanel() {
// Load the image
img = getImage("images/space-2.jpg");
Snippets
DIC Chatroom
Join our IRC Chat
is.
2 of 7
C Snippets
this.setPreferredSize(new
Dimension(img.getWidth(), img.getHeight()));
}
public void paintComponent(Graphics g) {
// Draw the image on the panel
g.drawImage(img, 0,0,null);
}
private BufferedImage getImage(String
filename) {
// This time, you can use an
InputStream to load
20
try {
// Grab the InputStream for the
21
image.
22
InputStream in =
getClass().getResourceAsStream(filename);
23
24
// Then read it in.
25
return ImageIO.read(in);
26
} catch (IOException e) {
27
System.out.println("The image was
not loaded.");
28
System.exit(1);
29
}
30
return null;
31
}
32 }
19
package com.thousandcodes.imagespart1;
import
import
import
import
import
java.awt.Dimension;
java.awt.Graphics;
java.awt.image.BufferedImage;
java.io.IOException;
java.io.InputStream;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Tutorial extends JFrame {
public Tutorial() {
add(new PicturePanel());
// Standard JFrame things to do.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
private class PicturePanel extends JPanel
{
26
27
28
29
30
31
BufferedImage img;
public PicturePanel() {
// Load the image
img = getImage("images/space2.jpg");
3 of 7
32
33
34
35
36
37
this.setPreferredSize(new
Dimension(img.getWidth(), img.getHeight()));
}
public void paintComponent(Graphics g)
{
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
08
09
10
11
4 of 7
this.setPreferredSize(new
Dimension(img.getWidth(), img.getHeight()));
}
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
5 of 7
Dogstopper
NeoTifa
6 of 7
7 of 7