Hello. I want to make a File->Open menu functionality to this program. In JMenuBar I think I create everything correctly but it doesn't display when I run it. How do I display it and make the Open functionality work?

package oimg;

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

class JumbledImage extends Component

    private int numlocs = 2;
    private int numcells = numlocs*numlocs;
    private int[] cells;
    private BufferedImage bi;
    int w, h, cw, ch;

    public JMenuBar createMenuBar()
        JMenuBar menuBar;
        JMenuItem menuItem;
        JMenu menu, submenu;
        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        "The only menu in this program that has menu items");

        //a group of JMenuItems
        menuItem = new JMenuItem("Open",
                        KeyEvent.VK_1, ActionEvent.ALT_MASK));
        "This doesn't really do anything");

        return menuBar;

    public JumbledImage(URL imageSrc)
            bi = ImageIO.read(imageSrc);
            w = bi.getWidth(null);
            h = bi.getHeight(null);
        catch (IOException e)
            System.out.println("Image could not be read");
        cw = w/numlocs;
        ch = h/numlocs;
        cells = new int[numcells];
        for (int i=0;i<numcells;i++)
            cells[i] = i;

    void jumble()
        Random rand = new Random();
        int ri;
        for (int i=0; i<numcells; i++)
            while ((ri = rand.nextInt(numlocs)) == i);

            int tmp = cells[i];
            cells[i] = cells[ri];
            cells[ri] = tmp;

    public Dimension getPreferredSize()
        return new Dimension(w, h);

    public void paint(Graphics g)

        int dx, dy;
        for (int x=0; x<numlocs; x++)
            int sx = x*cw;
            for (int y=0; y<numlocs; y++)
                int sy = y*ch;
                int cell = cells[x*numlocs+y];
                dx = (cell / numlocs) * cw;
                dy = (cell % numlocs) * ch;
                            dx, dy, dx+cw, dy+ch,
                            sx, sy, sx+cw, sy+ch,

public class JumbledImageApplet extends JApplet

    static String imageFileName = "img.jpg";
    private URL imageSrc;
    private JumbledImage jumbledImage;

    public JumbledImageApplet ()

    public JumbledImageApplet (URL imageSrc)
        this.imageSrc = imageSrc;

    public void init()
            imageSrc = new URL(getCodeBase(), imageFileName);
        catch (MalformedURLException e)

    public void buildUI()
        final JumbledImage ji = new JumbledImage(imageSrc);
        add("Center", ji);
        JButton jumbleButton = new JButton("Jumble");
        jumbleButton.addActionListener(new ActionListener()
                public void actionPerformed(ActionEvent e)
                    JButton b = (JButton)e.getSource();
        Dimension jumbleSize = ji.getPreferredSize();
        resize(jumbleSize.width, jumbleSize.height+40);
        add("South", jumbleButton);


    public static void main(String s[])
        JFrame f = new JFrame("Jumbled Image");
                new WindowAdapter()
            public void windowClosing(WindowEvent e) 
        URL imageSrc = null;
        try {
             imageSrc = ((new File(imageFileName)).toURI()).toURL();
        } catch (MalformedURLException e) {
        JumbledImageApplet jumbler = new JumbledImageApplet(imageSrc);
        f.add("Center", jumbler);

I am not sure. I type "f.createMenuBar();" after line 182 and the compiler doesn't like it. No method found, wtf I have it.

What class is the createMenuBar() method in? You need to have a reference to that class to be able to call its methods.

I am lost now. Can you clarify further with code, please?

What class is the createMenuBar() method in? Where is it defined?

Say it's in a class named: TheClass
TheClass aRefToClass = new TheClass(); // create an instance of the class
aRefToClass.createMenuBar(); // call the method that is in the class

This article has been dead for over six months. Start a new discussion instead.