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");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
        "The only menu in this program that has menu items");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("Open",
                         KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                        KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
        "This doesn't really do anything");
        menu.add(menuItem);

        return menuBar;
    }


    public JumbledImage(URL imageSrc)
    {
        try
        {
            bi = ImageIO.read(imageSrc);
            w = bi.getWidth(null);
            h = bi.getHeight(null);
        } 
        catch (IOException e)
        {
            System.out.println("Image could not be read");
            System.exit(1);
        }
        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;
                g.drawImage(bi,
                            dx, dy, dx+cw, dy+ch,
                            sx, sy, sx+cw, sy+ch,
                            null);
            }
        }
    }
}

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()
    {
        try
        {
            imageSrc = new URL(getCodeBase(), imageFileName);
        }
        catch (MalformedURLException e)
        {
        }
        buildUI();
    }

    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();
                    ji.jumble();
                    ji.repaint();
                };
            });
        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");
        f.addWindowListener
                (
                new WindowAdapter()
        {
            public void windowClosing(WindowEvent e) 
            {
                System.exit(0);
            }
        }
                );
        URL imageSrc = null;
        try {
             imageSrc = ((new File(imageFileName)).toURI()).toURL();
        } catch (MalformedURLException e) {
        }
        JumbledImageApplet jumbler = new JumbledImageApplet(imageSrc);
        jumbler.buildUI();
        f.add("Center", jumbler);
        f.pack();
        f.setVisible(true);
    }
}

Recommended Answers

All 5 Replies

Where do you call the code to create the JMenuBar? Where do you add it to the JFrame?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.