From: Knute Johnson on
On 6/14/2010 1:36 AM, jimmy wrote:
> Oops! I'm really not doing well pasting code - I posted two copies of
> the same class previously. The missing class is:
>
>
>
> import javax.swing.*;
>
> import java.awt.BorderLayout;
> import java.awt.Component;
> import java.awt.event.ActionListener;
>
> public class MyImageApp extends Component{
>
> public static void main(String[] args){
>
> JFrame myFrame = new JFrame();
>
> myFrame.setTitle("Here's my Frame");
> myFrame.setSize(200, 200);
> myFrame.setVisible(true);
> myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> myFrame.pack();
>
> Action myAction = new MyAction();
>
> JButton myButton = new JButton(myAction);
>
> myFrame.add(myButton, BorderLayout.SOUTH);
>
> MyImage core = new MyImage(myFrame);
> myFrame.add(core, BorderLayout.CENTER);
> core.MyAction();
> }
> }

This is too painful to watch anymore.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test extends JPanel {
private BufferedImage image;

public test() {
setPreferredSize(new Dimension(400,300));
}

public void setImage(BufferedImage bi) {
image = bi;
setPreferredSize(new Dimension(bi.getWidth(),bi.getHeight()));
revalidate(); // tells the JScrollPane the size has changed
repaint(); // redraws the new image
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image,0,0,null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final test t = new test();
final JFileChooser chooser = new JFileChooser();

JMenuBar mb = new JMenuBar();
f.setJMenuBar(mb);
JMenu file = new JMenu("File");
mb.add(file);
JMenuItem mi = new JMenuItem("Open");
file.add(mi);
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (chooser.showOpenDialog(f) ==
JFileChooser.APPROVE_OPTION) {
try {
BufferedImage bi = ImageIO.read(
chooser.getSelectedFile());
if (bi != null)
t.setImage(bi);
else
JOptionPane.showMessageDialog(f,
"File is not an image!");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(f,
"Error Reading File!");
}
}
}
});

f.add(new JScrollPane(t),BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/knute2010/

From: John B. Matthews on
In article
<8fe03a4a-cc5d-403d-baec-0e31d35ce3a0(a)q12g2000yqj.googlegroups.com>,
jimmy <jimmy.cullen(a)gmail.com> wrote:

> I have attached my new code. I would be most grateful if someone could
> identify the problem and indicate how I could resolve it.

Again, Lew is more assiduous than I, and ilAn's advice will serve you
better in the long run; but I will make a few observations:

0) Don't invoke pack() until you've added the components.

1) Again, extend Component? I don't see a reason.

2) Again, Swing components should be constructed in the EDT; I've
implemented Runnable as another example of doing so.

3) Invoking super.paintComponent(g) is unnecessary when you paint the
entire content.

4) If you make MyAction a separate class, its constructor needs to know
who gets the selected image. Similarly, MyImage needs a method,
setImgage(), to set the new image for later painting.

5) As an example, I've added the same action to a menu bar.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyImageApp implements Runnable {

public static void main(String[] args) {
EventQueue.invokeLater(new MyImageApp());
}

@Override
public void run() {
JFrame myFrame = new JFrame();
myFrame.setTitle("Here's my Frame");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyImage core = new MyImage(myFrame);
Action myAction = new MyAction(core);
JButton myButton = new JButton(myAction);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(myAction));
menuBar.add(menu);
myFrame.setJMenuBar(menuBar);
myFrame.add(core, BorderLayout.CENTER);
myFrame.add(myButton, BorderLayout.SOUTH);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}

class MyAction extends AbstractAction {

MyImage panel;

public MyAction(MyImage panel) {
super("Open");
this.panel = panel;
}

@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
System.out.println("Image selected: " + file.getPath());
try {
BufferedImage image =
ImageIO.read(fileChooser.getSelectedFile());
panel.setImgage(image);
System.out.println("Image loaded to buffer.");
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
System.out.println("File access cancelled by user.");
}
}
}

