I have several buttons in my applet, however in certain case i use fillRect function to draw my whole applet go black, but i want to leave one Jbutton visible.

The fillRect function hides my button, but the button comes visible if i move mouse cursor on top of it. Is it possible to keep the button on top of fillRect?

Make sure that your fillRect call is being made before the other UI components are being rendered. Typically that would mean making it the first statement in your paintComponent() method.

I have the button in panel, i tried panel.remove(button); panel.add(button); AFTER fillRect but it doesn't help.

No one can tell unless you post some code. Are you using JApplet or the old AWT Applet?

I'm not sure if it's JApplet(i have imports like import java.awt.* ;
import java.awt.event.* ;), how can i tell for sure?

the button is first created in Panel function like this

class app
{
  static JPanel panel1  =  new JPanel() ;
  static boolean end = false;
class GamePanel extends JPanel
                      implements ActionListener, MouseListener
{
   static JButton  newgame     =  new  newgame( "new game" ) ;

public GamePanel()
   {
      setLayout( new BorderLayout() ) ;
      app.panel1.setBackground( back_color ) ;
      newgame.addActionListener( this ) ;
      app.panel1.add( newgame ) ;
      add( "South", app.panel1 ) ;

in paint function there is a check if screen should go black:

public void paint( Graphics graphics )
   if (app.end)
		{
              graphics.setColor( Color.BLACK ) ;
	      graphics.fillRect (0, 0, 800, 600);
	      app.panel1.remove(newgame);
              app.panel1.add(newgame);
              app.panel1.setBackground( back_color.darker() ) ;
              app.panel1.setForeground(Color.PINK);
 	     }
public class GameApplet extends JApplet
{
   public void init()
   {
      getContentPane().add( new GamePanel() ) ;
   }
}

I hope that's enough to understand

Ok, it's a JApplet

GameApplet extends [B]JApplet[/B]

.
I'd recommend changing

public void paint( Graphics graphics )

to

public void paintComponent( Graphics graphics )

so that you're only overriding the paint behavior of the panel itself and not affecting the the rendering of the other components.

In paintComponent() all you really should need are these

if (app.end)	{
  graphics.setColor( Color.BLACK ) ;
  graphics.fillRect (0, 0, 800, 600);
}

After the change the applet compiles fine, but it doesn't run. Only gives errors like this:

Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError
	at java.util.Locale.hashCode(Locale.java:891)
	at java.util.Hashtable.get(Hashtable.java:334)
	at java.text.DecimalFormatSymbols.initialize(DecimalFormatSymbols.java:513)
	at java.text.DecimalFormatSymbols.<init>(DecimalFormatSymbols.java:77)
	at java.text.DecimalFormat.<init>(DecimalFormat.java:416)
	at PelikortitPanel.paintComponent(GameApplet.java:668)
	at javax.swing.JComponent.paint(JComponent.java:1029)
	at PelikortitPanel.paintComponent(GameApplet.java:669)
	at javax.swing.JComponent.paint(JComponent.java:1029)
	at PelikortitPanel.paintComponent(GameApplet.java:669)
	at javax.swing.JComponent.paint(JComponent.java:1029)
	at PelikortitPanel.paintComponent(GameApplet.java:669)
	at javax.swing.JComponent.paint(JComponent.java:1029)
.......

at line 669 i have super.paint( graphics ) ; Removing this line messes applet's graphics completely. Same happens if create seperate paintComponent function just for the check.

Your super.paint() call is causing to paintComponent() to get called again, hence the infinite loop and stack overflow.

You shouldn't really need a super.paint() call in your paintComponent() method if you are just painting the background. If anything, it would be a super.paintComponent() instead of paint(), since that is the one you are overriding.

THANK YOU!! After changing it to super.paintComponent() it works!

Glad to hear it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.