Prev: My first attempt at java code fails. OK, why please?
Next: 2010 world cup jerseys,paypal payment and free shipping
From: jimmy on 12 Jun 2010 02:08 Hi All, 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. I have written two classes: MyImage does the painting to the screen and MyImageApp for drawing the button, and then calls MyImage when it has returned an image selected by the user. The problem is it doesn't work. I am almost there, but I think I am causing problems with MyImageApp. I need to be able to access the BufferedImage returned by the JFileChooser and pass it as an argument to MyImage for painting. I just can't figure it out though. Can someone show me the errors of my ways? Cheers Jimmy import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.net.URL; @SuppressWarnings("serial") class MyImage extends JPanel{ private BufferedImage bi; public MyImage(BufferedImage imgSrc){ int h = imgSrc.getHeight(); int w = imgSrc.getWidth(); bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(bi, 0, 0, null); } } import javax.swing.*; import java.awt.Component; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; public class MyImageApp extends Component { public MyImageApp(){ } BufferedImage image = null; public void buildUI(){ JButton button1 = new JButton("Open file"); button1.setSize(150, 100); button1.addActionListener((ActionListener) new myevent()); final MyImage panny = new MyImage(image); } public static void main(String[] args){ JFrame myFrame = new JFrame(); myFrame.setTitle("Here's my Frame"); myFrame.setSize(1000, 800); myFrame.setVisible(true); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyImageApp core = new MyImageApp(); core.buildUI(); } }
From: jimmy on 12 Jun 2010 02:30 Oops! I forgot the third class I wrote to handle the getting of the file. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFileChooser; public class myevent implements ActionListener{ BufferedImage image; 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 stored as a buffered image"); } catch (IOException ex) { System.out.println("problem accessing file "+file.getAbsolutePath()); } } else { System.out.println("File access cancelled by user."); } } }
From: Lew on 12 Jun 2010 08:38 On 06/12/2010 02:08 AM, jimmy wrote: > 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. I have written two classes: MyImage Actually, three ... > does the painting to the screen and MyImageApp for drawing the button, > and then calls MyImage when it has returned an image selected by the > user. The problem is it doesn't work. What does "doesn't work" mean? It hangs? It displays some different image? It does nothing apparent? It gives errors? Your request is like asking a doctor to prescribe medicine when all you'll admit is, "I don't feel well". We need the specific symptoms, accurately reported. On the face of it, 'MyImage' refuses to use the image data passed to it. > I am almost there, but I think I am causing problems with MyImageApp. > I need to be able to access the BufferedImage returned by the > JFileChooser and pass it as an argument to MyImage for painting. I > just can't figure it out though. Can someone show me the errors of my > ways? Stop using TAB to indent code for Usenet. Use up to four space characters per indent level. > import ... [omitted for brevity] > @SuppressWarnings("serial") > class MyImage extends JPanel{ > > private BufferedImage bi; > > public MyImage(BufferedImage imgSrc){ > > int h = imgSrc.getHeight(); 'NullPointerException' ("NPE") thrown here. > int w = imgSrc.getWidth(); NPE here, but that the code doesn't get this far. > bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); > } > > > public void paintComponent(Graphics g) { > super.paintComponent(g); > g.drawImage(bi, 0, 0, null); 'bi' will never have data. > } > } > public class MyImageApp extends Component { > BufferedImage image = null; Here you set 'image' to 'null', twice. > public void buildUI(){ > > JButton button1 = new JButton("Open file"); > button1.setSize(150, 100); > button1.addActionListener((ActionListener) new myevent()); By convention, class names should begin with an upper-case character and be in camel case. > final MyImage panny = new MyImage(image); Here you build a 'MyImage' around a 'null' image, which in turn is ignored except to throw 'NullPointerException'. > } > > public static void main(String[] args){ GUI code should ONLY run on the Event Dispatch Thread (EDT). > JFrame myFrame = new JFrame(); > > myFrame.setTitle("Here's my Frame"); > myFrame.setSize(1000, 800); > myFrame.setVisible(true); > myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > > MyImageApp core = new MyImageApp(); > core.buildUI(); > } > } > public class myevent implements ActionListener{ By convention, class names should begin with an upper-case character and be in camel case. > > BufferedImage image; This 'image' is disconnected from all proposed use of it. You never give it to a component for display. It's also peculiarly scoped as an instance variable of the event-handler class. > 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"); Don't use 'System.out.println()' for debugging. Don't even use 'System.err.println()'. This is especially bad for GUI code. Use a logger. > try { > System.out.println("Image loaded to buffer"); > > image = ImageIO.read(fileChooser.getSelectedFile()); Now what? You have the image, but you do nothing with it. > System.out.println("Image stored as a buffered image"); > } > catch (IOException ex) { > System.out.println("problem accessing file > "+file.getAbsolutePath()); > } > } > else { > System.out.println("File access cancelled by user."); > } > } > } -- Lew
From: John B. Matthews on 12 Jun 2010 11:43 In article <c4d87166-e98d-4346-a119-290ddb26a24e(a)c33g2000yqm.googlegroups.com>, jimmy <jimmy.cullen(a)gmail.com> wrote: > 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. I have written two classes: MyImage > does the painting to the screen and MyImageApp for drawing the > button, and then calls MyImage when it has returned an image selected > by the user. The problem is it doesn't work. > > I am almost there, but I think I am causing problems with MyImageApp. > I need to be able to access the BufferedImage returned by the > JFileChooser and pass it as an argument to MyImage for painting. I > just can't figure it out though. Can someone show me the errors of my > ways? In addition to Lew's compelling observations, each of which deserves attention, I would like to address a few design problems. First, your class MyImageApp extends Component, but it doesn't really alter the behavior of Component; it just contains a JFrame holding two JComponents (JPanel and JButton). Second, your button handler needs to work in concert with your image panel, but the button itself may be elsewhere. The Action interface is intended "to separate functionality and state from a component." This makes it east to add the "Open" functionality to a button or menu item. <http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html> Compare this example with your own in light of Lew's comments. As a convenience, ImageOpenAction uses pack() to adjust the parent Window to the image's size: 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 java.util.logging.Level; import java.util.logging.Logger; 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; /** @author John B. Matthews */ public class ImageApp { public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { JFrame f = new JFrame(); f.setTitle("Title"); ImagePanel imagePanel = new ImagePanel(f); f.add(imagePanel, BorderLayout.CENTER); JButton open = new JButton(imagePanel.getAction()); f.add(open, BorderLayout.SOUTH); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); } }); } } class ImagePanel extends JPanel { private final Window parent; private BufferedImage image; private Action action = new ImageOpenAction(); public ImagePanel(JFrame parent) { this.parent = parent; this.setPreferredSize(new Dimension(320, 240)); } @Override public void paintComponent(Graphics g) { g.drawImage(image, 0, 0, null); } public Action getAction() { return action; } private class ImageOpenAction extends AbstractAction { public ImageOpenAction() { super("Open"); } @Override public void actionPerformed(ActionEvent e) { 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()); ImagePanel.this.setPreferredSize(new Dimension( image.getWidth(), image.getHeight())); parent.pack(); } catch (IOException ex) { Logger.getLogger(ImagePanel.class.getName()) .log(Level.SEVERE, null, ex); } } } } } -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
From: John B. Matthews on 12 Jun 2010 17:37
In article <nospam-B6EDD6.11432912062010(a)news.aioe.org>, "John B. Matthews" <nospam(a)nospam.invalid> wrote: Erratum: > public ImagePanel(JFrame parent) { public ImagePanel(Window parent) { -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews> |