-4

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

James Marlin
  • 13
  • 1
  • 7
  • 1
    Whats the error? `Paddle1.paint(Graphics g);` is obviously wrong. If you are overriding methods use the `@Override` annotation it will cause a compilation error if its not actually overriding the method. – Paul Rooney Jun 20 '19 at 04:19
  • I’m asking what’s the correct thing to do so I can get my desired result. – James Marlin Jun 20 '19 at 04:21
  • 1
    yes, the correct thing is (as @PaulRooney pointed out) to annotate @ Override on the overriden method in subclass. You should also include exact error/problem in your question. – Edward Aung Jun 20 '19 at 04:28
  • 1
    Welcome to SO. You actually have a number of issues with your code, including the fact that you shouldn't be calling paint yourself (it's called by the Swing display loop). Unfortunately there's no substitute for working through a tutorial (https://docs.oracle.com/javase/tutorial/uiswing/index.html) so you understand how it's supposed to work. – sprinter Jun 20 '19 at 04:30
  • IDEs don't differ too much. As long as you can type in the code and hit run, either should be fine. – Paul Rooney Jun 20 '19 at 05:18
  • @sprinter Thank you for the response. Looking through your link, is it best to download NETBEANS like the tutorial wants me to? I also started looking through the “Using Swing Components” section to find out what I need, but I was wondering about your opinion on the matter. – James Marlin Jun 20 '19 at 05:21
  • @PaulRooney Thanks – James Marlin Jun 20 '19 at 16:36

1 Answers1

-1

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);
Dan
  • 21
  • 5