Results 1 to 20 of 45
Thread: Browsing .png files?
- 06-22-2011, 10:04 PM #1
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Displaying an Image problem
Hi, i'm trying to make a little applet where you can load a image (via File Browsing), edit it to your liking (with a preview) and save it again. But for some reason the preview is not working. Here's most of my code (file browsing and preview image):
Java Code:package DesaturateApplet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageBrowser extends JFrame{ //====================================================== fields JTextField _fileNameTF = new JTextField(15); JTextField _wordCountTF = new JTextField(4); JFileChooser _fileChooser = new JFileChooser(); //================================================= constructor ImageBrowser() { String newline = System.getProperty("line.separator"); _fileNameTF.setEditable(false); _wordCountTF.setEditable(false); JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(new JLabel("File Name:" + newline)); content.add(_fileNameTF); content.add(new JLabel(newline + "Preview:")); JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open..."); openItem.addActionListener(new OpenAction()); menubar.add(fileMenu); fileMenu.add(openItem); this.setJMenuBar(menubar); this.setContentPane(content); this.setTitle("Select a Image File"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); // Layout components. this.setLocationRelativeTo(null); // Center window. } public static String fileimage; ///////////////////////////////////////////////////// OpenAction class OpenAction implements ActionListener { public void actionPerformed(ActionEvent ae) { int retval = _fileChooser.showOpenDialog(ImageBrowser.this); if (retval == JFileChooser.APPROVE_OPTION) { File file = _fileChooser.getSelectedFile(); _fileNameTF.setText(file.getName()); File filetemp = _fileChooser.getSelectedFile(); String fileimage = filetemp.getName(); } } } //========================================================= main static String path = fileimage; public static void main(String[] args) throws IOException{ JFrame window = new ImageBrowser(); window.setVisible(true); BufferedImage image = ImageIO.read(new File(path)); ImageBrowser test = new ImageBrowser(image); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(new JScrollPane(test)); window.setSize(400,400); window.setLocation(200,200); window.setVisible(true); } private static void showIcon(BufferedImage image) { ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "icon", -1); } BufferedImage image; Dimension size = new Dimension(); public ImageBrowser(BufferedImage image) { this.image = image; size.setSize(image.getWidth(), image.getHeight()); } static String imgfile = fileimage; protected void paintComponent(Graphics g) { int x = (getWidth() - size.width)/2; int y = (getHeight() - size.height)/2; g.drawImage(image, x, y, this); } public Dimension getPreferredSize() { return size; } }Last edited by petur170; 06-23-2011 at 05:31 AM. Reason: change title/ updated code/ make thread better
- 06-22-2011, 10:12 PM #2
Can you define what you mean by "browsing a png file"?
In what environment are you operating?press a button, and the browser will open up, you click a .png, edit it,
Where is the button? How is the HTML fed to the browser display a png image. How is that image linked to an editor?
- 06-22-2011, 10:15 PM #3
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
-removed- _ _
- 06-22-2011, 10:16 PM #4
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
I use windows 7.
By browsing for .png files i mean file browsing like this:
And i think you know what .png files are :)
- 06-22-2011, 10:18 PM #5
Sorry I'm C# illiterate.
Yes I'm familiar with png files.
Not sure what "browsing" an image means? Is it showing the pixel values at an x,y location?
And how a browser comes into it? Can you explain that?
- 06-22-2011, 10:21 PM #6
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
I meant to say file browsing >.< sorry
- 06-22-2011, 10:25 PM #7
There is the JFileChooser class for selecting a file.
- 06-22-2011, 11:03 PM #8
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Okay so i got his code
[spoiler]
[/spoiler]Java Code:package DesaturateApplet; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; public class ImageBrowser extends JFrame { JTextField _fileNameTF = new JTextField(15); JTextField _wordCountTF = new JTextField(4); JFileChooser _fileChooser = new JFileChooser(); //ImageDisplay png = new ImageDisplay(_fileNameTF); ImageBrowser() { _fileNameTF.setEditable(false); _wordCountTF.setEditable(false); JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(new JLabel("File Name:")); content.add(_fileNameTF); content.add(new JLabel("Preview:")); content.add(ImageDisplay(_fileNameTF)); JMenuBar menubar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem openItem = new JMenuItem("Open..."); openItem.addActionListener(new OpenAction()); menubar.add(fileMenu); fileMenu.add(openItem); this.setJMenuBar(menubar); this.setContentPane(content); this.setTitle("Select a Image File"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.pack(); this.setLocationRelativeTo(null); } class OpenAction implements ActionListener { public void actionPerformed(ActionEvent ae) { int retval = _fileChooser.showOpenDialog(ImageBrowser.this); if (retval == JFileChooser.APPROVE_OPTION) { File file = _fileChooser.getSelectedFile(); _fileNameTF.setText(file.getName()); } } } //========================================================= main public static void main(String[] args) { JFrame window = new ImageBrowser(); window.setVisible(true); } }
And it works, but i cant get it to display the image. Here's the small part that's supposed to display an image:
ThanksJava Code:JPanel content = new JPanel(); content.setLayout(new FlowLayout()); content.add(new JLabel("File Name:")); content.add(_fileNameTF); content.add(new JLabel("Preview:")); content.add(ImageDisplay(_fileNameTF));Last edited by petur170; 06-22-2011 at 11:19 PM.
- 06-22-2011, 11:11 PM #9
What is ImageDisplay? It appears to be a method.
- 06-22-2011, 11:16 PM #10
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
:)Java Code:import javax.swing.*; import java.awt.*; import javax.imageio.*; import java.io.*; public class ImageDisplay extends JPanel { private String path; private Image img; public ImageDisplay(String path) throws IOException { this.path = path; img = ImageIO.read(new File(path)); } public void paint(Graphics g) { if( img != null) g.drawImage(img,0,0, this); } }
- 06-22-2011, 11:19 PM #11
The code you posted does NOT compile.it works, but
What do you mean by "it works"?
There are lots of examples of how to display images on the forum. For example Search for ImageIO.read for some.
- 06-22-2011, 11:21 PM #12
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Well, i need it to display the image i loaded from the file browser
- 06-22-2011, 11:29 PM #13
The code as posted does not compile! It won't execute and display anything if it doesn't first compile.
- 06-22-2011, 11:32 PM #14
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
I'm using eclipse (you can test in eclipse)
- 06-22-2011, 11:34 PM #15
That's fine. What problem are you having? Do you get errors or what happens with your code.
The code you post will NOT compile for me!!!
- 06-22-2011, 11:35 PM #16
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Here's the error i get:
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method ImageDisplay(JTextField) is undefined for the type ImageBrowser at DesaturateApplet.ImageBrowser.<init>(ImageBrowser.java:33) at DesaturateApplet.ImageBrowser.main(ImageBrowser.java:96)
- 06-22-2011, 11:39 PM #17
The syntax for that statement, as I mentioned in post #9, is how you call a method.The method ImageDisplay(JTextField) is undefined for the type ImageBrowser
What is the statement where the error occured supposed to do?
How long have you been trying to write a Java program? Is this your first one?
- 06-22-2011, 11:44 PM #18
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
This is my second, My first was a simple bank account thing.
The line with the error is line 6 on the bottom code in post #10.
It is supposed to display an image right below the text "Preview:"
- 06-22-2011, 11:54 PM #19
Look at the syntax of the statement. It is a method call, but there is no method with that name.
What did you expect that statement to do?
- 06-22-2011, 11:58 PM #20
Member
- Join Date
- May 2011
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Java webstart having problem with multiple jar files and resource files
By rjalori in forum AWT / SwingReplies: 2Last Post: 06-11-2011, 01:54 PM -
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
Offline browsing issues
By stthepan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-16-2010, 07:50 AM -
web browsing
By mihir786 in forum Advanced JavaReplies: 2Last Post: 11-10-2009, 03:43 AM -
BROWSING and ATTACHING MORE THAN ONE FILES IN JAVA
By nehaa in forum New To JavaReplies: 3Last Post: 01-23-2009, 12:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks