I want to draw in Java's Canvas but can't get it work because I don't know what I'm doing. Here's my simple code:
import javax.swing.JFrame;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Color;
public class Program
{
public static void main(String[] args)
{
JFrame frmMain = new JFrame();
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMain.setSize(400, 400);
Canvas cnvs = new Canvas();
cnvs.setSize(400, 400);
frmMain.add(cnvs);
frmMain.setVisible(true);
Graphics g = cnvs.getGraphics();
g.setColor(new Color(255, 0, 0));
g.drawString("Hello", 200, 200);
}
}
Nothing appears on the window.
Am I wrong to think that Canvas is a paper and Graphics is my Pencil? Is that how it works?