Package samotnik

Source Code of samotnik.SamotnikView

/*
* SamotnikView.java
*/

package samotnik;

import java.awt.Color;
import java.awt.Graphics;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

/**
* The application's main frame.
*/
public class SamotnikView extends FrameView {

    public Game game = new Game(Game.BRITISH_GAME);
    private int n=Game.get_n();
    private int margin=4;
    private int x=3,y=3;
    boolean active_game=false;

    int max_x_size, max_y_size;
    int size_x, size_y;

    Color pawn_color = Color.BLACK;
    Color pawn_fill_color = Color.ORANGE;
    Color selected_color = Color.RED;
    Color maps_color = Color.YELLOW;

    public SamotnikView(SingleFrameApplication app) {
        super(app);

        initComponents();
        ButtonGroup group = new ButtonGroup();
        group.add(this.britishRadioButtonMenuItem);
        group.add(this.europeRadioButtonMenuItem);

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                }
            }
        });
    }

    private void Paint(Graphics g) {
        max_x_size = canvas1.getWidth();
        max_y_size = canvas1.getHeight();

        //if you comment it map won't be square
        max_x_size=Math.min(max_x_size,max_y_size);
        max_y_size=Math.min(max_x_size,max_y_size);

        size_x = max_x_size/n;
        size_y = max_y_size/n;

        for (int x2=0; x2<n; ++x2)
            for (int y2=0; y2<n; ++y2) {
                if (x2==x && y2==y) {
                    g.setColor(selected_color);
                    g.fillRect(x2*size_x, y2*size_y, size_x,size_y);
                }
                else if (game.getPawn(x2,y2)>=0) {
                    g.setColor(maps_color);
                    g.fillRect(x2*size_x, y2*size_y, size_x,size_y);
                }
                if (game.getPawn(x2,y2)==1) {
                    g.setColor(pawn_fill_color);
                    g.fillOval(x2*size_x+margin/2, y2*size_y+margin/2, size_x-margin, size_y-margin);
                    g.setColor(pawn_color);
                    g.drawOval(x2*size_x+margin/2, y2*size_y+margin/2, size_x-margin, size_y-margin);
                }
            }
    }

    @Action
    public void moveLeft() {
        if (game.checkCurrentPawn(x-1, y)) {
            x--;
            checkGame();
            Paint(canvas1.getGraphics());
        }
        else
            this.statusMessageLabel.setText("Ruch w lewo niemożliwy");
    }

    @Action
    public void moveRight() {
        if (game.checkCurrentPawn(x+1, y)) {
            x++;
            checkGame();
            Paint(canvas1.getGraphics());
        }
        else
            this.statusMessageLabel.setText("Ruch w prawo niemożliwy");
    }

    @Action
    public void moveUp() {
        if (game.checkCurrentPawn(x, y-1)) {
            y--;
            checkGame();
            Paint(canvas1.getGraphics());
        }
        else
            this.statusMessageLabel.setText("Ruch w górę niemożliwy");
    }

    @Action
    public void moveDown() {
        if (game.checkCurrentPawn(x, y+1)) {
            y++;
            checkGame();
            Paint(canvas1.getGraphics());
        }
        else
            this.statusMessageLabel.setText("Ruch w dół niemożliwy");
    }

    @Action
    public void jumpLeft() {
        if (game.movePawn(x, y, x-2, y)) {
            x-=2;
            Paint(canvas1.getGraphics());
            checkGame();
        }
        else
            this.statusMessageLabel.setText("Bicie w lewo niemożliwe");
    }

    @Action
    public void jumpRight() {
        if (game.movePawn(x, y, x+2, y)) {
            x+=2;
            Paint(canvas1.getGraphics());
            checkGame();
        }
        else
            this.statusMessageLabel.setText("Bicie w prawo niemożliwe");
    }

    @Action
    public void jumpUp() {
        if (game.movePawn(x, y, x, y-2)) {
            y-=2;
            Paint(canvas1.getGraphics());
            checkGame();
        }
        else
            this.statusMessageLabel.setText("Bicie w górę niemożliwe");
    }

    @Action
    public void jumpDown() {
        if (game.movePawn(x, y, x, y+2)) {
            y+=2;
            Paint(canvas1.getGraphics());
            checkGame();
        }
        else
            this.statusMessageLabel.setText("Bicie w dół niemożliwe");
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = SamotnikApp.getApplication().getMainFrame();
            aboutBox = new SamotnikAbout(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        SamotnikApp.getApplication().show(aboutBox);
    }

    @Action
    public void showAboutGameBox() {
        if (aboutGameBox == null) {
            JFrame mainFrame = SamotnikApp.getApplication().getMainFrame();
            aboutGameBox = new SamotnikAboutGame(mainFrame);
            aboutGameBox.setLocationRelativeTo(mainFrame);
        }
        SamotnikApp.getApplication().show(aboutGameBox);
    }

    @Action
    public void  switchGame() {
        x=3;
        y=3;
       
        if (europeRadioButtonMenuItem.isSelected()) {
            game=new Game(Game.EUROPE_GAME);
        }
        else {
            game=new Game(Game.BRITISH_GAME);
        }
        canvas1.repaint();
    }

    @Action
    public void newGame() {
        x=3;
        y=3;

        if (europeRadioButtonMenuItem.isSelected()) {
            game=new Game(Game.EUROPE_GAME);
        }
        else {
            game=new Game(Game.BRITISH_GAME);
        }

        newMenuItem.setEnabled(false);
        stopMenuItem.setEnabled(true);
        europeRadioButtonMenuItem.setEnabled(false);
        britishRadioButtonMenuItem.setEnabled(false);

        this.jumpLeftMenuItem.setEnabled(true);
        this.jumpRightMenuItem.setEnabled(true);
        this.jumpUpMenuItem.setEnabled(true);
        this.jumpDownMenuItem.setEnabled(true);

        this.moveLeftMenuItem.setEnabled(true);
        this.moveRightMenuItem.setEnabled(true);
        this.moveUpMenuItem.setEnabled(true);
        this.moveDownMenuItem.setEnabled(true);

        active_game=true;
        this.statusMessageLabel.setText("Start");
        canvas1.repaint();
    }

    @Action
    public void endGame() {
        newMenuItem.setEnabled(true);
        stopMenuItem.setEnabled(false);
        europeRadioButtonMenuItem.setEnabled(true);
        britishRadioButtonMenuItem.setEnabled(true);

        this.jumpLeftMenuItem.setEnabled(false);
        this.jumpRightMenuItem.setEnabled(false);
        this.jumpUpMenuItem.setEnabled(false);
        this.jumpDownMenuItem.setEnabled(false);

        this.moveLeftMenuItem.setEnabled(false);
        this.moveRightMenuItem.setEnabled(false);
        this.moveUpMenuItem.setEnabled(false);
        this.moveDownMenuItem.setEnabled(false);

        active_game=false;
        //this.statusMessageLabel.setText("Koniec gry");
        canvas1.repaint();
    }

    @Action
    public void setBackgroundColor() {
        JColorChooser clr = new JColorChooser();
        maps_color = clr.showDialog(this.getComponent(), "Wybierz kolor tła", maps_color);
        Paint(canvas1.getGraphics());
    }

    @Action
    public void setPawnColor() {
        JColorChooser clr = new JColorChooser();
        pawn_color = clr.showDialog(this.getComponent(), "Wybierz kolor pionka", pawn_color);
        Paint(canvas1.getGraphics());
    }

    @Action
    public void setFilledPawnColor() {
        JColorChooser clr = new JColorChooser();
        pawn_fill_color = clr.showDialog(this.getComponent(), "Wybierz kolor wypełnienia pionka", pawn_fill_color);
        Paint(canvas1.getGraphics());
    }

    @Action
    public void setSelectedColor() {
        JColorChooser clr = new JColorChooser();
        selected_color = clr.showDialog(this.getComponent(), "Wybierz kolor zaznaczonego pola", selected_color);
        Paint(canvas1.getGraphics());
    }

    public void checkGame() {
        if (game.endGame()) {
            endGame();
            statusMessageLabel.setText("Koniec gry, pozostało: "+Integer.toString(game.otherPawn()));
        }
        else
            statusMessageLabel.setText("Pozostało: "+Integer.toString(game.otherPawn()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu gameMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        stopMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        quitMenuItem = new javax.swing.JMenuItem();
        movementsMenu = new javax.swing.JMenu();
        moveLeftMenuItem = new javax.swing.JMenuItem();
        moveRightMenuItem = new javax.swing.JMenuItem();
        moveUpMenuItem = new javax.swing.JMenuItem();
        moveDownMenuItem = new javax.swing.JMenuItem();
        jumpLeftMenuItem = new javax.swing.JMenuItem();
        jumpRightMenuItem = new javax.swing.JMenuItem();
        jumpUpMenuItem = new javax.swing.JMenuItem();
        jumpDownMenuItem = new javax.swing.JMenuItem();
        settingsMenu = new javax.swing.JMenu();
        europeRadioButtonMenuItem = new javax.swing.JRadioButtonMenuItem();
        britishRadioButtonMenuItem = new javax.swing.JRadioButtonMenuItem();
        jMenu1 = new javax.swing.JMenu();
        pawnMenuItem = new javax.swing.JMenuItem();
        filledPanMenuItem = new javax.swing.JMenuItem();
        backgroundMenuItem = new javax.swing.JMenuItem();
        selectedMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutGameMenuItem = new javax.swing.JMenuItem();
        aboutAppMenuItem = new javax.swing.JMenuItem();
        mainPanel = new javax.swing.JPanel();
        canvas1 = new java.awt.Canvas()
        {
            public void paint(Graphics g)
            {
                super.paint(g);
                Paint(g);
            }
        };
        statusPanel = new javax.swing.JPanel();
        statusMessageLabel = new javax.swing.JLabel();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        mouseJumpLeftMenuItem = new javax.swing.JMenuItem();
        mouseJumpRightMenuItem = new javax.swing.JMenuItem();
        mouseJumpUpMenuItem = new javax.swing.JMenuItem();
        mouseJumpDownMenuItem = new javax.swing.JMenuItem();

        menuBar.setName("menuBar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(samotnik.SamotnikApp.class).getContext().getResourceMap(SamotnikView.class);
        gameMenu.setText(resourceMap.getString("gameMenu.text")); // NOI18N
        gameMenu.setFocusable(false);
        gameMenu.setName("gameMenu"); // NOI18N
        gameMenu.setRolloverEnabled(true);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(samotnik.SamotnikApp.class).getContext().getActionMap(SamotnikView.class, this);
        newMenuItem.setAction(actionMap.get("newGame")); // NOI18N
        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setMnemonic(KeyEvent.VK_N);
        newMenuItem.setText(resourceMap.getString("newMenuItem.text")); // NOI18N
        newMenuItem.setName("newMenuItem"); // NOI18N
        gameMenu.add(newMenuItem);

        stopMenuItem.setAction(actionMap.get("endGame")); // NOI18N
        stopMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_K, java.awt.event.InputEvent.CTRL_MASK));
        stopMenuItem.setMnemonic(KeyEvent.VK_K);
        stopMenuItem.setText(resourceMap.getString("stopMenuItem.text")); // NOI18N
        stopMenuItem.setEnabled(false);
        stopMenuItem.setName("stopMenuItem"); // NOI18N
        gameMenu.add(stopMenuItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        gameMenu.add(jSeparator1);

        quitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        quitMenuItem.setText(resourceMap.getString("quitMenuItem.text")); // NOI18N
        quitMenuItem.setName("quitMenuItem"); // NOI18N
        gameMenu.add(quitMenuItem);

        menuBar.add(gameMenu);

        movementsMenu.setText(resourceMap.getString("movementsMenu.text")); // NOI18N
        movementsMenu.setName("movementsMenu"); // NOI18N

        moveLeftMenuItem.setAction(actionMap.get("moveLeft")); // NOI18N
        moveLeftMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_LEFT, 0));
        moveLeftMenuItem.setText(resourceMap.getString("moveLeftMenuItem.text")); // NOI18N
        moveLeftMenuItem.setEnabled(false);
        moveLeftMenuItem.setName("moveLeftMenuItem"); // NOI18N
        movementsMenu.add(moveLeftMenuItem);

        moveRightMenuItem.setAction(actionMap.get("moveRight")); // NOI18N
        moveRightMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_RIGHT, 0));
        moveRightMenuItem.setText(resourceMap.getString("moveRightMenuItem.text")); // NOI18N
        moveRightMenuItem.setEnabled(false);
        moveRightMenuItem.setName("moveRightMenuItem"); // NOI18N
        movementsMenu.add(moveRightMenuItem);

        moveUpMenuItem.setAction(actionMap.get("moveUp")); // NOI18N
        moveUpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_UP, 0));
        moveUpMenuItem.setText(resourceMap.getString("moveUpMenuItem.text")); // NOI18N
        moveUpMenuItem.setEnabled(false);
        moveUpMenuItem.setName("moveUpMenuItem"); // NOI18N
        movementsMenu.add(moveUpMenuItem);

        moveDownMenuItem.setAction(actionMap.get("moveDown")); // NOI18N
        moveDownMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DOWN, 0));
        moveDownMenuItem.setText(resourceMap.getString("moveDownMenuItem.text")); // NOI18N
        moveDownMenuItem.setEnabled(false);
        moveDownMenuItem.setName("moveDownMenuItem"); // NOI18N
        movementsMenu.add(moveDownMenuItem);

        jumpLeftMenuItem.setAction(actionMap.get("jumpLeft")); // NOI18N
        jumpLeftMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_LEFT, java.awt.event.InputEvent.SHIFT_MASK));
        jumpLeftMenuItem.setText(resourceMap.getString("jumpLeftMenuItem.text")); // NOI18N
        jumpLeftMenuItem.setEnabled(false);
        jumpLeftMenuItem.setName("jumpLeftMenuItem"); // NOI18N
        movementsMenu.add(jumpLeftMenuItem);

        jumpRightMenuItem.setAction(actionMap.get("jumpRight")); // NOI18N
        jumpRightMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_RIGHT, java.awt.event.InputEvent.SHIFT_MASK));
        jumpRightMenuItem.setText(resourceMap.getString("jumpRightMenuItem.text")); // NOI18N
        jumpRightMenuItem.setEnabled(false);
        jumpRightMenuItem.setName("jumpRightMenuItem"); // NOI18N
        movementsMenu.add(jumpRightMenuItem);

        jumpUpMenuItem.setAction(actionMap.get("jumpUp")); // NOI18N
        jumpUpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_UP, java.awt.event.InputEvent.SHIFT_MASK));
        jumpUpMenuItem.setText(resourceMap.getString("jumpUpMenuItem.text")); // NOI18N
        jumpUpMenuItem.setEnabled(false);
        jumpUpMenuItem.setName("jumpUpMenuItem"); // NOI18N
        movementsMenu.add(jumpUpMenuItem);

        jumpDownMenuItem.setAction(actionMap.get("jumpDown")); // NOI18N
        jumpDownMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DOWN, java.awt.event.InputEvent.SHIFT_MASK));
        jumpDownMenuItem.setText(resourceMap.getString("jumpDownMenuItem.text")); // NOI18N
        jumpDownMenuItem.setEnabled(false);
        jumpDownMenuItem.setName("jumpDownMenuItem"); // NOI18N
        movementsMenu.add(jumpDownMenuItem);

        menuBar.add(movementsMenu);

        settingsMenu.setText(resourceMap.getString("settingsMenu.text")); // NOI18N

        europeRadioButtonMenuItem.setAction(actionMap.get("switchGame")); // NOI18N
        europeRadioButtonMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        europeRadioButtonMenuItem.setSelected(true);
        europeRadioButtonMenuItem.setText(resourceMap.getString("europeRadioButtonMenuItem.text")); // NOI18N
        europeRadioButtonMenuItem.setIcon(resourceMap.getIcon("europeRadioButtonMenuItem.icon")); // NOI18N
        europeRadioButtonMenuItem.setName("europeRadioButtonMenuItem"); // NOI18N
        settingsMenu.add(europeRadioButtonMenuItem);

        britishRadioButtonMenuItem.setAction(actionMap.get("switchGame")); // NOI18N
        britishRadioButtonMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        britishRadioButtonMenuItem.setSelected(true);
        britishRadioButtonMenuItem.setText(resourceMap.getString("britishRadioButtonMenuItem.text")); // NOI18N
        britishRadioButtonMenuItem.setActionCommand(resourceMap.getString("britishRadioButtonMenuItem.actionCommand")); // NOI18N
        britishRadioButtonMenuItem.setName("britishRadioButtonMenuItem"); // NOI18N
        settingsMenu.add(britishRadioButtonMenuItem);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        pawnMenuItem.setAction(actionMap.get("setPawnColor")); // NOI18N
        pawnMenuItem.setText(resourceMap.getString("pawnMenuItem.text")); // NOI18N
        pawnMenuItem.setName("pawnMenuItem"); // NOI18N
        jMenu1.add(pawnMenuItem);

        filledPanMenuItem.setAction(actionMap.get("setFilledPawnColor")); // NOI18N
        filledPanMenuItem.setText(resourceMap.getString("filledPanMenuItem.text")); // NOI18N
        filledPanMenuItem.setName("filledPanMenuItem"); // NOI18N
        jMenu1.add(filledPanMenuItem);

        backgroundMenuItem.setAction(actionMap.get("setBackgroundColor")); // NOI18N
        backgroundMenuItem.setText(resourceMap.getString("backgroundMenuItem.text")); // NOI18N
        backgroundMenuItem.setName("backgroundMenuItem"); // NOI18N
        jMenu1.add(backgroundMenuItem);

        selectedMenuItem.setAction(actionMap.get("setSelectedColor")); // NOI18N
        selectedMenuItem.setText(resourceMap.getString("selectedMenuItem.text")); // NOI18N
        selectedMenuItem.setName("selectedMenuItem"); // NOI18N
        jMenu1.add(selectedMenuItem);

        settingsMenu.add(jMenu1);

        menuBar.add(settingsMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutGameMenuItem.setAction(actionMap.get("showAboutGameBox")); // NOI18N
        aboutGameMenuItem.setText(resourceMap.getString("aboutGameMenuItem.text")); // NOI18N
        aboutGameMenuItem.setName("aboutGameMenuItem"); // NOI18N
        helpMenu.add(aboutGameMenuItem);

        aboutAppMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutAppMenuItem.setText(resourceMap.getString("aboutAppMenuItem.text")); // NOI18N
        aboutAppMenuItem.setName("aboutAppMenuItem"); // NOI18N
        helpMenu.add(aboutAppMenuItem);

        menuBar.add(helpMenu);

        mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        mainPanel.setDoubleBuffered(false);
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(450, 450));
        mainPanel.addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener() {
            public void ancestorMoved(java.awt.event.HierarchyEvent evt) {
            }
            public void ancestorResized(java.awt.event.HierarchyEvent evt) {
                mainPanelResized(evt);
            }
        });

        canvas1.setName("canvas1"); // NOI18N
        canvas1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                canvasMouseClick(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                .addContainerGap())
        );

        statusPanel.setName("statusPanel"); // NOI18N

        statusMessageLabel.setText(resourceMap.getString("statusMessageLabel.text")); // NOI18N
        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addContainerGap(253, Short.MAX_VALUE))
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addComponent(statusMessageLabel)
                .addGap(3, 3, 3))
        );

        statusMessageLabel.getAccessibleContext().setAccessibleName(resourceMap.getString("statusMessageLabel.AccessibleContext.accessibleName")); // NOI18N

        jPopupMenu1.setName("jPopupMenu1"); // NOI18N

        mouseJumpLeftMenuItem.setAction(actionMap.get("jumpLeft")); // NOI18N
        mouseJumpLeftMenuItem.setText(resourceMap.getString("mouseJumpLeftMenuItem.text")); // NOI18N
        mouseJumpLeftMenuItem.setName("mouseJumpLeftMenuItem"); // NOI18N
        jPopupMenu1.add(mouseJumpLeftMenuItem);

        mouseJumpRightMenuItem.setAction(actionMap.get("jumpRight")); // NOI18N
        mouseJumpRightMenuItem.setText(resourceMap.getString("mouseJumpRightMenuItem.text")); // NOI18N
        mouseJumpRightMenuItem.setName("mouseJumpRightMenuItem"); // NOI18N
        jPopupMenu1.add(mouseJumpRightMenuItem);

        mouseJumpUpMenuItem.setAction(actionMap.get("jumpUp")); // NOI18N
        mouseJumpUpMenuItem.setText(resourceMap.getString("mouseJumpUpMenuItem.text")); // NOI18N
        mouseJumpUpMenuItem.setName("mouseJumpUpMenuItem"); // NOI18N
        jPopupMenu1.add(mouseJumpUpMenuItem);

        mouseJumpDownMenuItem.setAction(actionMap.get("jumpDown")); // NOI18N
        mouseJumpDownMenuItem.setText(resourceMap.getString("mouseJumpDownMenuItem.text")); // NOI18N
        mouseJumpDownMenuItem.setName("mouseJumpDownMenuItem"); // NOI18N
        jPopupMenu1.add(mouseJumpDownMenuItem);

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void canvasMouseClick(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvasMouseClick
        if (active_game) {
            int x_buf=evt.getX()/this.size_x;
            int y_buf=evt.getY()/this.size_y;

            if (SwingUtilities.isRightMouseButton(evt)) {
                if (game.checkCurrentPawn(x_buf, y_buf)) {
                    x=x_buf;
                    y=y_buf;
                    Paint(canvas1.getGraphics());
                }
                if (game.getPawn(x_buf, y_buf)>=0) {
                    JPopupMenu Pmenu = new JPopupMenu();
                    JMenuItem menuItem;

                    menuItem = new JMenuItem("Zbij w lewo");
                    //menuItem.setAction(this.jumpLeftMenuItem.getAction());
                    Pmenu.add(menuItem);

                    menuItem = new JMenuItem("Zbij w prawo");
                    menuItem.setAction(this.jumpRightMenuItem.getAction());
                    Pmenu.add(menuItem);

                    menuItem = new JMenuItem("Zbij w górę");
                    menuItem.setAction(this.jumpUpMenuItem.getAction());
                    Pmenu.add(menuItem);

                    menuItem = new JMenuItem("Zbij w dół");
                    menuItem.setAction(this.jumpDownMenuItem.getAction());
                    Pmenu.add(menuItem);

                    this.jPopupMenu1.show(this.getComponent(), evt.getX(), evt.getY());
                    //Pmenu.show(this.getComponent(), evt.getX(), evt.getY());
                }
            }
            else {
                if (game.getPawn(x_buf, y_buf)==0) {
                    if (game.movePawn(x, y, x_buf, y_buf)) {
                        x=x_buf;
                        y=y_buf;
                        checkGame();
                        Paint(canvas1.getGraphics());
                    }
                    else
                        this.statusMessageLabel.setText("Ruch niemożliwy");
                }
                else {
                    if (game.checkCurrentPawn(x_buf, y_buf)) {
                        x=x_buf;
                        y=y_buf;
                        checkGame();
                        Paint(canvas1.getGraphics());
                    }
                    else
                        this.statusMessageLabel.setText("Ruch niemożliwy");
                }
            }
        }
}//GEN-LAST:event_canvasMouseClick

    private void mainPanelResized(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_mainPanelResized
        canvas1.setLocation(0, 0);
        canvas1.setSize(mainPanel.getWidth()-20,mainPanel.getHeight()-20);
        Paint(canvas1.getGraphics());
        menuBar.updateUI();
    }//GEN-LAST:event_mainPanelResized

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutAppMenuItem;
    private javax.swing.JMenuItem backgroundMenuItem;
    private javax.swing.JRadioButtonMenuItem britishRadioButtonMenuItem;
    private java.awt.Canvas canvas1;
    private javax.swing.JRadioButtonMenuItem europeRadioButtonMenuItem;
    private javax.swing.JMenuItem filledPanMenuItem;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JMenuItem jumpDownMenuItem;
    private javax.swing.JMenuItem jumpLeftMenuItem;
    private javax.swing.JMenuItem jumpRightMenuItem;
    private javax.swing.JMenuItem jumpUpMenuItem;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mouseJumpDownMenuItem;
    private javax.swing.JMenuItem mouseJumpLeftMenuItem;
    private javax.swing.JMenuItem mouseJumpRightMenuItem;
    private javax.swing.JMenuItem mouseJumpUpMenuItem;
    private javax.swing.JMenuItem moveDownMenuItem;
    private javax.swing.JMenuItem moveLeftMenuItem;
    private javax.swing.JMenuItem moveRightMenuItem;
    private javax.swing.JMenuItem moveUpMenuItem;
    private javax.swing.JMenu movementsMenu;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem pawnMenuItem;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JMenuItem selectedMenuItem;
    private javax.swing.JMenu settingsMenu;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JMenuItem stopMenuItem;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
    private JDialog aboutGameBox;
}
TOP

Related Classes of samotnik.SamotnikView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.