0

I feel like I went through everything I needed to do:

  • Make a graphics class that has a void called paintComponent and extends JComponent
  • Have that paintComponent void have Graphics g as a parameter, then do Graphics2D g2d = (Graphics2D) g;
  • Add the Graphics class to my JFrame

I can't find anything wrong with this, so I'm a little confused.
My code is here:

public static void main(String[] args) {
    DragonEscape game = new DragonEscape();
    frame.setTitle(title);
    frame.setSize(1000, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.add(new Graphicsa());
    frame.add(game);
}

and

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

public class Graphicsa extends JComponent {
    private static final long serialVersionUID = 1L;

    public Graphics g;

    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g.fillRect(0, 0, 1000, 500);
        g.setColor(Color.gray);
        g.fillRect(0, 0, 100, 100);
    }

}
3
  • Is anyone gonna do something
    – user8521521
    Commented Oct 7, 2017 at 16:34
  • (1-) Be patient! People answer questions when they have time. There is no guarantee when that will be.
    – camickr
    Commented Oct 7, 2017 at 16:45
  • @camickr ok. I will
    – user8521521
    Commented Oct 7, 2017 at 17:11

1 Answer 1

0
frame.add(new Graphicsa());
frame.add(game);

Only one component can be added to the CENTER of the BorderLayout of the JFrame. So your game component replaces the graphics component.

Read the Swing tutorial for Swing basics. There are sections on:

  1. How to use BorderLayout
  2. Custom Painting

that directly related to this question.

Also, why are you even trying to do graphics painting? If looks to me like you are just trying to paint the background a certain color. Just use the setBackground(...) method on your game component.

1
  • Thanks for that, I didn't realize that was a thing
    – user8521521
    Commented Oct 7, 2017 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.