2012-09-22 107 views
1

我想做一個naughts和十字架遊戲,所以你用鼠標選擇一個盒子,然後鍵盤上的X或O輸入一個答案。我已經得到儘可能的盒子選擇工作,但似乎無法得到X和O的工作。我的代碼可能不是最好的,因爲這是我製作的第一款遊戲。按鍵顯示圖像,並保持在屏幕上,直到線程停止

這裏是我的主類:

package com; 

import javax.swing.*; 

import java.awt.event.*; 
import java.awt.*; 

public class Board extends JPanel implements ActionListener, MouseListener, 
     KeyListener { 

    public int box = 1; 
    public int x, y; 
    public int cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4, cx5, cy5; 

    public boolean player1 = true; 
    public boolean player2 = false; 
    public boolean box1 = false; 
    public boolean box2 = false; 
    public boolean box3 = false; 
    public boolean box4 = false; 
    public boolean box5 = false; 
    public boolean box6 = false; 
    public boolean box7 = false; 
    public boolean box8 = false; 
    public boolean box9 = false; 
    public boolean xpress = false; 
    public boolean cross1 = false; 
    public boolean cross2 = false; 
    public boolean cross3 = false; 
    public boolean cross4 = false; 
    public boolean cross5 = false; 

    public Thread t; 
    Timer time; 

    JButton cross, naught; 

    Image Board, Select, Cross, Cross2, Cross3, Cross4, Cross5, Naught; 

    Board() { 
     super(); 
     x = 0; 
     y = 60; 

     t = new Thread(); 
     time = new Timer(5, this); 
     time.start(); 
     t.start(); 
     setFocusable(true); 

     addMouseListener(this); 
     addKeyListener(this); 

     ImageIcon i = new ImageIcon(getClass().getResource(
       "/images/Board_N+C.png")); 
     Board = i.getImage(); 
     ImageIcon i1 = new ImageIcon(getClass().getResource(
       "/images/Select_N+C.png")); 
     Select = i1.getImage(); 
     ImageIcon i2 = new ImageIcon(getClass().getResource(
       "/images/Cross_N+C.png")); 
     Cross = i2.getImage(); 
     Cross2 = i2.getImage(); 
     Cross3 = i2.getImage(); 
     Cross4 = i2.getImage(); 
     Cross5 = i2.getImage(); 

     ImageIcon i3 = new ImageIcon(getClass().getResource(
       "/images/Naught_N+C.png")); 
     Naught = i3.getImage(); 

    } 

    public void paint(Graphics g) { 
     super.paint(g); 
     Graphics2D g2d = (Graphics2D) g; 

     g2d.drawImage(Board, 0, 60, null); 
     if (cross1) { 

     } 

     if (x < 170 && y < 230 && !xpress) { 
      g2d.drawImage(Select, 0, 60, null); 

      repaint(); 
     } else if (x < 340 && x > 170 && y < 230 && !xpress) { 
      g2d.drawImage(Select, 170, 60, null); 
      repaint(); 

     } else if (x < 510 && x > 340 && y < 230 && !xpress) { 
      g2d.drawImage(Select, 340, 60, null); 
      repaint(); 

     } else if (x < 170 && y < 400 && y > 230 && !xpress) { 
      g2d.drawImage(Select, 0, 230, null); 
      repaint(); 

     } else if (x < 340 && x > 170 && y < 400 && y > 230 && !xpress) { 
      g2d.drawImage(Select, 170, 230, null); 
      repaint(); 

     } else if (x < 510 && x > 340 && y < 400 && y > 230 && !xpress) { 
      g2d.drawImage(Select, 340, 230, null); 
      repaint(); 

     } else if (x < 170 && y < 570 && y > 400 && !xpress) { 
      g2d.drawImage(Select, 0, 400, null); 
      repaint(); 

     } else if (x < 340 && x > 170 && y < 570 && y > 400 && !xpress) { 
      g2d.drawImage(Select, 170, 400, null); 
      repaint(); 

     } else if (x < 510 && x > 340 && y < 570 && y > 400 && !xpress) { 
      g2d.drawImage(Select, 340, 400, null); 
      repaint(); 
     } 
     if (xpress && !player2) { 
      if (x < 170 && y < 230) { 
       cross1 = true; 
       while (cross1) { 
        g2d.drawImage(Cross, 0, 60, null); 
        System.out.println("Cross"); 

       }repaint(); 
       xpress = false; 
      } else if (x < 340 && x > 170 && y < 230) { 
       repaint(); 

      } else if (x < 510 && x > 340 && y < 230) { 
       repaint(); 

      } else if (x < 170 && y < 400 && y > 230) { 
       repaint(); 

      } else if (x < 340 && x > 170 && y < 400 && y > 230) { 
       repaint(); 

      } else if (x < 510 && x > 340 && y < 400 && y > 230) { 
       repaint(); 

      } else if (x < 170 && y < 570 && y > 400) { 
       repaint(); 

      } else if (x < 340 && x > 170 && y < 570 && y > 400) { 
       repaint(); 

      } else if (x < 510 && x > 340 && y < 570 && y > 400) { 
       repaint(); 
      } 
     } 
    } 

    public void mouseClicked(MouseEvent me) { 

     x = me.getX(); 
     y = me.getY(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    } 

    public void mouseEntered(MouseEvent arg0) { 
    } 

    public void mouseExited(MouseEvent arg0) { 
    } 

    public void mousePressed(MouseEvent arg0) { 
    } 

    public void mouseReleased(MouseEvent arg0) { 
    } 

    public void keyPressed(KeyEvent e) { 
     int key = e.getKeyCode(); 
     if (key == e.VK_X) { 
      if (player1) { 
       xpress = true; 
       player1 = false; 
       player2 = true; 
      } 
     } 

    } 

    public void keyReleased(KeyEvent e) { 

    } 

    public void keyTyped(KeyEvent e) { 

    } 

} 

你如何在按鍵顯示圖像,並直到線程停止它保留在屏幕上?

希望有人能幫忙。如果您需要更多信息,請詢問。

+1

@AndrewThompson,我認爲它是在標題。 – Nate

+0

一般提示:1)'g2d.drawImage(Select,0,60,null);'應該是'g2d.drawImage(Select,0,60,this);'2)使用圖像作爲按鈕的圖標在網格佈局中。 3)對於Swing,使用'KeyListener'查看鍵綁定。 4)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

