0

I'm trying to do a canvas object... is homework.. but I want to make a object

Canvas obj=new Canvas();

but i can't draw..

package canvasobjeto;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;

/**
 *
 * @author Robin
 */

public class CanvasObjeto {


    JFrame Frame=new JFrame();

    public CanvasObjeto() {

        Frame.setTitle("CanvasObjeto");
        Frame.setName("CanvasObjeto");
        Frame.setSize(300, 300);
        Frame.setResizable(false);
        Frame.setUndecorated(false);
        Frame.setLayout(null);
        Frame.setLocationRelativeTo(null);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.getContentPane().setBackground(Color.WHITE);
        Formato();
        Frame.setVisible(true);
    }


    private void Formato() {

        Canvas lien=new Canvas();
        Graphics g= lien.getGraphics();
        g.drawLine(0, 0, 20, 20);
        lien.setBounds(0, 0, 200, 200);
        lien.setBackground(Color.red);
        lien.repaint();
        Frame.add(lien);

    }



    public static void main(String[] args) {

        CanvasObjeto Ventana=new CanvasObjeto();
    }

}

this is the error... i don't know why says that if i making the obj..

Exception in thread "main" java.lang.NullPointerException at canvasobjeto.CanvasObjeto.Formato(CanvasObjeto.java:43)

WearFox
  • 293
  • 2
  • 19
  • You need a Swing Timer – Shrey Mar 15 '14 at 19:14
  • Any attempt to draw in Swing or AWT using a `Graphics` object that was obtained by calling `getGraphics` on a Component will fail in one way or the other. You should read http://docs.oracle.com/javase/tutorial/uiswing/painting/ . (And although it should be possible to mix Swing (JFrame) and AWT (Canvas), I'd recommend you *not* to do this unless you *exactly* know what you're doing, because there may be additional caveats) – Marco13 Mar 15 '14 at 20:27

0 Answers0