[EMAIL PROTECTED] wrote:

graphics using a canvas have always given me trouble, so I thought I'd investigate where I'm going wrong. So far, no progress. any suggestions?

[code snipped]


Ouch. Well, the major problem you're having is that you're overriding the paint() method of the applet, and then covering the applet with a Canvas.

The Canvas is the thing that wants to draw. Override Canvas and change -its- paint method.

Along the way - get rid of all your Graphics and offscreen-image variables. You shouldn't ever save Graphics - just use the one passed into paint(), and only use it inside that method. Your local variables are overriding your class-members much of the time anyway.

I had some time this morning, so I simplified your example to concentrate on the drawing aspects. The attached code produces a JFrame that contains a "maze", the top of which is a Canvas class that knows how to draw itself as a black grid on a yellow background. Use in good health.

Grant
--
In theory, there is no difference between Theory and Practice.
In practice, there is no relationship between Theory and Practice.
import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;

import javax.swing.JFrame;

class MyCanvas extends Canvas {
  /**maze object we'll use for our data model */
  protected maze _holder;
  public MyCanvas (maze m) {
    super();
    _holder = m; // I'd use this as a way to get at your real maze data
  }
  public void paint(Graphics g) {
    Dimension d = this.getSize();
    g.setColor(new Color(255,255,0));
    g.fillRect(0,0,d.width, d.height);

    g.setColor(new Color(0,0,1));
    for (int col=0; col<d.width; col+=10) {
      g.drawLine(col,0,col,d.height);
    }
    for (int row=0; row<d.width; row+=10) {
      g.drawLine(0,row,d.width,row);
    }
  }
}

public class maze extends Applet
{
  TextArea t=new TextArea("",25,25,TextArea.SCROLLBARS_VERTICAL_ONLY);
  
  public void init()
  {
    // Build the GUI
    MyCanvas c=new MyCanvas(this);
    repaint();
    Panel p=new Panel();
    t=new TextArea();
    p.setLayout(new GridLayout(2,1));
    //c.setSize(200,200);
    c.setVisible(true);
    p.add(c);
    p.add(t);
    this.add(p);

    // Create the maze
    createmaze();
  }
  
  public void createmaze()  {  }
  
  public static void main(String args[])
  {
      try {
        maze a =(maze) Class.forName("maze").newInstance();
        a.init();
        
        JFrame f=new JFrame("hurl");
        f.add("Center",a);
        f.pack();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
      } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    
  }
  
}
_______________________________________________
Juglist mailing list
[EMAIL PROTECTED]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to