From: jimmy on
Reply to Lew:

Many thanks for your comments on my code. Yes my statement "it doesn't
work" was vague and not informative, apologies. As you can tell I am a
Java novice and could not diagnose the faults myself. From your
comments it is clear that my code contained many problems. I will read
more about EDT and logger (I have left my println statements in for
the time being as I am more concerned with getting the images to
display at the moment).

reply to John B. Matthews:

Many thanks for your comments also. I have read and digested your
code, most of which I can follow, but some is still too advanced for
me. I have read the Action page you posted the link to and have tried
to implement an action in my code.



Heavily influenced by John B. Matthews' code, I have rewritten much of
my code, however it still does not display an image. I cannot identify
the source of the problem, but based on the println statements (which
I will remove in place of a logger) I think that myAction (the action
associated with the button) is not being returned.

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

Many thanks,

Jimmy

import java.awt.event.ActionEvent;


class MyAction extends AbstractAction{

BufferedImage image;

public MyAction(){
super("Open");
}

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());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}

import javax.imageio.ImageIO;

class MyImage extends JPanel{

private final Window parent;
BufferedImage img;
MyAction myAction = new MyAction();

public MyImage(JFrame parent){
this.parent = parent;
}

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

public Action MyAction(){
return myAction;
}
}

import java.awt.event.ActionEvent;


class MyAction extends AbstractAction{

BufferedImage image;

public MyAction(){
super("Open");
}

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());
System.out.println("Image about to be loaded to buffer");
try {
System.out.println("Image loaded to buffer");
image = ImageIO.read(fileChooser.getSelectedFile());
System.out.println("Image painted");
}
catch (IOException ex) {
System.out.println("problem accessing
file"+file.getAbsolutePath());
}
}
else {
System.out.println("File access cancelled by user.");
}
}
}
From: jimmy on
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();
}
}
From: pillemi on
jimmy <jimmy.cullen(a)gmail.com> writes:

<snip>

One common way of debugging is reading the eror stack; and
fixing the lines with errors iteratively.

For example..

If you get a null pointer exception on line 10; you go to line 10 and
work out which object is null and resolve that.

You can ten rerun your program until you get no errors.

You can then fix your functional errors.

After that you can refactor etc...

--
ilAn
From: Lew on
On 06/14/2010 04:22 AM, jimmy wrote:
> Reply to Lew:
>
> Many thanks for your comments on my code. Yes my statement "it doesn't
> work" was vague and not informative, apologies. As you can tell I am a
> Java novice and could not diagnose the faults myself. From your
> comments it is clear that my code contained many problems. I will read
> more about EDT and logger (I have left my println statements in for
> the time being as I am more concerned with getting the images to
> display at the moment).
>
> reply to John B. Matthews:
>
> Many thanks for your comments also. I have read and digested your
> code, most of which I can follow, but some is still too advanced for
> me. I have read the Action page you posted the link to and have tried
> to implement an action in my code.
>
>
>
> Heavily influenced by John B. Matthews' code, I have rewritten much of
> my code, however it still does not display an image. I cannot identify
> the source of the problem, but based on the println statements (which
> I will remove in place of a logger) I think that myAction (the action
> associated with the button) is not being returned.
>
> I have attached my new code. I would be most grateful if someone could
> identify the problem and indicate how I could resolve it.
....
> import javax.imageio.ImageIO;

This is an unused import. Didn't your IDE warn you? Does your IDE support an
operation to clean up imports (Ctrl-Shift-O in Eclipse, Ctrl-Shift-I in NetBeans)?

> class MyImage extends JPanel{
>
> private final Window parent;
> BufferedImage img;
> MyAction myAction = new MyAction();
>
> public MyImage(JFrame parent){
> this.parent = parent;
> }
>
> public void paintComponent(Graphics g) {
> super.paintComponent(g);
> g.drawImage(img, 0, 0, null);

'img' will always be 'null', since you never assign it a value.

> }
>
> public Action MyAction(){
> return myAction;
> }
> }
>
> import java.awt.event.ActionEvent;
>
>
> class MyAction extends AbstractAction{
>
> BufferedImage image;
>
> public MyAction(){
> super("Open");
> }
>
> 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());
> System.out.println("Image about to be loaded to buffer");
> try {
> System.out.println("Image loaded to buffer");
> image = ImageIO.read(fileChooser.getSelectedFile());

You create an image then throw it away. That is, if this line were to
compile, which it won't unless you import 'ImageIO'.

> System.out.println("Image painted");

What in the world makes you think the image was painted here? All you did was
assign a variable. You never passed it to anything to paint anything.

> }
> catch (IOException ex) {
> System.out.println("problem accessing
> file"+file.getAbsolutePath());
> }
> }
> else {
> System.out.println("File access cancelled by user.");
> }
> }
> }

--
Lew
From: Roedy Green on
On Fri, 11 Jun 2010 23:08:17 -0700 (PDT), jimmy
<jimmy.cullen(a)gmail.com> wrote, quoted or indirectly quoted someone
who said :

>
>I am having big problems setting out my code and need help organising
>it. I want to write a GUI that displays jpg images that are selected
>by the user from a file chooser

You might use one of classes I have written for that purpose.

ImageViewer is part of the common11 bundle. See
http://mindprod.com/products1.html#COMMON11
--
Roedy Green Canadian Mind Products
http://mindprod.com

There is no harm in being sometimes wrong especially if one is promptly found out.
~ John Maynard Keynes (born: 1883-06-05 died: 1946-04-21 at age: 62)