-1

I was working on this lab in class and when I tried changing the background color it would stay at its default of white can someone please explain where I my programming went wrong.

import javax.swing.*;
import java.awt.*;
public class  DemoPoly extends JFrame {  

// constructor
public DemoPoly() {
  // defines Frame characteristics

  int size = 300;
  setSize(size,size);
  setTitle("a random window");

  getContentPane().setBackground(Color.red);

  setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  setVisible(true);
}

public static void main (String[] args){ 
  //  instantiates a JFrame
  //  exits on close (opional)
  JFrame object = new DemoPoly();
} 

public void  paint  (Graphics g){
  // This provides the Graphics object g,  where
  // you are going to use you graphics primitive
  // to paint on the content pane of the frame.
  int[] arr = {0,100,100,0};
  int[] yarr = {0,0,100,100};
  Square object = new Square(arr,yarr,Color.red);
  AbstractPolygon randSquare = new Square(arr, yarr, Color.red);
} 
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

2 Answers2

1

I see a couple of problems in your code:

  1. Extending JFrame is like saying your class is a JFrame, JFrame is a rigid container, instead create your GUI based on JPanels. See Java Swing extends JFrame vs calling it inside of class for more information.

  2. You're breaking the paint chain by removing the super.paint(g) call on the paint(...) method. When changing your GUI to extend JPanel instead of JFrame you should use the paintComponent(...) method instead. Take the Lesson: Performing Custom Painting in Swing.

  3. You forgot to add @Override notation on the paint(...) method.

  4. You're not placing your program on the Event Dispatch Thread (EDT) which could cause threading issues.

    You can solve this by changing your main() method like this:

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                //Your constructor here
            }
        });
    }
    
  5. Instead of setting the JFrame size, override the getPreferredSize() method and call pack(). See Should I setPreferred|Maximum|MiniumSize in Java Swing?. The general consensus says yes.


Your problem gets solved by adding

super.paint(g);

on the paint(...) method:

@Override
public void paint(Graphics g) {
    super.paint(g); //Never remove this
    //Your code goes here
}

With all the above recommendations taken into account, your code should look like this now:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class DemoPoly {

    private JFrame frame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DemoPoly().createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        CustomPanel cp = new CustomPanel();
        cp.setBackground(Color.RED);
        frame.add(cp);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

class CustomPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

Which produces this output (and is the same output that you'll get with your current code but better because it gives you more control over your components)

enter image description here

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
0

I'm don't understand your question. But here is code for change your background to RED;

public class DemoPoly extends JFrame {

    public DemoPoly() {
        // defines Frame characteristics

        int size = 300;
        setSize(size, size);
        setTitle("a random window");

        //change background here
        getContentPane().setBackground(Color.red);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        //  instantiates a JFrame
        //  exits on close (opional)
        JFrame object = new DemoPoly();
    }
}

Your code is well. Maybe use @override in your paint method.

Fabio Noth
  • 85
  • 7