Results 1 to 3 of 3
  1. #1
    alvalv is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default painting problems, one class in one file

    Since I started to learn Java, one month ago, I've been having painting problems.
    I simplified the program as much as I could.
    The program is a JFrame with a menu. The program paints a drawing on the window. Using the menu you can choose a different drawing to be painted. But it does not work.
    I have three files, MyFrame.java, MyPanel.java and MyMenu.java.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class MyFrame extends JFrame {
    
      MyFrame(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension (200,200));
        setLocationRelativeTo(null);
        setVisible(true);
      }
    
      public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                MyFrame h = new MyFrame();
    				    MyPanel g = new MyPanel();
                h.getContentPane().add(g);
                MyMenu m= new MyMenu();
                h.setJMenuBar(m);
                h.pack();
                }
            });
      }
    
    }

    Java Code:
    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import javax.swing.JFrame;
    import javax.imageio.*;
    import java.awt.image.*;
    
    
    public class MyPanel extends JPanel{
    
    static int imgtopaint=1;
    BufferedImage imgone, imgtwo;
    
    MyPanel(){        
        LoadImageApp();
      }
    
    public void LoadImageApp()                  {
           try {
          imgone = ImageIO.read(new File("one.gif"));
    		  imgtwo = ImageIO.read(new File("two.gif"));
           } catch (IOException e) {}           }
    
    public void paint(Graphics g) {
      //super.paint( g );
      System.out.println(" paint called,  imgtopaint = " + imgtopaint );  // for debugging
      if(imgtopaint == 1)
        g.drawImage(imgone, 30, 60, null); 	
      if(imgtopaint == 2)
        g.drawImage(imgtwo, 30, 60, null); 
    }
    
    }

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    
    public class MyMenu extends JMenuBar{
    
      MyMenu(){
      JMenu mimenu = new JMenu("Paint");
      JMenuItem selectone = new JMenuItem("One");      
      mimenu.add(selectone);
      JMenuItem selecttwo = new JMenuItem("Two");      
      mimenu.add(selecttwo);
      this.add(mimenu);
    
      selectone.addActionListener( new ActionListener()
      { public void actionPerformed( ActionEvent e )
      { MyPanel.imgtopaint=1;
        System.out.println(" menu one called");  // for debugging
        MyPanel.repaint();    // <----  error
      } } );
    
      selecttwo.addActionListener(new ActionListener()
      { public void actionPerformed( ActionEvent e )
      { MyPanel.imgtopaint=2;
        System.out.println(" menu two called");  // for debugging
        MyPanel.repaint();    //  <---  error
      } } );
      }
    
    }

    When I compile the files I get: "MyMenu.java:19: non-static method repaint cannot be referenced from a static context".

    My questions: 1- Is the ActionListener routine a static context ?
    2 - How can I call a repaint from a menu action ?

    I wish to have the menu code in a separate file.
    Thanks in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: painting problems, one class in one file

    You need to have an instance of MyPanel ('g' in your main() method), since repaint is an instance method.
    That instance needs to be passed into the MyMenu instance, so it can then be used in the listener methods.
    Also, your imptopaint should not be static.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    alvalv is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default Re: painting problems, one class in one file

    Thank you Tolls, it worked.

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2012, 06:42 PM
  2. Replies: 2
    Last Post: 10-17-2011, 08:04 PM
  3. InputStream/Jar Problems/File IO Problems
    By rdjava in forum Advanced Java
    Replies: 31
    Last Post: 01-17-2011, 11:12 AM
  4. Replies: 12
    Last Post: 10-09-2009, 06:26 AM
  5. Inner class problems
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-13-2008, 08:56 AM

Posting Permissions

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