Painting in JPanel


A

ahd292

Hi everyone.
I want to paint some bars with "fillrect()" in JPanel. my JPanel
component, call it panel, is inside JFrame. it means:
---JFrame
-------JPanel

In fact I want to draw bars with method "drawbar" that i create it in
my Main class that extended JFrame and have panel inside itself. i
call drawbar metohd from othe class, for example ChartGenerator.


this is my pseudo code:

Class Main extends JFrame{
JPanel panel;
//other fields

drawbar(){
//paint bars in panel with fillrect method
}

void main(){
ChartGeneratirCG = new ChartGenrator(this);
}
}

class ChartGenerator{
Main m;
public ChartGenerator(Main m){
this.m = m;
}

makechart(){
//produce chart details
this.m.drawbar();
}
}


Does I need reconstruct this class structure or this is correct.
please help me with explain how to use graphics and painting in Swing
component specially if one Comp is inside other one.
 
Ad

Advertisements

M

Mark Space

ahd292 said:
Does I need reconstruct this class structure or this is correct.
please help me with explain how to use graphics and painting in Swing
component specially if one Comp is inside other one.

This appears to be ok to me. I think your code may be a bit awkward,
but it is not incorrect. You should probably look at the MVC design
pattern to improve this.

One thing that bugs me is that "main" method in class Main looks a bit
at first like the standard "main" entry point, or a constructor. I'd
pick a different method name there, at least.

Also Class is a syntax error (should be lower case) but I assume you'll
catch that when you try to compile, no biggie.
 
Ad

Advertisements

K

Knute Johnson

ahd292 said:
Hi everyone.
I want to paint some bars with "fillrect()" in JPanel. my JPanel
component, call it panel, is inside JFrame. it means:
---JFrame
-------JPanel

In fact I want to draw bars with method "drawbar" that i create it in
my Main class that extended JFrame and have panel inside itself. i
call drawbar metohd from othe class, for example ChartGenerator.


this is my pseudo code:

Class Main extends JFrame{
JPanel panel;
//other fields

drawbar(){
//paint bars in panel with fillrect method
}

void main(){
ChartGeneratirCG = new ChartGenrator(this);
}
}

class ChartGenerator{
Main m;
public ChartGenerator(Main m){
this.m = m;
}

makechart(){
//produce chart details
this.m.drawbar();
}
}


Does I need reconstruct this class structure or this is correct.
please help me with explain how to use graphics and painting in Swing
component specially if one Comp is inside other one.

You want to draw on a JPanel, make that the class that you extend.

class BarPanel extends JPanel {
public BarPanel() {
// process parameters
// set preferred sizes
// or whatever
}

public void paintComponent(Graphics g) {
// draw my bars here
}
}

Then you can add this class to a container such as a JFrame.

You can make your class more dynamic by adding fields to specify number
of bars, color, height etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top