wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes About JButton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "About JButton" Watch "About JButton" New topic
Author

About JButton

chaitanya thakur
Greenhorn

Joined: Apr 02, 2002
Posts: 2
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);
}

if((val2<=1000))
{
g.setColor(color[val3]);
g.fillArc(700,100,100,100,0,360);
}
else if((val2>1000 && val2<=2000))
{
g.setColor(color[val4]);
g.fillRoundRect(700,100,100,100,15,15);
}
else if ((val2>2000 && val2<=2999))
{
g.setColor(color[val5]);
int xval[]={700,900,800,700,900};
int yval[]={100,100,150,200,200};
num=5;
g.fillPolygon(xval,yval,num);
}
/*Polygon*/
if((val<=300) && (val1>700 && val1<=1300) && (val2>2000 && val2<=2999) && (val3==val4) && (val4==val5))
{
g.setColor(color[val3]);
g.drawString("You Win",400,300);
}
/*Round Rect*/
else if((val>300 && val<=600) && (val1<=700) && (val2>1000 && val2<=2000) && (val3==val4))
{
g.setColor(color[val3]);
g.drawString("You Win",400,300);
}
/* Circle*/
else if((val>600 && val<=999) && (val1>1300 && val1<=1999) && (val2<=1000) && (val3==val5))
{
g.setColor(color[val3]);
g.drawString("You Win",400,300);
}
else
{
g.setColor(Color.blue);
g.drawString("You Lose",400,300);
}
}
public void actionPerformed(java.awt.event.ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("play"));
{
repaint();
}
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
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
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
 
 
subject: About JButton
 
Similar Threads
Help need in query
c:if tag
Manfred please help
Can't accept parameters from an applet
Populate Table Rows With Data from AJAX