user1949713
user1949713

Reputation: 101

Draw lines on jpanel

I want to make it like Draw a ruler (line with tick marks at 90 degree angle) just not on jframe but on jpanel.

So I tried:

JFrame f = new JFrame();
JPanel ff = new JPanel();

ff.add(new JComponent() {
...
});

f.add(ff);
...

but I failed. :( How to?

Upvotes: 2

Views: 11223

Answers (1)

Mike
Mike

Reputation: 2434

You can simply override paintComponent(Graphics g){} for ff and draw your within that method.

i.e.

JPanel ff = new JPanel(){ 
    public void paintComponent(Graphics g){
        // Draw what you want to appear on your JPanel here.
        // g.drawLine(blah blah blah), etc.
    }
};

In which case you have no need for this...

ff.add(new JComponent() {
    ...
});

You don't need this generic component unless you want to implement the custom component as suggest in the link you provided. In the case that you do want to create such a custom component, then you don't need ff, since a JFrame is already a container that can hold your component.

Upvotes: 4

Related Questions