Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class Main extends JFrame{
- static Main windows;
- Image o_viewer;
- String f_filename;
- DrawPanel o_panel;
- int v_imgheight = 0;
- int v_imgwidht = 0;
- JButton bt_okey,bt_exit,bt_about,bt_file;
- public static final int DEFAULT_WIDTH = 360;
- public static final int DEFAULT_HEIGHT = 480;
- Main(){
- super("Maze pather");
- setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
- setVisible(true); //окно видимое
- //setResizable(false); //запрет на resize
- setLocationRelativeTo( null ); //появление окна в центре
- bt_file = new JButton("Open Maze");
- bt_okey = new JButton("Build It!");
- bt_exit = new JButton("Exit");
- bt_about = new JButton("About");
- setLayout(new FlowLayout()); //какая то штука для того чтобы кнопки были не на весь экран
- o_viewer = null;
- f_filename = null;
- add(bt_file);
- add(bt_okey);
- add(bt_about);
- add(bt_exit);
- o_panel = new DrawPanel();
- add(o_panel);
- bt_file.addActionListener(new ButtonActionListener());
- bt_exit.addActionListener(new ButtonActionListener()); //описываем что у кнопки есть событие
- bt_about.addActionListener(new ButtonActionListener());
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //описание закрытие окна
- }
- class ButtonActionListener implements ActionListener{ //описываем класс обработки событий нажатия кнопок
- public void actionPerformed (ActionEvent v_event){
- if(v_event.getActionCommand().equals("About")){
- //AboutWindow about = new AboutWindow();
- }
- if(v_event.getActionCommand().equals("Exit")){
- System.exit(0);
- }
- if(v_event.getActionCommand().equals("Open Maze")){
- LoadFile();
- }
- }
- }
- public void LoadFile(){
- FileDialog d_file = new FileDialog(this,"Open maze image file ...", FileDialog.LOAD);
- d_file.setVisible(true);
- f_filename = d_file.getFile();
- if(f_filename == null)
- return;
- ImageIcon tmp = new ImageIcon(f_filename);
- o_viewer = tmp.getImage();
- v_imgheight = o_viewer.getHeight(o_panel);
- v_imgwidht = o_viewer.getWidth(o_panel);
- o_panel.repaint();
- }
- public static void main(String [] args){
- windows = new Main();
- }
- class DrawPanel extends JPanel {
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- if(o_viewer != null)
- g.drawImage(o_viewer,0,0,this);
- else
- System.out.printf("XUI");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment