简体   繁体   中英

Java Applet - How to add double-buffering to JButton

I am currently using the Applet class to create a simple game. Because there is a flickering effect, I've added double-buffering for Graphics components by creating an off-screen buffer like so:

public class AppletTest extends Applet implements Runnable {

    Thread thread;
    Image img;
    Graphics gfx;

    public final int WIDTH = 700, HEIGHT = 500;

    public void init() {
        this.resize(WIDTH, HEIGHT);

        thread = new Thread(this);
        thread.start();

        img = createImage(WIDTH, HEIGHT); // off-screen buffering
        gfx = img.getGraphics();
    }
    public void draw(Graphics g) {
        gfx.setColor(Color.BLACK);
        gfx.fillRect(0, 0, WIDTH, HEIGHT);
        gfx.setColor(Color.WHITE);

        gfx.fillRect(50, 50, 100, 100);
        gfx.setFont(new Font("Century", Font.BOLD, 30));
        gfx.drawString("I feel good sometimes I don't", 200, 200);            

        g.drawImage(img, 0, 0, this); // draws the off-screen image
    }
    public void update(Graphics g) {
        draw(g);
    }

    public void run() {
        while(true) {
            repaint();
            try {
                Thread.sleep(5);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

If you run the application, all the Graphics (.fillRect, .drawString, etc.) components/methods are drawn on the off-screen buffer. However, my goal is to add a JButton to the applet - and as expected, there's no off-screen loading for the JButton component (which means flickering).

Graphics gfx;
JButton button1;

public void draw(Graphics g) {
    setLayout(null);

    button1.setBounds(225, 400, 250, 50);
    button1.setFont(new Font("Courier", Font.PLAIN, 17));
    button1.setForeground(Color.WHITE);
    button1.setBackground(Color.DARK_GRAY);

    add(button1); // is it possible to draw the JButton on the off-screen buffer?
}

How would you add off-screen loading to a JButton component?

Applet (and JApplet ) are officially deprecated, they are no longer supported by Java, Oracle, Browsers (or the community generally)

Swing components are, by default, double buffered. If you work with the painting system correctly, you shouldn't experience any flickering, if you do, it's clear sign that you're doing something wrong.

I would recommend having a look at Performing Custom Painting and Painting in AWT and Swing for more details about how the Swing painting system works.

Swing is single threaded AND not thread safe. This means that you should not perform any long running operations within the context of the Event Dispatching Thread and you should not update the UI from outside the context of the EDT.

Have a look at Concurrency in Swing for more details.

A simple solution to these problems is to make use a Swing Timer , which can be used to schedule regular updates which are executed within the context of the EDT.

See How to Use Swing Timers for more details...

As a basic runnable example...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        public static final int WIDTH = 700, HEIGHT = 500;

        public TestPane() {
            setLayout(new GridBagLayout());
            add(new JButton("Big fat button"));
            Timer timer = new Timer(5, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(WIDTH, HEIGHT);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, WIDTH, HEIGHT);
            g2d.setColor(Color.WHITE);

            g2d.fillRect(50, 50, 100, 100);
            g2d.setFont(new Font("Century", Font.BOLD, 30));
            g2d.drawString("I feel good sometimes I don't", 200, 200);

            g2d.dispose();
        }

    }

}

Okay, "But I absolutely, must, no questions asked, use Applet ... 😓, then I feel sorry for you, but that doesn't change the fact that Swing is already double buffered. The above example could easily be applied to a J/Applet simply by creating an instance of the JPanel and adding to an Applet container

Swing makes use of "passive rendering" algorithm, if you absolutely must be complete control, then you can have a look at BufferStrategy which hands complete control of the painting system over to you, but you won't be able to use Swing components, as they are updated by the Swing sub-system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM