0

I am trying to compile example 1-2 from the Java In A Nutshell Book that deals with Java 1.0. I get an error saying that choice cannot be converted to Button. Now, I don't know whether this is an issue of current Java not supporting the libraries that I am calling, or the something is off.

The applet is supposed to be a sort of drawing pad, and the error is at line 14 / clear_button = new Choice();

import java.applet.*;
import java.awt.*;

public class Scribble extends Applet {
    private int last_x = 0;
    private int last_y = 0;
    private Color current_color = Color.black;
    private Button clear_button;
    private Choice color_choices;

public void init(){
    this.setBackground(Color.white);

    clear_button = new Choice();
    clear_button.setForeground(Color.black);
    clear_button.setBackground(Color.lightGray);
    this.add(clear_button);

    color_choices = new Choice();
    color_choices.addItem("black");
    color_choices.addItem("red");
    color_choices.addItem("yellow");
    color_choices.addItem("green");
    color_choices.setForeground(Color.black);
    color_choices.setBackground(Color.lightGray);
    this.add(new Label("Color: "));
    this.add(color_choices);
}

public boolean mouseDown(Event e, int x, int y){
    last_x = x; last_y = y;
    return true;
}

public boolean mouseDrag(Event e, int x, int y){
    Graphics g = this.setGraphics();
    g.setColor(current_color);
    g.drawline(last_x, last_y, x, y);
    last_x = x;
    last_y = y;
    return true;
}

public boolean action(Event event, Object arg) {
    if (event.target == clear_button) {
        Graphics g = this.getGraphics();
        Rectangle r = this.bounds();
        g.setColor(this.getBackground());
        g.fillRect(r.x, r.y, r.width, r.height);
        return true;
    }

    else if (event.target == color.choices) {
        if (arg.equals("black")) current_color = color.black;
        else if (arg.equals("red")) current_color = color.red;
        else if (arg.equals("yellow")) current_color = color.yellow;
        else if (arg.equals("green")) current_color = color.green;
        return true;
    }

    else return super.action(event, arg);

 }
}
qmesa
  • 5
  • 1
  • 3
  • 1
    Why use such an old version of Java? – Reimeus Jul 17 '16 at 22:08
  • do you a favor and forget about applets, they are considered as old technology with security issues and shouldnt be used anymore – JohnnyAW Jul 17 '16 at 22:19
  • *"I am trying to compile example 1-2 from the Java In A Nutshell Book.."* Get a more modern book. 1) See [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free) .. – Andrew Thompson Jul 18 '16 at 08:05
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. – Andrew Thompson Jul 18 '16 at 08:05
  • Thank you guys for your comments. The reason why I am using Java 1.0 is that I am building my knowledge of Java simultaneously with releases to get a solid understanding of how the language evolved over time and use the new features as building blocks. – qmesa Jul 18 '16 at 22:23

1 Answers1

1

This is because Choice does not extend from Button. The type is incompatible.

https://docs.oracle.com/javase/7/docs/api/java/awt/Choice.html

In order to fix this you need to change the type of clear_button to Choice like this:

private Choice clear_button;

I just checked the old java 1.0.2 documentation and there Choice is also not extending from Button:

http://web.mit.edu/java_v1.0.2/www/javadoc/java.awt.Choice.html#top

Based on your comment this.setGraphics() does not exist neither in Java 1.0.2 neither in modern Java.

Judging from your code you should replace it with this.getGraphics().

Another way to obtain a handler to Graphics would be to override the:

public void paint(Graphics g)

You can override this method in your class.

Alexander Petrov
  • 9,204
  • 31
  • 70
  • Thank you, Alexander. I fixed the issue, but a whole set of new issues popped up. Right now, the first error deals with Graphics g = this.setGraphics() The error is cannot find symbol. Any ideas? – qmesa Jul 17 '16 at 21:57
  • Well I dont think there is such method either declared in this ( Applet). – Alexander Petrov Jul 17 '16 at 22:05
  • This this.setGraphics() is non sense . Replace it with this.getGraphics() and also if this is copy paste from your book. Buy a new book please! Don't torture yourself. – Alexander Petrov Jul 17 '16 at 22:12
  • I got the code to compile with javac but now it does not show in the browser after implementing in in an HTML file. Ah well, maybe it is indeed to old to even work. – qmesa Jul 18 '16 at 22:24