1

When I run this program all I see is a blank JFrame. I have no idea why the paintComponent method isn't working. Here's my code:

package com.drawing;

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

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPaint extends JPanel {

    private void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.YELLOW);
        g.fillRect(50, 50, 100, 100);
    }

    public static void main(String[] args) {
        My paintTester = new MyPaint();
        paintTester.go();
    }
}
nachokk
  • 14,363
  • 4
  • 24
  • 53
Billy Jean
  • 35
  • 6
  • use `this` instead of `panel` in `go()` – felix-eku Jul 14 '13 at 19:55
  • 1
    Also, why not have the JFrame and JPanel in two different classes? – H3XXX Jul 14 '13 at 19:58
  • Just a suggestion, instead of setting size on the `JFrame`, it would be wise if you override [__getPreferredSize()__](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getPreferredSize()), like you overriding `paintComponent()` method and call `frame.pack()` – nIcE cOw Jul 14 '13 at 20:02
  • [For example](http://stackoverflow.com/a/17573406/1057230) – nIcE cOw Jul 14 '13 at 20:09

2 Answers2

4

You have to do this

private void go() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.add(this);
        frame.pack();
        frame.setVisible(true);
    }

But i would refactor your class and separate responsabilities..

go() shouldn't be declared in this class

public class MyPaint extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.YELLOW);
        g.fillRect(50, 50, 100, 100);
    }

}

And in another class

// In another class 
public static void main(String[] args) {
    JPanel paintTester = new MyPaint();
    JFrame frame = new JFrame();
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.add(paintTester);
    frame.pack();
    frame.setVisible(true);
}

Or if you only gonna use this panel in one site you can take approach of anonymous classes

     JFrame frame = new JFrame();
     frame.add(new JPanel(){
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.YELLOW);
            g.fillRect(50, 50, 100, 100);
        }

     });
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • That worked!! Thank you! But what exactly is "this" referring to in this case? Is it referring to the JPanel that I made a new object out of earlier? And what's wrong with my class structure? Sorry I'm sort of a noob when it comes to designing programs in a good OO way. – Billy Jean Jul 14 '13 at 19:59
  • @BillyJean i edit my answer, hope it helps.. `this` refers to the instance of `this class` in your case the class you created – nachokk Jul 14 '13 at 20:02
2

You're adding a plain JPanel to your JFrame which does not contain your custom paint logic. Remove

JPanel panel = new JPanel();

and add

frame.add(this);

but better to maintain 2 classes: a main class and a custom JPanel with paint logic for separation of concerns.

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Both suggestions worked! Thanks a lot!! But what exactly was I doing wrong? Does Vanilla mean that it's an original JPanel? – Billy Jean Jul 14 '13 at 20:00
  • You were adding a different component which did not implement your custom paint logic (as aboove). btw: _vanilla_ means [plain](http://www.thefreedictionary.com/plain+vanilla), in this case, no custom painting implemented. – Reimeus Jul 14 '13 at 20:02