Jump to content
  • Advertisement
Sign in to follow this  
Esqulax

[java] Componant not being displayed

This topic is 3608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya New to all this.. teaching myself... (yadda yadda) I have this basic code, it compiles OK, but when it runs, the frame appears, but the rectangle doesnt, if anyone can spot the flaw, it would be awesome
package app1;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class MainClass extends JFrame {

    public static void main(String[] args){
new MainClass();
    }

public MainClass() {
 this.setSize(800, 800);
 this.setTitle("Awesome");
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 
this.add(new DrawIt());
 
 this.setVisible(true);
}

private class DrawIt extends JComponent{

    public void Paint(Graphics g){

        Graphics2D g2 = (Graphics2D)g;

        Shape s = new Rectangle2D.Float(10,10,40,40);
        g2.setPaint(Color.RED);
        g2.draw(s);
}
    
}
}

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure if this is the problem, but you are supposed to add your components to the content pane instead of the frame when you use swing?

try replacing "this.add(new DrawIt());" with "this.getContentPane().add(new DrawIt());"

Share this post


Link to post
Share on other sites
Same result from that code unfortunatly

I thought that, but apparently it is possible to draw directly to the Frame, just not very efficient.

I've also tried filling the rect with a random colour, to be sure that its not just the same colour as the BG, but to no avail :s

Share this post


Link to post
Share on other sites
Ok, well, on a whim i changed:

public void Paint(Graphics g)

to

public void paintComponent(Graphics g)

And it worked :)

Thanks for replying :)

Share this post


Link to post
Share on other sites
Actually, changing:

public void Paint(Graphics g)

to

public void paint(Graphics g)

should work too.

Regards
elFarto

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!