@Nate哦對,我總是'讀&忘記'的標題。 –

回答

1

你顯然不知道Java中的線程是如何工作的。

t = new Thread(); 
time = new Timer(5, this); 
time.start(); 
t.start(); 

首先,Thread的建立將什麼也不做(其他然後有一個線程啓動並迅速死亡的原因,而是沒有做任何事情)。其次,你最好有一個非常好的理由每5毫秒發射一個計時器。這將會限制事件調度線程,最終會消耗你的CPU並讓你的程序停下來。

花時間通過

這裏是一個非常簡單的例子,BYO圖像具有讀取...

public class PopShowAndFadeImage { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 

     JFrame test = new JFrame("Test"); 
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     test.setSize(700, 1000); 
     test.setLocationRelativeTo(null); 
     test.setLayout(new BorderLayout()); 
     test.add(new ImagePane()); 
     test.setVisible(true); 

    } 

    protected static class ImagePane extends JPanel { 

     private BufferedImage background; 

     private Image image; 
     private Timer timeOut; 

     public ImagePane() { 

      timeOut = new Timer(1000, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 

        image = null; 
        repaint(); 

       } 
      }); 
      timeOut.setRepeats(false); 
      timeOut.setCoalesce(true); 

      setFocusable(true); 

      InputMap im = getInputMap(WHEN_FOCUSED); 
      ActionMap am = getActionMap(); 

      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "HitMe"); 
      am.put("HitMe", new AbstractAction() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 

        if (background == null) { 
         try { 
          background = ImageIO.read(getClass().getResource("/MT011.gif")); 
         } catch (IOException exp) { 
          exp.printStackTrace();; 
         } 
        } 

        image = background; 

        repaint(); 

        timeOut.restart(); 

       } 
      }); 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 

      super.paintComponent(g); 

      if (image != null) { 

       int width = getWidth() - 1; 
       int height = getHeight() - 1; 

       Graphics2D g2d = (Graphics2D) g; 
       int x = (width - image.getWidth(this))/2; 
       int y = (height - image.getHeight(this))/2; 

       g2d.drawImage(image, x, y, this); 

      } 

     } 

    } 

} 

UPDATE

只是一個快速的一些你所面臨的問題的概述。

你承認自己是Java /編程的新手,但你已經在跳躍到自定義圖形和動畫。自定義圖形非常困難,動畫更是如此。

以下是您需要的一些背景元素。

  • 很好的瞭解基本編程原理(如,循環,等等) - 這是基本的東西
  • 面向對象編程的很好的理解。這對大多數人來說都不是微不足道的
  • 對Swing API的瞭解,特別是繪畫的工作原理
  • 對Graphics/Graphics2D API有很好的理解。這可不是微不足道的
  • 對線程的理解以及它們如何與Swing API相關。這不是微不足道的
  • 至少對動畫原理有基本的瞭解。

從你所說的話,它看起來好像你試圖在你掌握爬行之前飛行。我並不是說你做不到,但是你的理解和知識存在很大差距,這會使你難以達到你的目標。

當涉及到動畫,我用的是Timing Framework,但你也可以看看Trident

我要去嘗試,並把一些例子在一起,但我在前面貼了一個會做完全按照您的標題已經要求

+0

花時間去瀏覽[Java Trails](http://docs.oracle.com/javase/tutorial/),他們是一個很好的資源 – MadProgrammer

+0

是的我是新來的java,不知道什麼一半是事物。我正在使用計時器將動畫選擇框移動到每個方塊,但忘記將其移除。你能否展示一個與我的項目相關的例子,因爲我非常需要幫助。 Thanx – user1690395

+0

我經歷了很多Java Trails,但似乎無法想出如何在不使用按鈕的情況下使該項目工作的想法。我真的很喜歡它,但它不會工作。 – user1690395