Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

displaying images at runtime

23 views
Skip to first unread message

steve

unread,
Feb 23, 2009, 9:31:03 AM2/23/09
to
I have a windows GUI application with multiple tabs in it. At program
runtime I want to display recently created images in the fourth jpanel
(4th tab). Not sure how to go about this, have been researching but no
luck yet.

Thanks

John B. Matthews

unread,
Feb 23, 2009, 10:55:05 AM2/23/09
to
In article
<01b08bde-2d3a-4daf...@q9g2000yqc.googlegroups.com>,
steve <Stephen.Sc...@gmail.com> wrote:

Assuming you have an Image, here are two ways: The first simply
overrides paintComponent() to draw the image. The second constructs an
ImageIcon for use in a JLabel. Myriad other ways are described in the
tutorial:

<http://java.sun.com/docs/books/tutorial/uiswing/index.html>

<code>
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ImageTest {

private static final int SIZE = 128;
private static final Font FONT = new
Font("Serif", Font.BOLD, SIZE * 7 / 8);
private static final BufferedImage errorImage = new
BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
static {
Graphics2D g2d = errorImage.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.red.darker());
g2d.setFont(FONT);
String s = "\u2298";
int w2 = g2d.getFontMetrics().stringWidth(s) / 2;
int h2 = g2d.getFontMetrics().getDescent();
g2d.drawString(s, SIZE / 2 - w2 , SIZE / 2 + h2);
g2d.dispose();
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
//@Override
public void run() {
createGUI();
}
});
}

private static void createGUI() {
JFrame f = new JFrame("Frame");
f.setLayout(new GridLayout(1, 0));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon img = new ImageIcon(errorImage);
f.add(new JLabel(img));
f.add(new ImagePanel(errorImage));
f.pack();
f.setVisible(true);
}

private static class ImagePanel extends JPanel {

private Image image;

public ImagePanel(Image image) {
this.image = image;
this.setPreferredSize(new Dimension(
image.getWidth(this), image.getHeight(this)));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(this.image, 0, 0, null);
}
}
}
</code>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

steve

unread,
Feb 23, 2009, 1:54:03 PM2/23/09
to
On Feb 23, 10:55 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <01b08bde-2d3a-4daf-a588-6b17855d5...@q9g2000yqc.googlegroups.com>,

Ok so my next question would be if I have multiple tabs on a
jtabbedpane and when I use the following code

public class ShowImage extends Panel {
BufferedImage image;

public ShowImage() {

}

public ShowImage(String filename){
try {
BufferedReader bf=new BufferedReader(new FileReader
(filename));
File input = new File(filename);
image = ImageIO.read(input);
} catch (IOException ie) {
System.out.println("Error:"+ie.getMessage());
}
}

@Override
public void paint(Graphics g) {
g.drawImage( image, 0, 0, null);
}
}

then in the main program

Panel ipanel1 = new ShowImage("image.jpg");
jPanel4.add(ipanel1);
ipanel1.setSize(1000,1000);
ipanel1.setVisible(true);

what happens is the image panel gets displayed on all the tabs but
once i click the tab it belongs on and then go back to the other tabs
the image panel is not there. What do I need to call after the above
statement to prevent this multiple panels being displayed?

Thanks

John B. Matthews

unread,
Feb 23, 2009, 4:57:04 PM2/23/09
to
In article
<44c587cf-2f1c-43c7...@e6g2000vbe.googlegroups.com>,
steve <Stephen.Sc...@gmail.com> wrote:

[...]


> Ok so my next question would be if I have multiple tabs on a
> jtabbedpane and when I use the following code

As it is incomplete, I am unable to run your code. Please consider
posting an sscce: <http://pscode.org/sscce.html>, as shown below.

