[2827] in java-interest

home help back first fref pref prev next nref lref last post

What does this error mean?

daemon@ATHENA.MIT.EDU (WAVELL RUFUS SEATON)
Mon Oct 16 23:43:34 1995

Date: Mon, 16 Oct 1995 19:48:03 -0600
From: WAVELL RUFUS SEATON <wavell@columbus.Colorado.EDU>
To: java-interest@java.sun.com

When I try and compile the following code I get this error:

CNDemo.java:71: Can't make a static reference to non static variable 
canvas in class CNDemo.
      canvas.getElem(i);
      ^
1 error

What does the error mean, and why can't I reference my canvas directly?

TIA, Bindu

P.S. Has anyone figured out how to create thick lines? I would like to draw
lines that are thicker than 1 pixel. I have two solutions, but they are slow,
one uses a filled pollygon, the other draws several lines... Neither are
very good or fast :(

------ SNIP ----------------------------------------------------------------
/*
 * CNDraw.java                    10/13/95                      Bindu Wavell
 *
 * Implements a drawing package that is audio enabled for the ClassNotes+
 * project.
 */

import java.applet.Applet;
import java.awt.*;
import java.util.Vector;

/*
 * helper class that contains a segment of a drawing
 */
class Segment extends Object{
  private int   x1;
  private int   y1;
  private int   x2;
  private int   y2;
  private int   w;
  private Color c;

  Segment(int cx1, int cy1, int cx2, int cy2, int cw, Color cc) {
    x1 = cx1; y1 = cy1; x2 = cx2; y2 = cy2; w  = cw; c  = cc; 
  }

  public int   getX1()    { return x1; }
  public int   getY1()    { return y1; }
  public int   getX2()    { return x2; }
  public int   getY2()    { return y2; }
  public int   getWidth() { return w; }
  public Color getColor() { return c; }
}

/**
 * An applet class that implements a Drawing interface with synchronized
 * audio content for the ClassNotes+ Project at the Univerisy Of Colorado 
 * At Boulder.
 *
 * NOTE: drawing is an int since multiple mouse buttons can be pressed 
 *       and so multiple mouse down events can occur before a mouse up
 *       event. This method will assume mouse down if any button is down
 *       so if button 1 is pressed, then button 2, then button 1 is 
 *       released, the mouse is still considered down untill mouse 2
 *       is released.
 */
public class CNDraw extends Applet{

  CNDrawCanvas canvas;
  int          x = 0;
  int          y = 0;
  int          drawing = 0;

  /**
   * Initialize CNDraw applet
   */
  public void init() {
    /* create simple layout for CNDraw */
    setLayout(new BorderLayout());

    /* create canvas for displaying demos */
    canvas = new CNDrawCanvas();

    /* create simple control bar */
    Panel   pBar    = new Panel();
    Panel   pColors = new Panel();
    Panel   pSizes  = new Panel();

    /* add buttons to the color section */
    pColors.add(new colorButton("Black",  Color.black));
    pColors.add(new colorButton("White",  Color.white));
    pColors.add(new colorButton("Red",    Color.red));
    pColors.add(new colorButton("Green",  Color.green));
    pColors.add(new colorButton("Blue",   Color.blue));
    pColors.add(new colorButton("Yellow", Color.yellow));
    pColors.add(new colorButton("Gray",   Color.lightGray));
    pColors.add(new Button("Zoom"));

    /* add radio buttons to size section */
    CheckboxGroup cbg = new CheckboxGroup();
    pSizes.add(new Label("Sizes: "));
    pSizes.add(new Checkbox("1",  cbg, true ));
    pSizes.add(new Checkbox("2",  cbg, false));
    pSizes.add(new Checkbox("4",  cbg, false));
    pSizes.add(new Checkbox("8",  cbg, false));
    pSizes.add(new Checkbox("16", cbg, false));
    
    pBar.setLayout(new GridLayout(2, 0));
    pBar.add(pColors);
    pBar.add(pSizes);
    pColors.reshape(2,2,2,2);
    pSizes.reshape(2,2,2,2);

    /* create the final interface */
    add("Center", canvas);
    add("South",  pBar);

  } /* end init method */

  /**
   * main program startup code
   */
  public static void main(String args[]) {
    Frame  fr     = new Frame("CNDrawApplet");
    CNDraw cnDemo = new CNDraw();
   
    cnDemo.init();
    cnDemo.start();

    fr.add("Center", cnDemo);
    fr.resize(300, 300);
    fr.show();

  } /* end main method */
  
  /* main event handler for CNDraw */
  public boolean handleEvent(Event e) {
    if(e.target instanceof colorButton) {
      canvas.setDefaultColor(((colorButton)e.target).getColor());
      canvas.repaint();
    } else if(e.target instanceof Checkbox) {
      if(e.id == e.ACTION_EVENT) {
        Checkbox cb = (Checkbox)(e.target);
        canvas.setDefaultWidth(Integer.parseInt(cb.getLabel()));
      }
    } else if(e.target instanceof CNDrawCanvas) {
      switch(e.id) {
        case Event.MOUSE_DOWN:
          drawing++;
          /* only start a segment if this is the first mouse down */
          if(drawing == 1) {
            x = e.x;
            y = e.y; 
          }
          break;
        case Event.MOUSE_DRAG:
          if(drawing > 0) {
            canvas.addSegment(new Segment(x, y, e.x, e.y, canvas.getWidth(), 
              canvas.getColor()));
            x = e.x;
            y = e.y; 
            canvas.repaint();
          }
          break;
        case Event.MOUSE_UP:
          drawing--;
          break;
      }
    } else if (e.target instanceof Button) {
      Button bt = (Button)(e.target);
      if (bt.getLabel() == "Zoom") {
        System.out.println("Got Zoom Click");
      }
    }

    /* return super.handleEvent(e); */
    return true;
  }

} /* end CNDraw class */

class CNDrawCanvas extends Canvas {
  String  sMessage        = "...Demo v0.2...";
  Color   cColor          = Color.black;
  int     iWidth          = 1;
  Vector  picture         = new Vector();

  public void paint(Graphics g) {
    int   w = size().width;
    int   h = size().height;

    super.paint(g);

    Segment sg;
    int   pn = picture.size();
/*
    int xPoints[] = {0, 0, 0, 0};
    int yPoints[] = {0, 0, 0, 0};
    double slope;
    double xOffset;
    double yOffset;
*/
    for (int i = 0; i < pn; i++) {
      sg = (Segment)(picture.elementAt(i));
      g.setColor(sg.getColor());
      
      g.drawLine(sg.getX1(), sg.getY1(), sg.getX2(), sg.getY2());
    }

    /* Display message in the Center of the window */
    g.setColor(cColor);
    Font fn  = g.getFont();
    g.setFont(new Font(fn.getName(), fn.getStyle() + fn.BOLD, 
      fn.getSize() + 15));
    FontMetrics fm = g.getFontMetrics();
    int x = (w - fm.stringWidth(sMessage))/2;
    int y = h/2;
    g.drawString(sMessage, x , y);

  } /* end paint method */
  
  public void setMessage(String msg) { sMessage = msg; }
  public void setDefaultColor(Color clr) { cColor = clr; }
  public void setDefaultWidth(int width) { iWidth = width; }
  public void addSegment(Segment sg) { picture.addElement(sg); }
  public int getWidth() { return iWidth; }
  public Color getColor() { return cColor; }
  public Dimension minimumSize() { return new Dimension (20, 20); }
  public Dimension preferedSize() { return new Dimension (300, 300); }

} /* end CNDrawCanvas class */

class colorButton extends Button {
  Color cButton;

  public colorButton(String lbl, Color clr) {
    super(lbl);

    cButton = clr;
    setBackground(cButton);
 
    /* !!! an attempt to hack buttons to have nice font color */
    if ((clr == Color.blue) || (clr == Color.black)) {
      setForeground(Color.white);
    } else {
      setForeground(Color.black);
    }
  }

  public Color getColor() {
    return cButton;
  }

} /* end colorButton class */
------ SNIP ----------------------------------------------------------------
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post