ParanoidPanda

Untitled

Nov 5th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class Main extends JFrame{
  6. static Main windows;
  7. Image o_viewer;
  8. String f_filename;
  9. DrawPanel o_panel;
  10. int v_imgheight = 0;
  11. int v_imgwidht = 0;
  12. JButton bt_okey,bt_exit,bt_about,bt_file;
  13. public static final int DEFAULT_WIDTH = 360;
  14. public static final int DEFAULT_HEIGHT = 480;
  15.  
  16. Main(){
  17.  
  18. super("Maze pather");
  19. setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  20. setVisible(true); //окно видимое
  21. //setResizable(false); //запрет на resize
  22. setLocationRelativeTo( null ); //появление окна в центре
  23.  
  24. bt_file = new JButton("Open Maze");
  25. bt_okey = new JButton("Build It!");
  26. bt_exit = new JButton("Exit");
  27. bt_about = new JButton("About");
  28.  
  29. setLayout(new FlowLayout()); //какая то штука для того чтобы кнопки были не на весь экран
  30.  
  31. o_viewer = null;
  32. f_filename = null;
  33.  
  34. add(bt_file);
  35. add(bt_okey);
  36. add(bt_about);
  37. add(bt_exit);
  38.  
  39. o_panel = new DrawPanel();
  40. add(o_panel);
  41.  
  42. bt_file.addActionListener(new ButtonActionListener());
  43. bt_exit.addActionListener(new ButtonActionListener()); //описываем что у кнопки есть событие
  44. bt_about.addActionListener(new ButtonActionListener());
  45.  
  46. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //описание закрытие окна
  47.  
  48. }
  49.  
  50. class ButtonActionListener implements ActionListener{ //описываем класс обработки событий нажатия кнопок
  51. public void actionPerformed (ActionEvent v_event){
  52. if(v_event.getActionCommand().equals("About")){
  53. //AboutWindow about = new AboutWindow();
  54. }
  55. if(v_event.getActionCommand().equals("Exit")){
  56. System.exit(0);
  57. }
  58. if(v_event.getActionCommand().equals("Open Maze")){
  59. LoadFile();
  60. }
  61. }
  62. }
  63.  
  64. public void LoadFile(){
  65. FileDialog d_file = new FileDialog(this,"Open maze image file ...", FileDialog.LOAD);
  66. d_file.setVisible(true);
  67. f_filename = d_file.getFile();
  68.  
  69. if(f_filename == null)
  70. return;
  71.  
  72. ImageIcon tmp = new ImageIcon(f_filename);
  73. o_viewer = tmp.getImage();
  74.  
  75. v_imgheight = o_viewer.getHeight(o_panel);
  76. v_imgwidht = o_viewer.getWidth(o_panel);
  77.  
  78. o_panel.repaint();
  79. }
  80.  
  81. public static void main(String [] args){
  82. windows = new Main();
  83. }
  84.  
  85. class DrawPanel extends JPanel {
  86. public void paintComponent(Graphics g) {
  87. super.paintComponent(g);
  88. if(o_viewer != null)
  89. g.drawImage(o_viewer,0,0,this);
  90. else
  91. System.out.printf("XUI");
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment