如何在java Graphics中检查按钮颜色是否与正方形颜色相同并增加点数
How to check if Button Color is same as square color in java Graphics and increase the points
我正在尝试创建一个游戏,其中有 2 个按钮 red 和 blue。正方形从 Color 数组中随机选择一种颜色。因此,如果我们按下 2 个按钮中的任何一个,并且该按钮的颜色与方块中的随机颜色相同,我们就会得分。这是我的代码-
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();
}
}
这是我在 eclipse 中遇到的错误:
线程异常 "AWT-EventQueue-0" java.lang.NullPointerException
在 apple1.paint(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)
在 sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1198)
在 java.awt.Component.dispatchEventImpl(Component.java:4965)
在 java.awt.Container.dispatchEventImpl(Container.java:2294)
在 java.awt.Window.dispatchEventImpl(Window.java:2746)
在 java.awt.Component.dispatchEvent(Component.java:4711)
在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
java.awt.EventQueue.access500 美元(EventQueue.java:97)
在 java.awt.EventQueue$3.run(EventQueue.java:709)
在 java.awt.EventQueue$3.run(EventQueue.java:703)
在 java.security.AccessController.doPrivileged(本机方法)
在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
在 java.awt.EventQueue$4.run(EventQueue.java:731)
在 java.awt.EventQueue$4.run(EventQueue.java:729)
在 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)
在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
在 java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Random Color Color Compare
我正处于开发这个所谓游戏的初始阶段,但我认为我无法获得在 b 按钮的 actionPerformed() 方法中调用的 g2 Graphics 变量。请帮忙。
您应该从构造函数中删除声明:
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();
}
}
count 应该像这样在 paint 函数之外声明
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();
}
}
我正在尝试创建一个游戏,其中有 2 个按钮 red 和 blue。正方形从 Color 数组中随机选择一种颜色。因此,如果我们按下 2 个按钮中的任何一个,并且该按钮的颜色与方块中的随机颜色相同,我们就会得分。这是我的代码-
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();
}
}
这是我在 eclipse 中遇到的错误: 线程异常 "AWT-EventQueue-0" java.lang.NullPointerException 在 apple1.paint(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) 在 sun.lwawt.LWComponentPeer.handleEvent(LWComponentPeer.java:1198) 在 java.awt.Component.dispatchEventImpl(Component.java:4965) 在 java.awt.Container.dispatchEventImpl(Container.java:2294) 在 java.awt.Window.dispatchEventImpl(Window.java:2746) 在 java.awt.Component.dispatchEvent(Component.java:4711) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) java.awt.EventQueue.access500 美元(EventQueue.java:97) 在 java.awt.EventQueue$3.run(EventQueue.java:709) 在 java.awt.EventQueue$3.run(EventQueue.java:703) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) 在 java.awt.EventQueue$4.run(EventQueue.java:731) 在 java.awt.EventQueue$4.run(EventQueue.java:729) 在 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) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Random Color Color Compare
我正处于开发这个所谓游戏的初始阶段,但我认为我无法获得在 b 按钮的 actionPerformed() 方法中调用的 g2 Graphics 变量。请帮忙。
您应该从构造函数中删除声明:
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();
}
}
count 应该像这样在 paint 函数之外声明
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();
}
}