Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.event.*;
- import javax.swing.*;
- public class MainWindow extends JFrame implements KeyListener, ActionListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- static int i=4;
- static int j=1;
- Cell cells[][] = new Cell[10][20];
- private Timer timer;;
- boolean firstLoad = true;
- Graphics gr;
- JTextArea displayArea;
- JTextField typingArea;
- static final String newline = System.getProperty("line.separator");
- @Override
- public void keyPressed(KeyEvent e) {
- if(e.getKeyCode() == KeyEvent.VK_F3)
- {
- setVisible(false);
- System.exit(0);
- }
- int keyCod = e.getKeyCode();
- if (keyCod == KeyEvent.VK_R)
- {try {
- redrawField();
- } catch (InterruptedException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- repaint();}}
- public static void main(String[] args) {
- Thread t=Thread.currentThread();
- try {
- UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
- } catch (UnsupportedLookAndFeelException ex) {
- ex.printStackTrace();
- } catch (IllegalAccessException ex) {
- ex.printStackTrace();
- } catch (InstantiationException ex) {
- ex.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
- public void paint(Graphics gr) {
- gr.drawLine(0, 0, 0, 600);
- gr.drawLine(0, 0, 300, 0);
- gr.drawLine(300, 600, 300, 0);
- gr.drawLine(0, 600, 300, 600);
- gr.setColor(Color.white);
- gr.fillRect(1, 1, 298, 598);
- this.gr = gr;
- if (firstLoad) {
- firstLoad = false;
- try {
- fillField();
- fillField();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public void redrawField() throws InterruptedException {
- for (i = 4; i < 5; i++) {
- for (j = 0; j < 18; j++) {
- cells[i - 1][j + 1] = new BlackZyu(gr, 0, (i + 1) * 30,
- j * 30, "Black");
- cells[i][j + 2] = new BlackZyu(gr, 0, (i + 1) * 30,
- j * 30, "Black");
- if (cells[i - 1][j + 1].getName().equals("Lock")
- || cells[i][j + 2].getName().equals("Lock"))
- break;
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, j * 30,
- "Black");
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, (j + 1) * 30,
- "Black");
- cells[i][j] = new BlackZyu(gr, 30, i * 30, (j + 1) * 30,
- "Black");
- cells[i][j] = new BlackZyu(gr, 30, i * 30, (j + 2) * 30,
- "Black");
- Thread.currentThread();
- Thread.sleep(150);
- if (j != 17) {
- cells[i][j] = new WhiteZyu(gr, 30, (i - 1) * 30, j * 30,
- "White");
- cells[i][j] = new WhiteZyu(gr, 30, (i - 1) * 30,
- (j + 1) * 30, "White");
- cells[i][j] = new WhiteZyu(gr, 30, i * 30, (j + 1) * 30,
- "White");
- cells[i][j] = new WhiteZyu(gr, 30, i * 30, (j + 2) * 30,
- "White");
- }
- }
- }
- if (j == 17) {
- cells[i][j] = new BlackZyu(gr, 0, i * 30, (j + 2) * 30, "Lock");
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, (j + 1) * 30,
- "Lock");
- }
- }
- // private int getI() {
- // return i;
- //
- // }
- public void fillField() throws InterruptedException {
- timer=new Timer(150,new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- if (j<17) {
- j++;
- System.out.print(""+cells[i][j].getName());
- cells[i - 1][j + 1] = new BlackZyu(gr, 0, (i + 1) * 30, j * 30, "Black");
- cells[i][j + 2] = new BlackZyu(gr, 0, (i + 1) * 30, j * 30, "Black");
- if (cells[i - 1][j + 1].getName().equals("Lock")
- || cells[i][j + 2].getName().equals("Lock"))
- timer.stop();
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, j * 30, "Black");
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, (j + 1) * 30, "Black");
- cells[i][j] = new BlackZyu(gr, 30, i * 30, (j + 1) * 30, "Black");
- cells[i][j] = new BlackZyu(gr, 30, i * 30, (j + 2) * 30, "Black");
- if (j != 17) {
- cells[i][j - 1] = new WhiteZyu(gr, 30, (i - 1) * 30, j * 30, "White");
- cells[i][j - 1] = new WhiteZyu(gr, 30, (i - 1) * 30, (j + 1) * 30,
- "White");
- cells[i][j - 1] = new WhiteZyu(gr, 30, i * 30, (j + 1) * 30, "White");
- cells[i][j - 1] = new WhiteZyu(gr, 30, i * 30, (j + 2) * 30, "White");
- }
- if (j == 17) {
- cells[i][j] = new BlackZyu(gr, 0, i * 30, (j + 2) * 30, "Lock");
- cells[i][j] = new BlackZyu(gr, 30, (i - 1) * 30, (j + 1) * 30, "Lock");
- }
- }
- }
- } );
- timer.start();
- }
- private static void createAndShowGUI() {
- MainWindow frame = new MainWindow("KeyEventDemo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.addComponentsToPane();
- frame.pack();
- frame.setVisible(true);
- }
- private void addComponentsToPane() {
- JButton button = new JButton("Новая игра");
- button.addActionListener(this);
- typingArea = new JTextField(20);
- typingArea.addKeyListener(this);
- displayArea = new JTextArea();
- displayArea.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(displayArea);
- scrollPane.setPreferredSize(new Dimension(300, 600));
- getContentPane().add(typingArea, BorderLayout.PAGE_START);
- getContentPane().add(scrollPane, BorderLayout.CENTER);
- getContentPane().add(button, BorderLayout.PAGE_END);
- }
- public MainWindow(String name) {
- super(name);
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- displayArea.setText("");
- typingArea.setText("");
- typingArea.requestFocusInWindow();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment