Problem With Program That is Supposed to Scribble In Different Co....
11/10/2018 12:08
The following program is suppossed to scribble a lineon a panel after different buttons are pressed but Iget the following errors:

At the line Panel p = new Scribble();I get the error message:
Scribble cannot be resolved to a type
In the lines with the colors: color = Color.black; color = Color.blue color = Color.green; color = Color.yellow; color = Color.orange;I get the error message:
color cannot be resolved
Finally at the line g.setColor(color)color cannot be resolved
The code follows and the points where Iget the errors are marked with *** ***Any suggestions would be appreciated
Thanx in advance
Alan
import java.applet.*;import java.awt.*;import java.awt.event.*;class TestScribble2 { public static void main(String[] args){ TestScribble ts = new TestScribble(); ts.setVisible(true); } } class TestScribble extends Frame implements ActionListener{ ***Panel p = new Scribble();*** Button Black = new Button(Black); Button Blue = new Button(Blue); Button Yellow = new Button(Yellow); Button Green = new Button(Green); Button Orange = new Button(Orange); public TestScribble(){ setSize(500,500); setLayout(new FlowLayout()); add(p); add(Black); add(Blue); add(Yellow); add(Black); add(Black); Black.addActionListener(this); Blue.addActionListener(this); Yellow.addActionListener(this); Green.addActionListener(this); Orange.addActionListener(this); }
public void actionPerformed(ActionEvent e){ if(e.getSource()== Black){ ***color = Color.black;*** } else if (e.getSource() == Blue) { *** color = Color.blue;*** } else if (e.getSource() == Yellow) { *** color = Color.yellow;*** } else if (e.getSource() == Green) { ***color = Color.green;*** } else if (e.getSource() == Orange) { *** color = Color.orange; *** }

class Scribble extends Panel { int last_x,last_y;
public Scribble(){ this.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ last_x = e.getX(); last_y = e.getY(); } }); this.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ Graphics g = getGraphics(); int x = e.getX(), y = e.getY(); ***g.setColor(color);*** g.drawLine(last_x,last_y,x,y); last_x = x; last_y = y; } }); } } }}

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
11/10/2018 12:08 - To assign a value to a variable, you must first *declare* the variable, which you haven't...


Declare an instance variable in Scribble as:

Color color = Color.black;
...and you'll see *that* problem go away, so you can continue with the rest... ;-)
// Bjorn A

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
11/10/2018 12:08 - Alan Fried wrote...


Read this line again:

I didn't say that you should declare *local* variables,but to declare an *instance* variable...


Because you only declared color as *local* variables inside actionPerformed, instead of as an instance variable...


Alright, I'll walk it through with you. I comment some things as we go along.


Where I have written [snipped code] means that I just have taken it out to shorten this message, not that you should do it in your code.


[snipped code]
class Scribble extends Panel implements ActionListener{
// And here we declare an *instance* variable...


Color color = Color.black;
[snipped code, declaration of other variables]
public Scribble() {
[snipped code, initializations, layouts, listeners, etc]
this.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ int x = e.getX(); int y = e.getY(); Graphics g = getGraphics();
// color is not a property of the class Color.

// g.setColor(Color.color);
// What you want is to get the color from the // instance variable...


g.setColor(color);
g.drawLine(last_x,last_y,x,y); last_x = x; last_y = y; } }); }
// In actionPerformed you now declare and assign // *local* Color-variables, but that you don't want to, // as mouseDragged above won't see them...


// You want to assign a new value to the common instance // variable instead, as I have marked with // <--.


public void actionPerformed(ActionEvent e) { if(e.getSource() == Black) { color = Color.black; // <-- } else if (e.getSource() == Blue) { color = Color.blue; // <-- } else if (e.getSource() == Yellow) { color = Color.yellow; // <-- } else if (e.getSource() == Green) { color = Color.green; // <-- } else if (e.getSource() == Orange) { color = Color.orange; // <-- } }}
// Bjorn A

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
11/10/2018 12:08 -
I revised the program and no longer get theproblem with the panel but the color problem continues to persist.


Here is the revised code:

import java.applet.*;import java.awt.*;import java.awt.event.*;
class TestScribble { public static void main(String[] args){ Frame f = new Frame(Scribble); f.setSize(500,500);
Panel p = new Scribble(); f.add(p); f.setVisible(true); }}
class Scribble extends Panel implements ActionListener { int last_x,last_y; Button Black = new Button(Black); Button Blue = new Button(Blue); Button Yellow = new Button(Yellow); Button Green = new Button(Green); Button Orange = new Button(Orange);
public Scribble(){ setSize(500,500); setLayout(new FlowLayout()); add(Black); add(Blue); add(Yellow); add(Green); add(Orange); Black.addActionListener(this); Blue.addActionListener(this); Yellow.addActionListener(this); Green.addActionListener(this); Orange.addActionListener(this); this.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ last_x = e.getX(); last_y = e.getY(); } }); this.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ Graphics g = getGraphics(); int x = e.getX(), y = e.getY(); g.setColor(Color.color); g.drawLine(last_x,last_y,x,y); last_x = x; last_y = y; } }); } public void actionPerformed(ActionEvent e){ if(e.getSource()== Black){ color = Color.black; } else if (e.getSource() == Blue) { color = Color.blue; } else if (e.getSource() == Yellow) { color = Color.yellow; } else if (e.getSource() == Green) { color = Color.green; } else if (e.getSource() == Orange) { color = Color.orange; }
} }

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
11/10/2018 12:08 - Thanx Bjorn that helped and at least the program compiled but I'mstill having a problem with the line: g.setColor(Color.color);
If I change color to black or any other color the programcompiles and runs but the buttons do not function. Itjust scribbles in what ever color I set.


If I leave as shown the program does not compileand I get the errror message:
color cannot be resolved. I have the problemmarked off *** ****
import java.applet.*;import java.awt.*;import java.awt.event.*;
class TestScribble { public static void main(String[] args){ Frame f = new Frame(Scribble); f.setSize(500,500); Panel p = new Scribble(); f.add(p); f.setVisible(true); }}
class Scribble extends Panel implements ActionListener { int last_x,last_y; Button Black = new Button(Black); Button Blue = new Button(Blue); Button Yellow = new Button(Yellow); Button Green = new Button(Green); Button Orange = new Button(Orange); public Scribble(){ setSize(500,500); setLayout(new FlowLayout()); add(Black); add(Blue); add(Yellow); add(Green); add(Orange); Black.addActionListener(this); Blue.addActionListener(this); Yellow.addActionListener(this); Green.addActionListener(this); Orange.addActionListener(this); this.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e){ last_x = e.getX(); last_y = e.getY(); } }); this.addMouseMotionListener(new MouseMotionAdapter(){ public void mouseDragged(MouseEvent e){ int x = e.getX(); int y = e.getY(); Graphics g = getGraphics(); ***g.setColor(Color.color);*** g.drawLine(last_x,last_y,x,y); last_x = x; last_y = y; } }); } public void actionPerformed(ActionEvent e){ if(e.getSource()== Black){ Color color = Color.black; } else if (e.getSource() == Blue) { Color color = Color.blue; } else if (e.getSource() == Yellow) { Color color = Color.yellow; } else if (e.getSource() == Green) { Color color = Color.green; } else if (e.getSource() == Orange) { Color color = Color.orange;
} } }

Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below