Moun Glow Moun Glow - 8 months ago 28
Java Question

methods not giving wanted result?

I have this program that is supposed to "

" Mickey Mouse face :

import java.awt.Canvas;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Rectangle;

public class Mickey extends Canvas {
public static void main(String[] args) {
JFrame frame = new JFrame();

Canvas canvas = new Mickey();
canvas.setSize(900, 500);

public void mickey(Graphics g) {
Rectangle bb = new Rectangle(400, 400, 100, 100);
boxOval(g, bb);

int dx = bb.width/2;
int dy = bb.height/2;

Rectangle half = new Rectangle(bb.x, bb.y, dx, dy);

half.translate(-dx/2, -dy/2);
boxOval(g, half);

half.translate(dx*2, 0);
boxOval(g, half);
public void boxOval(Graphics g, Rectangle bb) {
g.fillOval(bb.x, bb.y, bb.width, bb.height);

The program just shows nothing and is blank, is there something wrong?


You are supposed to override the paint() method as described in the javadoc

An application must subclass the Canvas class in order to get useful functionality such as creating a custom component. The paint method must be overridden in order to perform custom graphics on the canvas.

So here simply try to rename your method public void mickey(Graphics g) to public void paint(Graphics g).