File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes problems using paint(Graphics g) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problems using paint(Graphics g)" Watch "problems using paint(Graphics g)" New topic
Author

problems using paint(Graphics g)

saurabh sood
Greenhorn

Joined: Nov 22, 2000
Posts: 17
whenever i use paint method , all the added components disappear.commenting it solves the problem.pl help me out
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Could you post some code?
saurabh sood
Greenhorn

Joined: Nov 22, 2000
Posts: 17
thanx for replying, this is the following code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class eswing extends JFrame implements ActionListener,MouseListener
{JMenuBar mb;
int x1,y1,x2,y2,h,w;
JMenu mn;
String s=" ";
Color r;
Container c;
public eswing()
{x1=x2=y1=y2=h=w=0;
c=getContentPane();
c.addMouseListener(this);
r=Color.black;
mb=new JMenuBar();
setJMenuBar(mb);
mn=new JMenu("Draw");
mb.add(mn);
mn.add("Line").addActionListener(this);
mn.add("Rectangle").addActionListener(this);
mn.add("Ellipse").addActionListener(this);

mn=new JMenu("Color");
mb.add(mn);

mn.add("red").addActionListener(this);
mn.add("yellow").addActionListener(this);
mn.add("green").addActionListener(this);
mn=new JMenu("Exit");
mn.addActionListener(this);
mb.add(mn);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("Line"))
s="line";
if (e.getActionCommand().equals("Rectangle"))
s="rec";
if (e.getActionCommand().equals("Ellipse"))
s="ell";
if (e.getActionCommand().equals("red"))
r=Color.red;
if (e.getActionCommand().equals("yellow"))
r=Color.yellow;
if (e.getActionCommand().equals("green"))
r=Color.green;
}
public void paint(Graphics g)
{if(s.equals("line"))
{g.setColor(r);
g.drawLine(x1,y1,x2,y2);repaint();}
if(s.equals("rec"))
{g.setColor(r);
g.drawRect(x1,y1,(x2-x1),(y2-y1));repaint();}
if(s.equals("ell"))
{g.setColor(r);
g.drawOval(x1,y1,(x2-x1),(y2-y1));repaint();}
}
public void mousePressed(MouseEvent m)
{x1=m.getX();
y1=m.getY();
}
public void mouseReleased(MouseEvent m)
{x2=m.getX();
y2=m.getY();

}
public void mouseClicked(MouseEvent m)
{}
public void mouseEntered(MouseEvent m)
{}
public void mouseExited(MouseEvent m)
{}
public static void main(String[] args)
{
eswing obj=new eswing();
obj.setTitle("Swing Excercise");
obj.pack();
obj.setVisible(true);
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Saurabh,
The reason you are seeing this behavior is because you have overridden your JFrame's paint() method. It is this method that handles the painting of the JMenuBar that belongs to the JFrame. To test this, at the end of your paint method, put a call to mb.repaint() and you will see the menu bar show up. This will not fix your problem completely though, because menus that drop down across the JFrame will still not get painted.
You could put a call to super.paint(g) at the top of your paint() method, but I'm not sure that this would be good style. Any comments on this, gang?
A better solution might be to nest a JPanel inside your JFrame and override its paint method. That way you can still exercise control over the painting of graphics in your app but the JFrame can still handle what it needs to handle.
BTW, you may want to add something like this inside your constructor
<PRE>
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
</PRE>
Otherwise you will leave a JVM running each time you test your program.
[This message has been edited by Bodie Minster (edited November 24, 2000).]
saurabh sood
Greenhorn

Joined: Nov 22, 2000
Posts: 17
thanks a lot that really helped
saurabh sood
Greenhorn

Joined: Nov 22, 2000
Posts: 17
thanks a lot that really helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems using paint(Graphics g)
 
Similar Threads
Any questions on Paint in the new exam?
Amazing test....
GUI Refresh Problem
Applet wont load...
How is the challenge ...