• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

About JButton

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic