Why is there no red rectangle showing up when I run this program? I am using a JFrame and a Canvas. Here is my source code:
package javagame;
public class Launcher {
public static void main(
String[] args) {
Frame d = new Frame();
}
}
=============================================================
package javagame;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Frame {
public JFrame frame;
public Canvas canvas;
public Graphics g;
public Frame() {
createDisplay();
}
public void createDisplay() {
frame = new JFrame("Title");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(300, 300));
frame.add(canvas);
g = canvas.getGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 300, 300);
}
}