This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
Hi I have attatched my code in which i am changing the images on the click of button.In this I have created a jbutton.But when I click the button everything works fine except i can`t see that button.As soon as I move the curser away from the button the button reappears there.Any answer for this problem. CODE STARTS HERE import java.awt.*; import java.applet.*; import java.util.Random; import javax.swing.*; public class stage4 extends JApplet implements java.awt.event.ActionListener { private int val; private int val1; private int val2; private int val3; private int val4; private int val5; private int xval[]; private int yval[]; private int num; private Color color[]; JButton play; public void init() { Container content=getContentPane(); ImageIcon icon = new ImageIcon("images/play.gif", "play"); /* FlowLayout lay = new FlowLayout(); content.setLayout(lay);*/ content.setLayout(new FlowLayout(FlowLayout.CENTER)); /*ASK Even if I don`t write FlowLaout.CENTER it works fine matter of fact there is no constructor like that why?*/ play=new JButton("play",icon); content.add(play); play.addActionListener(this); setBackground(Color.black); Font f=new Font("SansSerif",Font.BOLD,20); setFont(f); } public void paint(Graphics g) { Random r=new Random(); val=r.nextInt(1000); val1=r.nextInt(2000); val2=r.nextInt(3000); val3=r.nextInt(4); val4=r.nextInt(4); val5=r.nextInt(4); Color color[]={Color.red,Color.yellow,Color.green,Color.blue}; g.setColor(this.getBackground()); g.fillRect(0,0,this.getWidth()-2,this.getHeight()-2);
if ((val<=300)) { g.setColor(color[val3]); int xval[]={100,300,200,100,300}; int yval[]={100,100,150,200,200}; num=5; g.fillPolygon(xval,yval,num); } else if ((val>300 && val<=600)) { g.setColor(color[val4]); g.fillRoundRect(100,100,100,100,15,15); } else if((val>600 && val<=999)) { g.setColor(color[val5]); g.fillArc(100,100,100,100,0,360); } if((val1<=700)) { g.setColor(color[val3]); g.fillRoundRect(400,100,100,100,15,15); } else if((val1>700 && val1<=1300) && (val4<5)) { g.setColor(color[val4]); int xval[]={400,600,500,400,600}; int yval[]={100,100,150,200,200}; num=5; g.fillPolygon(xval,yval,num); } else if ((val1>1300 && val1<=1999)) { g.setColor(color[val5]); g.fillArc(400,100,100,100,0,360); }
Hi Chaitanya, Your problem is probably your layout. You might be better off using a BorderLayout instead of a FlowLayout. Also you probably want to put your graphics on a JPanel by themselves as well as a separate JPanel for your controls (JButton). Maybe something like this:
Of course you'll have to draw on your graphicPanel instead. You also may have a problem in your actionPerformed method:
Hope this helps Michael Morris SCJP2 [ April 02, 2002: Message edited by: Michael Morris ]
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Niklas Junel
Greenhorn
Joined: Dec 19, 2001
Posts: 11
posted
0
I don't know if I am in the dark, but I did the same thing once. I did a image for the button when it is in its pressed status as well for the regular status. It's just a guess, but it might help. Niklas