在Java的图像使用GUI上添加图标

时间:2018-12-22 18:30:17

标签: java image swing imageicon

我需要使用Java(Eclipse)的GUI构建应用程序。

我需要显示图像并像下面的代码一样添加菜单。 另外,当用户使用鼠标单击图像时,我需要保存位置并在该位置添加图标。因此,如果用户在两个不同的位置单击,则需要在每个位置放置一个图标。

我尝试执行此操作,但是我不知道如何在图像上添加图标。我在其他一些文章中看到可能需要使用repaint()方法,但我不了解该方法的工作原理。

如果有人可以帮助我,我需要在代码中添加些什么,可以将图标放在图像上。

我把所有代码都放在这里。

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);
    });
}
}

0 个答案:

没有答案