> public class ShowImage extends Panel {

I've never tried to put a java.awt.Panel in a javax.swing.JTabbedPane,
but I'm led to believe that Swing and AWT Components don't mix well.

[...]


> what happens is the image panel gets displayed on all the tabs but
> once i click the tab it belongs on and then go back to the other tabs
> the image panel is not there. What do I need to call after the above
> statement to prevent this multiple panels being displayed?

This seems to work as expected.

<code>
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class ImageTest {

private static final int SIZE = 256;


private static final Font FONT = new
Font("Serif", Font.BOLD, SIZE * 7 / 8);

public static void main(String[] args) {


EventQueue.invokeLater(new Runnable() {
//@Override
public void run() {
createGUI();
}
});
}

private static void createGUI() {
JFrame f = new JFrame("Frame");

JTabbedPane tabbedPane = new JTabbedPane();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane.addTab("One", new ImagePanel("1"));
tabbedPane.addTab("Two", new ImagePanel("2"));
tabbedPane.addTab("Three", new ImagePanel("3"));
f.add(tabbedPane);
f.pack();
f.setVisible(true);
}

private static class ImagePanel extends JPanel {

private Image image;
private int w;
private int h;

public ImagePanel(String s) {
this.image = getImage(s);
this.w = image.getWidth(this);
this.h = image.getHeight(this);
this.setPreferredSize(new Dimension(w, h));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

int x = (this.getWidth() - w) / 2;
int y = (this.getHeight() - h) / 2;
g.drawImage(image, x, y, null);
}

private Image getImage(String s) {
BufferedImage bi = new BufferedImage(
SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.blue.darker());
g2d.setFont(FONT);


int w2 = g2d.getFontMetrics().stringWidth(s) / 2;
int h2 = g2d.getFontMetrics().getDescent();

g2d.drawString(s, SIZE / 2 - w2, SIZE / 2 + h2);
g2d.dispose();
return bi;
}
}
}
</code>

[Please trim signature lines when responding.]

Knute Johnson

unread,
Feb 23, 2009, 6:10:33 PM2/23/09
to
steve wrote:
> Ok so my next question would be if I have multiple tabs on a
> jtabbedpane and when I use the following code
>
> public class ShowImage extends Panel {
> BufferedImage image;
>
> public ShowImage() {
>
> }

Can't really see why you need the no argument constructor.

> public ShowImage(String filename){
> try {
> BufferedReader bf=new BufferedReader(new FileReader
> (filename));

The BufferedReader is redundant too.

> File input = new File(filename);
> image = ImageIO.read(input);
> } catch (IOException ie) {
> System.out.println("Error:"+ie.getMessage());
> }
> }
>
> @Override
> public void paint(Graphics g) {
> g.drawImage( image, 0, 0, null);
> }
> }
>
> then in the main program
>
> Panel ipanel1 = new ShowImage("image.jpg");
> jPanel4.add(ipanel1);
> ipanel1.setSize(1000,1000);
> ipanel1.setVisible(true);

I'm sure you don't need to set your ShowImage visible.

>
> what happens is the image panel gets displayed on all the tabs but
> once i click the tab it belongs on and then go back to the other tabs
> the image panel is not there. What do I need to call after the above
> statement to prevent this multiple panels being displayed?
>
> Thanks

It's really hard to tell what's happening in your code when you don't
provide it. Each pane of the JTabbedPane needs a different component
added to it. If you add the component to two different containers, it
ends up in the last one. Using setSize() is probably not the best plan
for your panel.

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

steve

unread,
Feb 25, 2009, 10:48:35 AM2/25/09
to
On Feb 23, 4:57 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <44c587cf-2f1c-43c7-9c79-62fcb6191...@e6g2000vbe.googlegroups.com>,


Ok thanks I understand your code but instead of drawing numbers how
would I "draw" an image from a specified path?

Message has been deleted

John B. Matthews

unread,
Feb 25, 2009, 10:54:39 AM2/25/09
to
In article
<b018d758-bdb5-4c1e...@f24g2000vbf.googlegroups.com>,
steve <Stephen.Sc...@gmail.com> wrote:

[...]

[Please trim signature lines when responding.]

> Ok thanks I understand your code but instead of drawing numbers how

> would I "draw" an image from a specified path?

<http://java.sun.com/docs/books/tutorial/2d/images/index.html>

0 new messages