Results 1 to 6 of 6
  1. #1
    ejyption is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Trouble adding a BufferedImage to a JFrame

    I don't understand why the image is not being added to the JPanel.
    Allot of this code is just the addition of buttons, menuItems, textareas etc. but the trouble im having is with the image, it never shows.
    The class getImg is called when the actionListener for 'open' is invoked. 'open' is a JMenuItem.

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

    public class a1 extends JFrame implements ActionListener, MouseMotionListener{
    JFrame f= new JFrame();
    JPanel controlP=new JPanel();
    JPanel textP= new JPanel();
    File fileName=null;
    ////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////JButtons and JLabels/////////////////////////////////
    JLabel l1=new JLabel("X");
    JLabel l2=new JLabel("Y");
    JLabel l3=new JLabel("R");
    JLabel l4=new JLabel("G");
    JLabel l5=new JLabel("B");
    JButton but1=new JButton("O");
    JButton but2=new JButton("FlipX");
    JButton but3=new JButton("FlipY");
    JButton but4=new JButton("FlipXY");
    JButton but5=new JButton("Negate");
    ////////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////Text Areas////////////////////////////
    protected JTextArea txtx=new JTextArea("",1,5);
    protected JTextArea txty=new JTextArea("",1,5);
    protected JTextArea txtr=new JTextArea("",1,5);
    protected JTextArea txtg=new JTextArea("",1,5);
    protected JTextArea txtb=new JTextArea("",1,5);
    ///////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////JMenu/////////////////////////////////////
    JMenuBar menu= new JMenuBar();
    JMenu file= new JMenu("File");
    JMenu help= new JMenu("Help");
    JMenuItem open= new JMenuItem("Open");
    JMenuItem quit= new JMenuItem("Quit");
    ///////////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////Action//////////////////////////////
    public void actionPerformed(ActionEvent e){
    JComponent source= (JComponent)e.getSource();

    if(source==open){
    JFileChooser chooser = new JFileChooser("./");
    int retVal = chooser.showOpenDialog(this);
    if (retVal == JFileChooser.APPROVE_OPTION) {
    fileName=chooser.getSelectedFile();
    System.out.println(fileName);
    Container pane = f.getContentPane();
    pane.add(new getImg(fileName));
    }
    }else if(source==quit){
    System.exit(1);
    }else if(source==but1){
    }else if(source==but2){
    }else if(source==but3){
    }else if(source==but4){
    }else if(source==but5){
    }
    }
    ////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////Mouse/////////////////////////////
    public void mouseDragged(MouseEvent event){}
    public void mouseMoved(MouseEvent event) {
    int x=event.getX(), y=event.getY();
    txtx.setText(String.valueOf(event.getX()));
    txty.setText(String.valueOf(event.getY()));
    }
    ////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////Constructor/////////////////////////
    public a1(){
    super("My Assignment");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.setSize(600,500);
    f.addMouseMotionListener(this);
    //////////////////////////////////////menu implemented////////////////////////////////
    f.setJMenuBar(menu);
    menu.add(file);
    menu.add(help);
    file.add(open); open.addActionListener(this);
    file.add(quit); quit.addActionListener(this);
    //////////////////////////////////////////add buttons to controlP//////////////////////////
    controlP.add(but1); but1.addActionListener(this);
    controlP.add(but2); but2.addActionListener(this);
    controlP.add(but3); but3.addActionListener(this);
    controlP.add(but4); but4.addActionListener(this);
    controlP.add(but5); but5.addActionListener(this);
    ////////////////////////////////////////Labels and TextArea to textP/////////////////////////////////
    textP.add(l1);
    textP.add(txtx); txtx.setEditable(false); textP.add(l2);
    textP.add(txty); txty.setEditable(false); textP.add(l3);
    textP.add(txtr); txtr.setEditable(false); textP.add(l4);
    textP.add(txtg); txtg.setEditable(false); textP.add(l5);
    textP.add(txtb); txtb.setEditable(false);
    //////////////////////////////////////////////////////////////////////////////////////
    f.add(controlP,BorderLayout.NORTH);
    f.add(textP);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    public static void main(String args[]){
    new a1();
    }
    }

    class getImg extends JFrame{
    BufferedImage image=null;

    public getImg(File fl){
    try {
    image = ImageIO.read(fl);
    System.out.println(image.getWidth() + " X " + image.getHeight());
    }
    catch( IOException e ){
    e.printStackTrace();
    }
    }
    public void paintComponent( Graphics g ){
    if( image != null ){
    Graphics2D g2 = (Graphics2D)g;
    g2.drawImage( image, 0, 0, null );
    }
    }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    Last edited by ejyption; 08-05-2013 at 01:42 AM. Reason: won't apply tab formating to lines

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Trouble adding a BufferedImage to a JFrame

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Trouble adding a BufferedImage to a JFrame

    Custom painting isn't done in a top level window like JFrame. Learn to do it the correct way: Lesson: Performing Custom Painting (The Java Tutorials > Creating a GUI With JFC/Swing)

    And while you're editing your post, get rid of those silly lines of slashes and inane comments. They only add clutter. Moreover, at a glance I can see that most of the posted code has nothing whatsoever to do with displaying and image. To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Prrmod is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Trouble adding a BufferedImage to a JFrame

    Refer this tutorial mate [Moderator edit: link removed] ..This will help you understand the logic of adding a BufferedImage to swing components .
    Last edited by DarrylBurke; 08-07-2013 at 11:18 AM. Reason: Removed link

  5. #5
    ejyption is offline Member
    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Default Re: Trouble adding a BufferedImage to a JFrame

    Thank you guys, i will have a go at learning these.
    Regards,
    E

  6. #6
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: Trouble adding a BufferedImage to a JFrame

    there should be a program here that puts a buffered image on a jframe if your still looking animated

Similar Threads

  1. adding a JFXPanel to a JFrame
    By such orb in forum AWT / Swing
    Replies: 2
    Last Post: 11-29-2012, 06:32 PM
  2. adding keylistener to JFrame
    By Alkor in forum New To Java
    Replies: 3
    Last Post: 03-08-2012, 12:02 PM
  3. Adding Scaled BufferedImage to ContentPane
    By switch in forum AWT / Swing
    Replies: 12
    Last Post: 01-26-2012, 01:46 AM
  4. Trouble adding JPanel to JFrame with NetBeans IDE
    By ozzyman in forum New To Java
    Replies: 1
    Last Post: 05-23-2011, 10:35 PM
  5. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •