2017-05-08 23 views
1

我想創建一個遊戲,其中有2個按鈕紅色藍色。方形從Color數組中隨機選取一種顏色。因此,如果我們按下2個按鈕中的任何一個,並且該按鈕的顏色與正方形中的隨機顏色相同,我們得分。這裏是我的代碼 -如何檢查按鈕顏色是否與在Java中的方形顏色相同圖形並增加分數

import java.awt.Button; 
import java.awt.Color; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.TextArea; 
import java.awt.TextField; 
import java.awt.event.*; 
import java.util.Random; 

import javax.swing.JLabel; 
import javax.swing.JTextArea; 
class apple1 extends Frame implements ActionListener{ 
    private Button b; 

    public apple1(){ 


     tf = new TextField("Points: "); 
     tf.setBounds(10, 30, 140, 20); 

     //create components 
     b=new Button("RED"); 
     b.setBackground(Color.red); 
     b.setBounds(80,260,80,30); 


     Button b2=new Button("BLUE"); 
     b2.setBackground(Color.blue); 
     b2.setBounds(180,260,80,30); 

     //register listener 
     b.addActionListener(this);//passing current instance 

     //add components and set size, layout and visibility 
     add(tf);add(b);add(b2); 
     setSize(600,600); 
     setLayout(null); 
     setVisible(true); 
     } 

TextField tf; 
JTextArea lbl; 
    public void paint(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 
     Color[] clrs = {Color.red,Color.blue}; 
     Random rand = new Random(); 
     g2.setColor(clrs[rand.nextInt(clrs.length)]); 
     g2.fillRect (60, 50, 200, 200); 
     if(g2.getColor().equals(b.getBackground())){ 
      int count = 0; 
      tf.setText(String.valueOf(count++)); 
     } 
     } 

public void actionPerformed(ActionEvent e){ 
    repaint(); 
} 
public static void main(String args[]){ 
new apple1(); 
} 
} 

這裏是我在日食得到的錯誤:在apple1.paint 異常在線程 「AWT-EventQueue的-0」 顯示java.lang.NullPointerException (apple1.java:53) 在sun.awt.RepaintArea.paintComponent(RepaintArea.java:264) 在sun.lwawt.LWRepaintArea.paintComponent(LWRepaintArea.java:59) 在sun.awt.RepaintArea.paint(RepaintArea.java:240) 在sun.lwawt.LWComponentPeer.handleJavaPaintEvent(LWComponentPeer.java:1314) at sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1198) at java.awt.Component.dispatchEventImpl(Com (java.util.java:4965) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component。 java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access $ 500(EventQueue.java:97) at java.awt.EventQueue $ 3.run(EventQueue。 java:709) at java.awt.EventQueue $ 3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java: 76) at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDom aa.java:86) at java.awt.EventQueue $ 4.run(EventQueue.java:731) at java.awt.EventQueue $ 4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(本地方法) 在java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java :201) 在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101 )在ja的 va.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Random Color Color Compare

我在這個發展的初始階段,所謂的遊戲,但我認爲我無法獲得在b按鈕的actionPerformed()方法中調用的g2 Graphics變量。請幫忙。

+0

你聲明'tf'和'lbl'無論是在範圍的構造函數和類成員。如果從構造函數中刪除聲明會發生什麼? – jsheeran

+0

嘗試了你的建議。但即使我的紅色按鈕點擊是正方形中的紅色,也只得到0分。所以我認爲count ++不起作用。 –

+0

嘗試將'count'聲明爲類成員。每次調用paint()時它都被初始化爲0。此外,按照慣例,類名以大寫字母開頭 - 它應該是「Apple1」。 – jsheeran

回答

0

你應該從構造函數中刪除聲明:

class apple1 extends Frame implements ActionListener { 

private Button b; 

TextField tf; 
JTextArea lbl; 
int count = 0; 

public apple1() { 

    tf = new TextField("Points: "); 
    tf.setBounds(10, 30, 140, 20); 

    //create components 
    b = new Button("RED"); 
    b.setBackground(Color.red); 
    b.setBounds(80, 260, 80, 30); 

    Button b2 = new Button("BLUE"); 
    b2.setBackground(Color.blue); 
    b2.setBounds(180, 260, 80, 30); 

    //register listener 
    b.addActionListener(this);//passing current instance 

    //add components and set size, layout and visibility 
    add(tf); 
    add(b); 
    add(b2); 
    setSize(600, 600); 
    setLayout(null); 
    setVisible(true); 
} 

public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    Color[] clrs = {Color.red, Color.blue}; 
    Random rand = new Random(); 
    g2.setColor(clrs[rand.nextInt(clrs.length)]); 
    g2.fillRect(60, 50, 200, 200); 
    if (g2.getColor().equals(b.getBackground())) { 
     tf.setText(String.valueOf(count++)); 
    } 
} 

public void actionPerformed(ActionEvent e) { 
    repaint(); 
} 

public static void main(String args[]) { 
    new apple1(); 
} 
} 
+0

是啊聲明/初始化構造函數和paint()函數之外的計數做了訣竅。謝謝你... –

0

計數應宣佈塗料功能之外這樣

class Apple1 extends Frame implements ActionListener { 

private Button b; 
TextField tf; 
JTextArea lbl; 
int count = 0; 

public Apple1() { 
    tf = new TextField("Points: "); 
    tf.setBounds(10, 30, 140, 20); 
    //create components 
    b = new Button("RED"); 
    b.setBackground(Color.red); 
    b.setBounds(80, 260, 80, 30); 

    Button b2 = new Button("BLUE"); 
    b2.setBackground(Color.blue); 
    b2.setBounds(180, 260, 80, 30); 

    //register listener 
    b.addActionListener(this);//passing current instance 

    //add components and set size, layout and visibility 
    add(tf); 
    add(b); 
    add(b2); 
    setSize(600, 600); 
    setLayout(null); 
    setVisible(true); 
} 

public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    Color[] clrs = {Color.red, Color.blue}; 
    Random rand = new Random(); 
    g2.setColor(clrs[rand.nextInt(clrs.length)]); 
    g2.fillRect(60, 50, 200, 200); 
    if (g2.getColor().equals(b.getBackground())) { 
     tf.setText(String.valueOf(count++)); 
    } 
} 

public void actionPerformed(ActionEvent e) { 
    repaint(); 
} 

public static void main(String args[]) { 
    new Apple1(); 
} 
} 
+0

是啊聲明/初始化構造函數和paint()函數之外的計數做了訣竅。謝謝... –