Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.AbstractAction;
  3. import javax.swing.JButton;
  4. import javax.swing.JPanel;
  5. import javax.swing.JFileChooser;
  6. import javax.swing.JMenu;
  7. import javax.swing.JMenuBar;
  8. import javax.swing.JOptionPane;
  9. import java.awt.BorderLayout;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. import java.awt.Image;
  13. import java.awt.event.*;
  14. import java.awt.image.BufferedImage;
  15. import java.io.*;
  16. import javax.imageio.*;
  17.  
  18. public class Game extends JFrame {
  19.  
  20. //static JFrame frame;
  21.  
  22. Game()
  23. {
  24. super("Robots see");
  25. setVisible(true);
  26. setSize(1400, 800);
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. JMenuBar menu = new JMenuBar();
  29. getContentPane().add(BorderLayout.NORTH, menu);
  30.  
  31. JMenu btn_load = new JMenu("Открыть");
  32. JMenu btn_save = new JMenu("Сохранить");
  33. JMenu btn_saveAs = new JMenu("Сохранить как");
  34. JMenu btn_sets = new JMenu("Настройки");
  35.  
  36. menu.add(btn_load);
  37. menu.add(btn_save);
  38. menu.add(btn_saveAs);
  39. menu.add(btn_sets);
  40.  
  41. btn_load.addActionListener(new LoadClicked(this));
  42. }
  43.  
  44.  
  45.  
  46. public static void main(String[] args) throws IOException {
  47.  
  48. Game game = new Game();
  49. }
  50. }
  51.  
  52. class LoadClicked implements ActionListener
  53. {
  54. static Game game;
  55. static BufferedImage image;
  56. LoadClicked(Game game)
  57. {
  58. this.game = game;
  59. }
  60. public void actionPerformed(ActionEvent e)
  61. {
  62. String filename;
  63. JFileChooser fileChooser = new JFileChooser();
  64. int result = fileChooser.showOpenDialog(null);
  65.  
  66. // Если директория выбрана, покажем ее в сообщении
  67.  
  68. try{ if (result == JFileChooser.APPROVE_OPTION )
  69. {
  70. filename = fileChooser.getSelectedFile().getAbsolutePath();
  71. image = ImageIO.read(new File(filename));
  72. }}
  73. catch(IOException e1) { }
  74.  
  75. JPanel pane = new JPanel() {
  76. @Override
  77. protected void paintComponent(Graphics g) {
  78. super.paintComponent(g);
  79. g.drawImage(image, 0, 0, null);
  80. }
  81. };
  82. game.add(pane);
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement