0

I need to build a app using GUI of Java (Eclipse).

I need to show an image and add menu like I did in the code below. Additionally when the user use is mouse to click on the image I need to save the location and to add icon in this location. So if the user click in two different places I need to put an icon in every location.

I try to do this but I don't know how to add the icons on the image. I see in some another articles that maybe I need to use repaint() method but I don't understand how this method works.

If someone can help me what I need to add to my code that I can put the icons on the image.

I put here all my code.

public class MainWindowApp extends JFrame {

private JMenuBar menuBar;
private JMenu fileMenu, helpMenu, gameMenu, createGame, addObjects, startMenu;
private JMenuItem openItem, saveItem, conMenu, clearItem, fruitItem, pacmanItem;
private JMenuItem aboutUsItem, conItem, playGameItem, simulationItem, stopDrawObjectsItem;
private Game game;
private int pacman = 0, fruit = 0;
private Board board;

public MainWindowApp() {
    game = new Game();
    initUI();
    initComponents();
    createActions();

}

private void initUI() {
    board=new Board();
    add(board);

    pack();

    setTitle("Bardejov");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}

public void initComponents() {
    // set menu bar
    menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    menuBar.setVisible(true);

    // set file menu with open, save
    fileMenu = new JMenu("File");
    openItem = new JMenuItem("Open");
    // CTRL_MASK: The control modifier. An indicator that the control key was held
    // down during the event.
    openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    fileMenu.add(openItem);
    saveItem = new JMenuItem("Save");
    saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    fileMenu.add(saveItem);
    menuBar.add(fileMenu);

    // set Game menu with create game, clear game
    gameMenu = new JMenu("Game options");
    createGame = new JMenu("Create new game");
    addObjects = new JMenu("Add objects");
    clearItem = new JMenuItem("Clear game");
    pacmanItem = new JMenuItem("Pacman");
    fruitItem = new JMenuItem("Fruit");
    stopDrawObjectsItem = new JMenuItem("Stop add objects");
    addObjects.add(pacmanItem);
    addObjects.add(fruitItem);
    addObjects.add(stopDrawObjectsItem);
    createGame.add(addObjects);
    gameMenu.add(createGame);
    gameMenu.add(clearItem);
    menuBar.add(gameMenu);

    // set start menu
    startMenu = new JMenu("Play game");
    simulationItem = new JMenuItem("Eat simulate");
    playGameItem = new JMenuItem("start game");

    // set help menu
    helpMenu = new JMenu("Help");
    conItem = new JMenuItem("Connection");
    aboutUsItem = new JMenuItem("About us");
    helpMenu.add(conItem);
    helpMenu.add(aboutUsItem);
    menuBar.add(helpMenu);

}

public void createActions() {
    pacmanItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            fruit = 0;
            pacman = 1;

        }

    });
    fruitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pacman = 0;
            fruit = 1;
        }
    });
    stopDrawObjectsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pacman = 0;
            fruit = 0;
        }
    });
    openItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("open file");
            try {
                readFileDialog();
            } catch (FileNotFoundException e1) {
                System.out.println("File not found");
            }
        }
    });
    saveItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("save file");
            writeFileDialog();
        }
    });
}

public void readFileDialog() throws FileNotFoundException {
    // try read from the file

    FileDialog fd = new FileDialog(this, "Open csv file", FileDialog.LOAD);
    fd.setFile("*.csv");
    fd.setDirectory("C:\\");
    fd.setFilenameFilter(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".csv");
        }
    });
    fd.setVisible(true);
    String folder = fd.getDirectory();
    String fileName = fd.getFile();
    try {
        CsvReader csvReader = new CsvReader();
        csvReader.init(folder + fileName, ",");
        game = csvReader.read(1);

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

public void writeFileDialog() {
    // try write to the file
    FileDialog fd = new FileDialog(this, "Save the game", FileDialog.SAVE);
    fd.setFile("*.csv");
    fd.setFilenameFilter(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".csv");
        }
    });
    fd.setVisible(true);
    String folder = fd.getDirectory();
    String fileName = fd.getFile();
    try {
        game.saveGame(folder + fileName);
    } catch (IOException ex) {
        System.out.print("Error writing file  " + ex);
    }
}

int x = -1;
int y = -1;

public void mouseClicked(MouseEvent arg) {
    System.out.println("mouse Clicked");
    System.out.println("(" + arg.getX() + "," + arg.getY() + ")");
    x = arg.getX();
    y = arg.getY();
    int maxId = game.getBiggestId() + 1;
    if (fruit == 1)
        game.add(new Fruit(maxId, Map.pixel2Polar(new Point3D(x, y, 0)), 1));
    else if (pacman == 1)
        game.add(new Pacman(maxId, Map.pixel2Polar(new Point3D(x, y, 0)), 1, 1, null));
    else
        return;
    repaint();
}

public static void main(String[] args) {

    EventQueue.invokeLater(() -> {
        MainWindowApp ex = new MainWindowApp();
        ex.setVisible(true);
    });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) *"I put here all my code."* Don't do that. An attempt to load an image can be done in a complete code example of less than 30 lines of code. For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) `FileDialog fd..` unless you have a .. – Andrew Thompson Dec 22 '18 at 19:23
  • .. compelling reason to do otherwise, stick with Swing based components like `JFileChooser`. – Andrew Thompson Dec 22 '18 at 19:25
  • Better start by having a look at [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) and [Painting in AWT and Swing](https://www.oracle.com/technetwork/java/painting-140037.html) – MadProgrammer Dec 22 '18 at 20:25

0 Answers0