Results 1 to 4 of 4
- 06-04-2011, 07:37 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
There is a problem. The image does'nt display on the panel2
Is there anything wrong with the openFile or is there something else missing?
because when i load the image using JFileChooser it gets loaded (the frame name changes to image name) but its does not display. is there any problem with the splitpane??
here is the code:
Java Code:import javax.swing.*; import java.awt.*; import javax.swing.border.BevelBorder; import javax.swing.border.EtchedBorder; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; import java.io.*; import java.awt.image.*; import java.util.Iterator; import java.awt.geom.*; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; import javax.imageio.stream.ImageOutputStream; import javax.swing.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.border.*; import java.awt.color.*; public class neil extends JFrame implements ActionListener { BufferedImage image,img; public JMenuBar menuBar; JMenuItem grey,open; JMenu file,edit; JPanel panel1,panel2; JButton negativeB,embossB,brightB,sharpB,greyB,zoominB,zoomoutB,editT,openT,saveT,oriT,helpT; JLabel jb; neil() { try { JFrame frame = new JFrame(); setTitle("FINAL LAYOUT"); // setUndecorated(true); //getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel1=new JPanel(); panel2=new JPanel(); JLabel j1 = new JLabel("Area 1"); JLabel j2 = new JLabel("Area 2"); //panel1.add(j1); panel2.add(j2); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, panel1, panel2); panel1.setLayout(new GridLayout(8,2)); //panel1.setLayout(new BorderLayout()); //splitPane.setOneTouchExpandable(true); splitPane.setDividerSize(2); splitPane.setDividerLocation(165); // splitPane.getMaximumDividerLocation(); getContentPane().add(splitPane); //JLabel jb=new JLabel(" to checck snsn snsna nsns nsnnnnnnn nssnshshs uohoh dhbgfdjgsgjgfgdfgsdgfsdgsgfsdfgsd weather //scccroll is working"); splitPane.setPreferredSize(new Dimension(100,300)); //JLabel jb=new JLabel ("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"); JScrollPane jScrollPane = new JScrollPane(); jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); splitPane.setRightComponent(jScrollPane); // splitPane.setResizeWeight(50.525); // splitPane.setMaximumSize(200,100); //setContentPane(panel); setSize(700,700); setVisible(true); //buttons in first panel brightB = new JButton(new ImageIcon("C://Bright.gif")); panel1.add(brightB); brightB.setVisible(false); brightB.addActionListener(this); brightB.setToolTipText("Bright"); embossB= new JButton(new ImageIcon("C://Emboss.gif")); panel1.add(embossB); embossB.setVisible(false); embossB.addActionListener(this); embossB.setToolTipText("Emboss"); sharpB = new JButton(new ImageIcon("C://Sharp.gif")); panel1.add(sharpB); sharpB.setVisible(false); sharpB.addActionListener(this); sharpB.setToolTipText("Sharp"); greyB = new JButton(new ImageIcon("C://grey.png")); panel1.add(greyB); greyB.addActionListener(this); greyB.setVisible(false); greyB.setToolTipText("Grey"); zoominB = new JButton(new ImageIcon("C://zoomin.png")); panel1.add(zoominB); zoominB.setVisible(false); zoominB.addActionListener(this); zoominB.setToolTipText("Zoomin"); zoomoutB= new JButton(new ImageIcon("C://zoomout.png")); panel1.add(zoomoutB); zoomoutB.setVisible(false); zoomoutB.addActionListener(this); zoomoutB.setToolTipText("Zoomout"); negativeB = new JButton(new ImageIcon("C://Negative.gif")); panel1.add(negativeB); negativeB.setVisible(false); negativeB.addActionListener(this); negativeB.setToolTipText("Negative"); /*greyB = new JButton(new ImageIcon("C://Gray.gif")); panel1.add(greyB); brightB = new JButton(new ImageIcon("C://Bright.gif")); panel1.add(brightB); embossB= new JButton(new ImageIcon("C://Emboss.gif")); panel1.add(embossB); sharpB = new JButton(new ImageIcon("C://Sharp.gif")); panel1.add(sharpB); greyB = new JButton(new ImageIcon("C://Gray.gif")); panel1.add(greyB); brightB = new JButton(new ImageIcon("C://Bright.gif")); panel1.add(brightB); embossB= new JButton(new ImageIcon("C://Emboss.gif")); panel1.add(embossB); sharpB = new JButton(new ImageIcon("C://Sharp.gif")); panel1.add(sharpB); greyB = new JButton(new ImageIcon("C://Gray.gif")); panel1.add(greyB); */ //ho gaya :) menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); file = new JMenu("File"); open = new JMenuItem("open"); file.add(open); open.addActionListener(this); edit = new JMenu("Edit"); grey = new JMenuItem("Greyscale"); edit.add(grey); menuBar.add(file); menuBar.add(edit); setJMenuBar(menuBar); //TOOLBAR buttons JToolBar toolbar = new JToolBar("Toolbar", JToolBar.HORIZONTAL); openT = new JButton(new ImageIcon("C://open.png")); toolbar.add(openT); openT.addActionListener(this); openT.setToolTipText("Open"); saveT = new JButton(new ImageIcon("C://save.png")); toolbar.add(saveT); saveT.addActionListener(this); saveT.setToolTipText("Save"); helpT = new JButton(new ImageIcon("C://help.png")); toolbar.add(helpT); helpT.addActionListener(this); helpT.setToolTipText("Help"); editT= new JButton(new ImageIcon("C://edit.png")); toolbar.add(editT); editT.addActionListener(this); editT.setToolTipText("Edit"); oriT= new JButton(new ImageIcon("C://Original.gif")); toolbar.add(oriT); oriT.addActionListener(this); oriT.setToolTipText("Original"); getContentPane().add(toolbar,BorderLayout.NORTH); //done } catch(Exception e) { /*System.out.println("error:"+e.getMessage()); } setContentPane(new JPanel() { private static final long serialVersionUID = 1L; public void paint(Graphics g) { g.drawImage(image,0,0,this); } });*/ } /*setLeftComponent setRightComponent setTopComponent setBottomComponent*/ } public void actionPerformed(ActionEvent e) { if((e.getSource()==open) || (e.getSource()== openT )) { openFile(); } else if(e.getSource()== editT ) { greyB.setVisible(true); embossB.setVisible(true); sharpB.setVisible(true); zoominB.setVisible(true); zoomoutB.setVisible(true); greyB.setVisible(true); brightB.setVisible(true); negativeB.setVisible(true); } } public void openFile() { try { JFileChooser chooser = new JFileChooser(); //chooser.setCurrentDirectory(new File(".")); int r = chooser.showOpenDialog(this); if (r != JFileChooser.APPROVE_OPTION) return; File f = chooser.getSelectedFile(); String name = f.getName(); image=ImageIO.read(chooser.getSelectedFile()); //box.add(new JLabel(new ImageIcon(image))); //panel.add(new JScrollPane(box)); setSize(image.getWidth(), image.getHeight()); setTitle(name); } catch(Exception e) { } } public static void main(String[] args) { new neil(); } }Last edited by Fubarable; 06-04-2011 at 07:53 PM. Reason: code tags added
- 06-04-2011, 07:54 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,423
- Blog Entries
- 7
- Rep Power
- 17
You're not doing anything with the image you have just loaded; the paint( ... ) method is commented out but that is not the place where you want to paint the image anyway. Read the API documentation for the JFrame class to see how painting is delegated to a chid component of a JFrame.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
-
Moderator edit: code tags added to allow code to retain formatting (such as it is) and be more readable. Please see my signature below to see how to use these yourself.
Regarding your question:
You post a lot of code unrelated to your problem making it more difficult for both you and us to debug the actual problem. You're better off creating a small simple program built specifically to solve the issue at hand, and if unsuccessful, then posting it here (with code tags). Also, do your JPanel's drawing in its paintComponent method, not its paint method, and call super.paintComponent(g) as the first line of this method to help refresh the JPanel's graphics. Also, when changing the image, call repaint() on the JPanel.
- 06-04-2011, 08:44 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,144
- Rep Power
- 5
You commented out code is attempting to paint the image at its actual size. There is no need to do custom painting for this. You only need to do custom painting if you scale or tile or use other painting effects with the image.
See Background Panel « Java Tips Weblog which has a solution for both approaches.
Similar Threads
-
Display Image (png)
By vimalaranjan in forum Java GamingReplies: 2Last Post: 05-04-2011, 12:23 AM -
to display image
By asmitarnd in forum AWT / SwingReplies: 6Last Post: 04-07-2011, 11:17 AM -
image display
By asmitarnd in forum AWT / SwingReplies: 3Last Post: 04-07-2011, 11:17 AM -
Problem trying to display a .jpg image file on a web browser
By allan123 in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 08-05-2010, 08:41 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks