llamaCaraDara
llamaCaraDara

Reputation: 105

Cannot get this blasted rectangle to draw!! (java swing)

Class one:

public class Bar extends JComponent {

    // instance variables - replace the example below with your own
    private static boolean litUp = false;
    private static boolean vertical = true;
    private static int positionX;
    private static int positionY;
    private static int sizeX;
    private static int sizeY;
    private static Color color;

    public void paintComponent(Graphics g) {

        System.out.println("I am being called");
        positionX = 50;
        positionY = 30;
        vertical = true;

        if(vertical == true) {
            sizeX = 10;
            sizeY = 30;
            if(litUp == true)
            {
                color = Color.red;
            }
            else
            {
                color = Color.black;
            }
        } else{

            sizeX = 30;
            sizeY = 10;
            if(litUp == true)
            {
                color = Color.red;
            }
            else
            {
                color = Color.black;
            }
        }
        g.fillRect(positionX, positionY, sizeX, sizeY);
        g.setColor(color);
        super.paintComponent(g);
    }
}

Class Two:

public class TestingBar {

    public static void main(String[] args) {

        final int FRAME_WIDTH = 317;
        final int FRAME_HEIGHT = 415;

        //created frame and panel. Panel layout is taken away from the flow layout.
        final JFrame myFrame = new JFrame();
        myFrame.setTitle("Tester Window (v2)");
        myFrame.setSize(FRAME_WIDTH,FRAME_HEIGHT);
        final JPanel myPanel = new JPanel();
        myPanel.setLayout(null);

        final Bar testBar = new Bar();

        //myPanel.add(testBar);
        myFrame.getContentPane().add(testBar);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setVisible(true);

        myPanel.add(testBar);
    }
}

This is just giving me an empty JFrame. And the print statement is never being utilized in the paintComponent method. I am seriously stumped.

PS: They won't let me post this without typing more, so I will talk about turtles. I don't understand how they survive out in the wild. They can't run, their shells really aren't that hard, they can only attack from the front, and if they are flipped over they are crippled for an indefinite amount of time. You think that natural selection would have worn them out of the pool by now.

Maybe they survived by being so darn adorable. I mean, just watch youtube videos of a baby turtle trying to eat a cherry tomato. It is the epitome of cute.

Upvotes: 0

Views: 59

Answers (4)

Beggarstune
Beggarstune

Reputation: 11

Call super.paintComponents first thing. You probably want to cast g to a Graphics2D object, too.

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D)g;

Wrong order -- set the color, then draw the rectangle (using g2):

g2.setColor(color);
g2.fillRect(positionX, positionY, sizeX, sizeY);

Upvotes: 1

Dave
Dave

Reputation: 888

I turned your Bar class into a JPanel and ovrrode the paint method instead and it worked but the other comments are correct, you're not adding the panel to the frame, just the bar object:

    //created frame and panel. Panel layout is taken away from the flow layout.
    final JFrame myFrame = new JFrame();
    myFrame.setTitle("Tester Window (v2)");
    myFrame.setSize(FRAME_WIDTH,FRAME_HEIGHT);

    final Bar testBar = new Bar();

    myFrame.getContentPane().add(testBar);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setVisible(true);

and in bar:

 public class Bar extends JPanel
 {
 //....

     public void paint(Graphics g)
     {
          g.setColor(Color.black);
          g.fillRect(20,20,20,20);
     }
 }

Upvotes: 1

martinez314
martinez314

Reputation: 12332

You are adding testBar to your frame then again to your myPanel which removes it from the frame (a component can only have one parent). But since myPanel is not added to anything, it never gets called.

Commenting out this line will fix it, but you probably are intending to do something meaningful with your myPanel, so check your design.

//myPanel.add(testBar);

Upvotes: 1

nkoniishvt
nkoniishvt

Reputation: 2521

I think you should call super.paintComponent(g); at the beggining of the function. It probably paints over your rectangle.

Upvotes: 1

Related Questions