My Superclass is failing to use the method I created in my subclass. I get an error because I'm using the Graphics argument. What am I missing here?
I've tried the suggestions given by eclipse, however they result in more errors.
Here's my super class
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class GameScreen extends Canvas{
public GameScreen() {
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//Sets Screen
JFrame jframe = new JFrame("Game Screen");
Canvas canvas = new GameScreen();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.cyan);
canvas.setSize(1000,800);
jframe.add(canvas);
jframe.pack();
jframe.setVisible(true);
Paddle1.paint(Graphics g);
}
}
And here's my subclass
import java.awt.Color;
import java.awt.Graphics;
public class Paddle1 extends GameScreen {
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(50, 50, 40, 130);
}
}
The subclass creates a rectangle that is supposed to appear on top of the canvas
If you would like to trigger method "paint" of Paddle1, you have to modify your code as below:
JFrame jframe = new JFrame("Game Screen");
Canvas canvas = new Paddle1();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.setBackground(Color.cyan);
canvas.setSize(1000,800);
jframe.add(canvas);
jframe.pack();
jframe.setVisible(true);
canvas.paint(g);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.