Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing the color of a dot from blue to red or green with the use of buttons

 
Phil Kooistra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't figure out why this will not work, Every other example I look at show the listeners where they are in my code but it won't even compile. The program is suppose to make 2 buttons one saying red, one green and when they are pressed turn the dot that color respectively



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Lab35C {
JFrame frame;
JButton green;
JButton red;
Dot dot;
public static void main(String[] args) {
Lab35C me = new Lab35C();
me.go();
}
public void go() {
frame = new JFrame("Watch the dot");
frame.setDefaultCloseOperation(JFrame.EX…
frame.setSize(300, 300);
green = new JButton("Green dot");

green.addActionListener(this);
frame.getContentPane().add(green, BorderLayout.NORTH);

red = new JButton("Red dot");

red.addActionListener(this);
frame.getContentPane().add(red, BorderLayout.SOUTH);

dot = new Dot();
frame.getContentPane().add(dot);
frame.setVisible(true);
while (true) {
dot.repaint();
try {
Thread.sleep(50);
}
catch (Exception err) {}
}
}
class Dot extends JPanel {
Color color = Color.blue;
public void paintComponent(Graphics g) {
int height = this.getHeight();
int width = this.getWidth();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(color);
int randomX = (int) (Math.random() * width);
int randomY = (int) (Math.random() * height);
g.fillOval(randomX, randomY, 30, 30);
}
public void changeColor(Color nColor) {
color = nColor;
}
}
class GreenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.green);
}
}
class RedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.red);
}
}
}


javascript:emoticon('');

Thanks a ton ahead of time
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compiler error(s) are you getting?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all welcome to the JavaRach.

Please when posting code UseCodeTags and TellTheDetails. Shortening JFrame.EXIT_ON_CLOSE is only annoying because then it will not compile.
The problem is that you're trying to add actionListeners using this but your class doesn't implement ActionListener.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Here is your code formatted (with line 18 corrected) and posted with code tags. See how much friendlier that is?

See Wouter's post for the likely problem.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EX.. arose while copying from the cross post
http://answers.yahoo.com/question/index?qid=20100915162844AARYRH3
 
Phil Kooistra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I messed up on the copy paste from one blog to this one the code is


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Lab35C {
JFrame frame;
JButton green;
JButton red;
Dot dot;

public static void main(String[] args) {
Lab35C me = new Lab35C();
me.go();
}

public void go() {
frame = new JFrame("Watch the dot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
green = new JButton("Green dot");

green.addActionListener(this);
frame.getContentPane().add(green, BorderLayout.NORTH);

red = new JButton("Red dot");

red.addActionListener(this);
frame.getContentPane().add(red, BorderLayout.SOUTH);

dot = new Dot();
frame.getContentPane().add(dot);
frame.setVisible(true);
while (true) {
dot.repaint();
try {
Thread.sleep(50);
}
catch (Exception err) {}
}
}

class Dot extends JPanel {
Color color = Color.blue;

public void paintComponent(Graphics g) {
int height = this.getHeight();
int width = this.getWidth();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(color);
int randomX = (int) (Math.random() * width);
int randomY = (int) (Math.random() * height);
g.fillOval(randomX, randomY, 30, 30);
}

public void changeColor(Color nColor) {
color = nColor;
}
}

class GreenListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.green);
}
}

class RedListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dot.changeColor(Color.red);
}
}
}



The compile errors I'm getting are


Lab35C.java:22: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
green.addActionListener(this);
^
Lab35C.java:27: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
red.addActionListener(this);
^
2 errors



Thanks for the help, you guys are on this quick
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should UseCodeTags.

What are the purposes of having these two classes?
The answer would help you to fix the error.



Lab35C.java:22: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
green.addActionListener(this);
^
Lab35C.java:27: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Lab35C)
red.addActionListener(this);
^
2 errors
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Tong wrote: . . . The answer would help you to fix the error. . . .
Disagree. I think addActionListener(this), however often you see it in books, is non-object-oriented programming. You are actually better calling a class ColourListener than RedListener etc. I think you should have the Dot as a field of the ColourListener class, as well as the Color object, so you can say this sort of thing:You obviously set up the two fields in the ColourListener constructor. Not quite like this
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We usually discuss this sort of question in our GUIs forum: moving thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic