I have a code that play's an animation on a JFrame.. On top of that, I have added a Jpanel with few buttons. my problem is, mybuttons are not visible on the animation screen until i hover a mouse on them?? what am i doing wrong..

Recommended Answers

All 7 Replies

Seeing your code might help, however I can take a couple of guesses.

1. You haven't done pack() before doing setVisible(true)
2. You are "playing" the animation in the vent thread, thereby delaying/preventing the operation of the GUI

hey thanks.. yes i did not put the pack() , even so still it does not work. so what is a vent thread...if this helps I'm running the animation in a new thread.... sorry I can't post the code too many classes and lines..

If you can't post the code, no one call say what you are doing wrong. Do you think we can read it over your shoulder?

It was suppossed to be "event" thread. And if you don't know what it is, then I can guarantee you're doing it. Post your code.

It was suppossed to be "event" thread. And if you don't know what it is, then I can guarantee you're doing it. Post your code.

he said he's running it in a new thread so he probably isn't doing in the event thread

still post your code

this is the first class Animate1...bare with it please i've done a lot of editing to minimize the code..hope it helps

import java.awt.*;
import org.omg.PortableInterceptor.SUCCESSFUL;
 
  public class Animate1 extends Main1
    implements Runnable {
   
   private Image[] picture2 = new Image[2];

   private Thread runner;
   private ImageLoader loader;
   private boolean resume = false;
   private Thread thisThread;
  
   public Animate1(String myvar) {
          super(myvar);
          loader = new ImageLoader();
          for (int i = 0; i < 2; i++) {
           
               loader.Loader(myvar + Fids[i] + "_image.GIF", this);
               picture2[i] = loader.image;
                }
 
   }

   public void paint(Graphics screen) {
    Graphics2D screen2D = (Graphics2D) screen;
    if (mypicture[current] != null){
         super.paint(screen);
         screen2D.drawImage(picture2[current], 300,100,200,200, this);
                     
         }
  
   }

 public void buttonPauseMouseClicked(java.awt.event.MouseEvent evt) {
    if(resume)
        Resume();
    else
        Pause();
    resume = !(resume);
         // TODO add your handling code here:
    }

 public void start() {
     if (runner == null) {
       runner = new Thread(this);
       runner.start();
     }
   }

 public void Pause(){
       thisThread.suspend();
       buttonPause.setText("Resume");
 }

 public void Resume(){
      thisThread.resume();
      buttonPause.setText("Pause");
}
  
 public void run() {
     thisThread = Thread.currentThread();
     while (runner == thisThread) {
      repaint();
        current++;
       if (current >= 2){
         current = 0;
             }
       try {
      //   Thread.sleep(pause);
           Thread.sleep(100);
       } catch (InterruptedException e) { }
     }
   }

   public void stop() {
     if (runner != null) {
       runner = null;
     }
   }
  }

]
this is the second class Main1, i have made the programm run from there...help ...
[
import java.awt.*;
import javax.swing.*;
public class Main1 extends Frame{
  
    Image groupIconImage;
    protected String var;
    protected Image[] mypicture = new Image[2];
    protected String  [] Fids = {"a", "u"};
       protected JButton buttonPause;
     public int current = 0;
    /** Creates a new instance of Main */
    public Main1(String var) {
        JFrame y = new JFrame();
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(new Dimension(1024,768));
        this.setResizable(false);
        this.setLocationRelativeTo( null );   
        //this.Fidel = fidel;
        JPanel panel = new JPanel();             
            panel.setLayout(null);
            panel.setOpaque( false );
            panel.setPreferredSize( new Dimension(1024,768) );

            buttonPause = new JButton( "Pause" );
            buttonPause.setBackground(new java.awt.Color(102,50, 255));
            buttonPause.setFont(new java.awt.Font("Gills sons MT", 3, 14));
            buttonPause.setForeground(new java.awt.Color(51, 0, 204));
            buttonPause.setBorder(new javax.swing.border.CompoundBorder());
            buttonPause.setBounds(680,600,100,40);
            buttonPause.setFocusPainted(true);
            buttonPause.setFocusable(true);
            buttonPause.setOpaque(false);
            buttonPause.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
              buttonPauseMouseClicked(evt);
            }
        });
          
              
        panel.add( buttonPause );
        buttonPause.setVisible(true);
        super.add(panel);
        panel.setVisible(true);
        this.pack();
     Images(var);
   
                 
    }
    
    public void Images(String var){
        
          this.var = var;
          ImageLoader loader = new ImageLoader();
                    loader.Loader(this.var + Fids[0] + "_image.GIF", this);
                 mypicture[0] = loader.image;
          
         
                         
    }
     public void buttonPauseMouseClicked(java.awt.event.MouseEvent evt) {
         
   }
                
        
       public void paint(Graphics g) {
           g.drawImage(mypicture[0], 300,100,200,200, this);
                 
          }
          
       public static void main(String[] args) {
            Animate1 x = new Animate1("h");
            x.start();
            x.setBackground(new Color(100,100,199));
            x.pack();
            x.setVisible(true);
            
        }
}

You have overridden paint() but you are not calling paintComponents(), which is responsible for rendering the components in the container.

Painting on the frame directly often lends itself to troublesome behavior like this if you are not careful to preserve the top-level container rendering processes. It is much easier to instead use a JPanel for this within the frame and override paintComponent() (not paint()) on the JPanel for your drawing code.

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.