File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Changing the color of a dot from blue to red or green with the use of buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Changing the color of a dot from blue to red or green with the use of buttons" Watch "Changing the color of a dot from blue to red or green with the use of buttons" New topic
Author

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

Phil Kooistra
Greenhorn

Joined: Sep 15, 2010
Posts: 2
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

Joined: Jun 01, 2010
Posts: 618
What compiler error(s) are you getting?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

The EX.. arose while copying from the cross post
http://answers.yahoo.com/question/index?qid=20100915162844AARYRH3


luck, db
There are no new questions, but there may be new answers.
Phil Kooistra
Greenhorn

Joined: Sep 15, 2010
Posts: 2
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

Joined: Aug 15, 2010
Posts: 230
    
    2

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

Joined: Oct 13, 2005
Posts: 40034
    
  28
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

Joined: Oct 13, 2005
Posts: 40034
    
  28
We usually discuss this sort of question in our GUIs forum: moving thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Changing the color of a dot from blue to red or green with the use of buttons