class MyImage extends JPanel {

private final Window parent;
BufferedImage image;

public MyImage(JFrame parent) {
this.parent = parent;
this.setPreferredSize(new Dimension(200, 200));
}

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

public void setImgage(BufferedImage image) {
this.image = image;
setPreferredSize(new Dimension(
image.getWidth(), image.getHeight()));
parent.pack();
parent.setLocationRelativeTo(null);
}
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
From: jimmy on
I never dreamed my post would provoke such a response! Thanks all for
helping me in my image display attempts.

ilAn - thanks for the tip about the error stack - its something I'll
look into.

Lew - thanks for pointing out two serious problems. In my println
statement I wrote that the image had been printed, but didn't really
believe that - it was more just a marker to see where execution had
reached, but I agree again very misleading.

Roedy Green - thanks for the link to your classes, I will investigate
these. I have been reading some other helpful pages on your website
that I found useful also, thanks. http://mindprod.com/jgloss/image.html

Knute Johnson - LOL. I know it must be disparaging to watch someone
fumble around when the answer is obvious to you, but this is the best
way for me to learn. Thanks for your solution, I will read and digest
your techniques.

John B. Matthews - thanks once again for your help. I find it
particularly useful that you have bullet pointed major failings and
modified my code (which is largely based on the code you wrote!), as I
am happy with it's style and flow. I will read and digest your
solution in detail.


Whilst there was all of the activity on this post I was busily
fumbling around with code in NetBeans. My ultimate intention is to
create a GUI which displays images for viewing. There will be many
widgets on the GUI so I decided to use NetBeans, for ease of layout.
Using a button push event and a class which extends JPanel, I have
been able to achieve my desired effect of displaying an image in a
JPanel in the GUI. However, not having seen this style of code before
(calling the paintComponent() method) in any of the many examples on
the net, I was wondering if there is a particular reason why it is not
used? Below is the relevant snippet from the code ((button push event
and DrawImage class).

Thanks once again, I'm going to read in detail the solutions that have
been posted in detail now.

Cheers,

Jimmy



BufferedImage image;
String filename;
DrawImage pic = null;

private void
openFileButtonActionPerformed(java.awt.event.ActionEvent evt)
{
JFileChooser chooser = new JFileChooser(
new File(System.getProperty("user.dir")));
int returnVal = chooser.showOpenDialog(chooser);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
image = ImageIO.read(chooser.getSelectedFile());
System.out.println("The file selected is: " +
file.getPath());

//mainImagePanel is the target JPanel for the
image
Graphics g=mainImagePanel.getGraphics();

pic = new DrawImage();
pic.setImage(image);
System.out.println("got to here");

pic.paintComponent(g);

filename = file.getPath();
jLabel1.setText(filename);

} catch (IOException ex) {
System.out.println("There was a problem opening
the selected file: " + ex);
}
}
}

class DrawImage extends JPanel{

public DrawImage() {
}

public void setImage(BufferedImage image){
this.img = image;
}

BufferedImage img;
@Override
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
System.out.println("Now I'm down here");
if(img != null)
{
g2d.scale(0.2, 0.2);
g2d.drawImage(image, 0, 0, null);
}
}
}
From: John B. Matthews on
In article
<8795dede-bb6a-48d3-8020-f65b31e4ced2(a)d8g2000yqf.googlegroups.com>,
jimmy <jimmy.cullen(a)gmail.com> wrote:

[..]
> John B. Matthews - thanks once again for your help. I find it
> particularly useful that you have bullet pointed major failings and
> modified my code (which is largely based on the code you wrote!), as I
> am happy with it's style and flow. I will read and digest your
> solution in detail.

Thank you for an excellent summary. Knute Johnson's use of a scroll pane
is more flexible than my window resizing approach. I found it
instructive to alter my example accordingly.

[...]
> g2d.scale(0.2, 0.2);
> g2d.drawImage(image, 0, 0, null);

As an aside, overloads of dawImage() that specify a width and height
will scale, too:

<http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html>

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