swing + doublebuffer

hey, i am quite new to java.
prob is: java doesn’t display swing button1
perhaps u know any gd solution

thanks sQ

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.VolatileImage;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Window
{
    private boolean run = true;
       
    public Window()
    {    
        JFrame f = new JFrame("LevelCreator"); 
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        f.setSize(800, 600); 
        f.setVisible(true); 
        f.setResizable(false);
        
        final Icon icon2 = new ImageIcon(  
        		  Window.class.getResource( "/pattern/block.png" ) ); 
        		 
        		final JButton button1 = new JButton( icon2 ); 
        		button1.setLocation(200,200);
        		button1.setDoubleBuffered(true);
        		button1.setVisible(true);
        		f.add( button1 );
        
        Main main = new Main(f);
        
        Map<RenderingHints.Key, Object> config = new HashMap<RenderingHints.Key, Object>();
        config.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        config.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
        config.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
        config.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        config.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
        config.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
        config.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        config.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        config.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
           
        f.addKeyListener(new KeyListener()
                {

            public void keyPressed(KeyEvent e) 
            {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) 
                {
                	run = false;
                    System.exit(0);
        		}
            }

            public void keyReleased(KeyEvent arg0) {}

			public void keyTyped(KeyEvent arg0) {}
              
                });
           
        int fps_counter = 0;
        long t_start = System.currentTimeMillis();
        long t_current = 0, fps = 0;
        
    	Graphics2D buffered = (Graphics2D) f.getGraphics();
    	buffered.setRenderingHints(config);
    	
    	VolatileImage img = f.createVolatileImage(Statics.width, Statics.height);
    	Graphics2D g = (Graphics2D) img.createGraphics();
    	g.setRenderingHints(config);
    
    	while(run)
    	{
    		g.setColor(Color.BLACK);
        	g.fillRect(0, 0, Statics.width, Statics.height);

    		if(t_current > t_start + 1000)
    		{
    			t_start = System.currentTimeMillis();
    			fps = fps_counter;
    			fps_counter = 0;
    		}
            
    		++ fps_counter;
         
    		main.paint(g);
			g.setColor(Color.red);
			g.drawString("fps: " + fps, 100, 200);
			
    		buffered.drawImage(img, 0, 10, f);
    		t_current = System.currentTimeMillis();
    	}
    }
       
    public static void main(String[] args)
    {
    	try
    	{
    		new Window();
    	}
		catch(Exception e ) {System.exit(0);}
    }
}

You are mixing 2 differente things :

  • passive rendering (the button)
  • active rendering (main.paint(g))

The button is paint one time at the begging and then it is erased by your active rendering. I didn’t test it but try to do a f.paintComponents(g) at each frames.

it is working fine now

thank you!!!

sQ