1

I created two classes which one extending. I create an object, but a file is not appearing (method work fine, because title form draw() method appears. There is all code:

public class Main_class extends JFrame implements ActionListener{
 //**************************// 
 public static void main(String[] args) {
       java.awt.EventQueue.invokeLater(new Runnable(){
 public void run(){
 new Main_class().setVisible(true);
 }
 });
}
 //**************************// 
 JPanel panel;

 JMenuBar mbar; 
 JMenuItem item;

 JMenuItem open;
 JMenu file;
 BufferedImage my_image;


public Main_class(){
setSize(800, 600);
setTitle("TEST");
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel=new JPanel();

mbar=new JMenuBar();
setJMenuBar(mbar);

    file=new JMenu("File");
    mbar.add(file);

    open=new JMenuItem("Open");
    open.addActionListener(this);
    file.add(open);      
 }

@Override
public void actionPerformed(ActionEvent e) {
 String zrodlo=e.getActionCommand();
 image_class k=new image_class();
    if(zrodlo.equals("Open")) try {
        k.load(my_image);
    } catch (IOException ex) {
        Logger.getLogger(Main_class.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

///////////////////////////////////////////

public class image_class extends Main_class{

public void load(BufferedImage my_image) throws IOException{
  JFileChooser open_chooser=new JFileChooser("//");
  FileNameExtensionFilter rast=new FileNameExtensionFilter("Pliki grafiki rastrowej(.jpeg,.png.,gif...)", "jpeg","jpg", "gif","png","bmp");
  open_chooser.setFileFilter(rast);
  int a=open_chooser.showOpenDialog(null);

  if(a==JFileChooser.APPROVE_OPTION){
  String image_name=open_chooser.getSelectedFile().getAbsolutePath();
  String roz=image_name.substring(image_name.lastIndexOf('.')+1);
  my_image=ImageIO.read(open_chooser.getSelectedFile());               
  draw();    
}
}

public void draw(){  
 panel=new JPanel(){
        protected void paintComponent(Graphics g){
            Graphics g2 = g.create();
            g2.drawImage(my_image, 0, 0, getWidth(), getHeight(), null);
            g2.dispose();             
        }         
 };   
 panel.setBounds(0, 0, 200, 200);
 add(panel);
 revalidate();  
 repaint();   
 System.out.print("LOADED!!!!!!");
 }
 }
Swapnil
  • 8,201
  • 4
  • 38
  • 57
Michal
  • 65
  • 2
  • 9
  • image_class should be renamed ImageClass or something like that to conform with Java naming conventions (class names begin with an upper case letter), but much more importantly, image_class should most definitely *not* extend your Main_class as that's a misuse of inheritance and can cause all sorts of misbehaviors. – Hovercraft Full Of Eels Jan 12 '13 at 14:12
  • Ok. So I want create two classes, which one contain methods to open, save and process image (for example) and second one to create frame, menubar etc. . How can i do that? – Michal Jan 12 '13 at 14:27

2 Answers2

2

Your image does not appear as you

  1. Never display your 2nd JFrame, image_class, by calling setVisible.
  2. Because Java is pass-by-value, you are not assigning my_image to the class member variable of the same name in your load method, but rather a local copy of the variable:

Replace

my_image = ImageIO.read(open_chooser.getSelectedFile());

with

this.my_image = ImageIO.read(open_chooser.getSelectedFile());

(or simply don't pass in the variable)

Would recommend that you use a single JFrame here, add a sub-class of JComponent and draw the image on that component.

Related:

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

One of your main problems is that you're misusing inheritance. Your code here from image_class.java:

public void draw() {
  panel = new JPanel() {
     protected void paintComponent(Graphics g) {
        Graphics g2 = g.create();
        g2.drawImage(my_image, 0, 0, getWidth(), getHeight(), null);
        g2.dispose();
     }
  };
  panel.setBounds(0, 0, 200, 200);
  add(panel);
  revalidate();
  repaint();
  System.out.print("LOADED!!!!!!");
}

You are adding the new JPanel to a Main_class instance, but not the one that is displayed but rather to the one that image_class inherits from. These are two completely distinct objects and making changes to one will not affect the other.

The solution is not to misuse inheritance for this but rather to display the image in the original GUI.

Also, you should never dispose of a Graphics object that was given to you from the JVM as this can have nasty side effects.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I found tut, which is showing what in this situation must to do: http://www.youtube.com/watch?v=YqO6NsFapZ0 . – Michal Jan 12 '13 at 16:52