Painting on jpanel
I think this is the correct place to post this.
I have 3 classes that. The program runs, and i can change the background color on the custom jpanel. How do i get the graphics from the class baseCharacter to paint on the customer jpanel, mPanel? baseCharacter simply draws a red circle, when the program loads i can brifely see it before mPanel covers it, so im not sure if it is painting it on the mPanel and then the background color covers it, or if its on the frame and the mPanel is covering it. I want it to print on the jpanel and remain on top when the background color changes.Any help is greatly appreciated
(note: i left off imports to make it a little short for the post)
publicclass baseCharacter
{
privateint xPos;
privateint yPos;
privateint w;
privateint h;
public baseCharacter()
{
initCharacter(0, 0, 0, 0);
}
publicvoid initCharacter(int ix,int iy,int iw,int ih)
{
xPos = ix;
yPos = iy;
w = iw;
h = ih;
}
publicvoid drawChar(Graphics g)
{
g.setColor(Color.red);
g.fillOval(this.xPos, this.yPos, this.w, this.h);
}
}
class mPanelextends JPanel
{
private Color bgc;
mPanel()
{
bgc = Color.blue;
setPreferredSize(new Dimension(150, 150));
}
publicvoid paintComponent(Graphics g)
{
super.paintComponent(g);
setBackground(bgc);
}
publicvoid setBgColor(Color col)
{
bgc = col;
repaint();
}
}
publicclass charTestextends JFrameimplements ActionListener
{
private baseCharacter bc1;
private JButton chColor;
private mPanel mp =new mPanel();
public charTest()
{
super("PANEL TEST");
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(mp);
chColor =new JButton("Color");
chColor.addActionListener(this);
c.add(chColor);
pack();
setVisible(true);
super.addWindowListener(new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.exit(0);
}
});
}
publicvoid paint(Graphics g)
{
if(bc1 !=null)
{
bc1.drawChar(g);
}
}
publicvoid makeChar()
{
bc1 =new baseCharacter();
bc1.initCharacter(75, 75, 40, 40);
}
publicstaticvoid main(String args[])
{
charTest ct =new charTest();
ct.makeChar();
}
publicvoid actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Color"))
{
mp.setBgColor(Color.green);
}
}
}
[5661 byte] By [
java0359a] at [2007-11-27 5:55:04]

# 1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Your gui class with user interaction/event code */
public class CharTest extends JFrame implements ActionListener
{
private BaseCharacter bc1 = new BaseCharacter();
private JButton chColor;
private CharPanel mp = new CharPanel(bc1);
public CharTest()
{
super("PANEL TEST");
Container c = getContentPane();
//c.setLayout(new FlowLayout()); // poor choice
System.out.println("default layout for JFrame = " +
getLayout().getClass().getName());
c.add(mp); // default center section
chColor = new JButton("Color");
chColor.addActionListener(this);
c.add(chColor, "South");
pack();
setVisible(true);
super.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void makeChar()
{
bc1.initCharacter(75, 75, 40, 40);
}
public static void main(String args[])
{
CharTest ct = new CharTest();
ct.makeChar();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Color"))
{
mp.setBgColor(Color.green);
}
}
}
/** This can be your model */
class BaseCharacter
{
private int xPos;
private int yPos;
private int w;
private int h;
public BaseCharacter()
{
initCharacter(0, 0, 0, 0);
}
public void initCharacter(int ix, int iy, int iw, int ih)
{
xPos = ix;
yPos = iy;
w = iw;
h = ih;
}
public void drawChar(Graphics g)
{
g.setColor(Color.red);
g.fillOval(this.xPos, this.yPos, this.w, this.h);
}
}
/** Your graphic component - does the drawing */
class CharPanel extends JPanel
{
BaseCharacter baseCharacter;
private Color bgc;
CharPanel(BaseCharacter bc)
{
baseCharacter = bc;
bgc = Color.blue;
setPreferredSize(new Dimension(150, 150));
}
public void paintComponent(Graphics g)
{
// Tells java to fill the background of this component
// with the default color.
super.paintComponent(g);
// If you are going to always fill it yourself with the
// two lines below you don't need the to call super. An
// alternative is to call setBackground in the constructor
// and in the setBgColor method, continue with the call to
// super and eliminate the next two lines.
g.setColor(bgc);
g.fillRect(0, 0, getWidth(), getHeight());
baseCharacter.drawChar(g);
}
public void setBgColor(Color col)
{
bgc = col;
repaint();
}
}
# 2
Don't really understand what you are trying to do but there are several problems with your code.
a) don't override the paint() method of JFrame
b) all components have a setBackground() method so there is no need to create a setBgColor() method.
c) don't set the background color in the paintComponent() method.
d) if you are trying to paint a character then the custom painting should be done in the paintComponent() method of the panel, you don't create a separate character class, the panel (or JComponent) becomes your character. Then you just add your character to the GUI like any other component.
Here is a simple painting example:
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=674807&start=3