• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

problems using paint(Graphics g)  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever i use paint method , all the added components disappear.commenting it solves the problem.pl help me out
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post some code?
 
saurabh sood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot that really helped
 
saurabh sood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot that really helped
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!