0

How can I make a button appear over a double buffer image?

Window

package org.learning.learn;

import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.*;

@SuppressWarnings("serial")
public class Window extends JFrame implements Runnable{

    public JLabel label;
    public MainScreen mainScreen;
    Graphics2D g;
    public String title;
    public static int width;
    public static int x,y;
    public int height;
    public Window(String title, int width, int height) throws HeadlessException {

        this.title = title;
        this.width = width;
        this.height = height;
        
        this.setVisible(false);
        this.setTitle(title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(width,height);
        this.setResizable(false);
        this.setVisible(true);
        
        //label= new JLabel();
        //label.setBounds(0,0,width,height);

        this.g=(Graphics2D)this.getGraphics();
        this.mainScreen=new MainScreen(g);
    }
    
    public void update() {
        Image dbimage =createImage(width,height);
        Graphics dbg =dbimage.getGraphics();
        draw(dbg);
        g.drawImage(dbimage,0,0,this);
        /*
         * ImageIcon i= new ImageIcon(dbimage); label =new JLabel(i);
         * label.setSize(width,height); this.add(label);
         */
        //MainScreen.botton.paint(g);
    }
    
    public void draw(Graphics dbg) {
        Graphics2D g=(Graphics2D)dbg;
        g.setColor(Color.CYAN);
        g.fillRect(0,0,width,height);
        mainScreen.draw(g,this);
    }
    
    @Override
    public void run() {
        while (true) {
            this.update();
        }
    }
}

MainScreen

package org.learning.learn;

import javax.swing.JButton;
import java.awt.*;

@SuppressWarnings("serial")
public class MainScreen extends Screen{
    
    public static JButton botton;
    Graphics2D g;
    Text text=new Text("Library",new Font("Comic Sans",Font.PLAIN,50),Window.width/2,100);
    
    MainScreen(Graphics2D g){
        this.g=g;
        this.botton=new JButton();
        botton.setBounds(40,40,100,50);
        botton.setFocusPainted(true);
    }
    
    @Override
    public void update() {
    }

    @Override
    public void draw(Graphics2D g,Window window) {
        
        botton.setBackground(Color.WHITE);
        botton.setDoubleBuffered(true);
        botton.setFocusPainted(true);
        botton.setFocusable(true);
        window.add(botton);
        g.setColor(Color.WHITE);
        g.setFont(text.font);
        g.drawString(text.text,text.x-(int)text.width/4,(int)text.y);
        //botton.paint(g);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
blaze rod
  • 1
  • 1

0 Answers0