How can I reduce the flickering in this java program?

I’m new to Java graphics, so most of this code is stuff I’ve gathered from the internet and injecting it into my own program. This program is meant to have a red square, controlled by the arrow keys, detect when it collides with a falling blue dot that resets to the top each time it hits the bottom.

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

class Surface extends JPanel implements ActionListener, KeyListener {

    private final int DELAY = 8;
    private Timer timer;
    private Image image;
    private int x, y;
    private final int MOVE_AMOUNT = 5;
    public final int width = 800;
    public final int length = 600;
    private boolean upPressed, downPressed, leftPressed, rightPressed;
    ;

    public Surface() {
        setDoubleBuffered(true);
        initTimer();
        loadImage();
        setFocusable(true);
        requestFocusInWindow();
        addKeyListener(this);
        
        x = 200;
        y = 200;
    }
    
    private Image resizeImage(Image originalImage, int newWidth, int newHeight) {
        return originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    
    public Rectangle getRedDotBounds() {
        return new Rectangle(x, y, image.getWidth(this), image.getHeight(this));
    }
    
    private void initTimer() {

        timer = new Timer(DELAY, this);
        timer.start();
    }
    
    private void loadImage(){
        ImageIcon ii = new ImageIcon("Basic_red_dot.png");
        if (ii.getImageLoadStatus() == MediaTracker.ERRORED) {
            System.out.println("Image failed to load.");
        }
        Image originalImage =ii.getImage();
     // Resize the image to the desired dimensions
        int newWidth = 75; // Set the desired width
        int newHeight = 75; // Set the desired height
        image = resizeImage(originalImage, newWidth, newHeight);
    }
    
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.clearRect(0, 0, getWidth(), getHeight());
        drawImage(g);
    }
    
    private void drawImage(Graphics g){
        g.drawImage(image, x, y, this);
    }
    
    public Timer getTimer() {
        
        return timer;
    }
    
    public void actionPerformed(ActionEvent e) {
        updatePosition();
        repaint();
    }
    
    
    private void updatePosition(){
        if (leftPressed){
            x = Math.max(x-MOVE_AMOUNT, 0);
        }
        if(rightPressed){
            x = Math.min(x + MOVE_AMOUNT, getWidth() - image.getWidth(this));
        }
        if(upPressed){
            y = Math.max(y - MOVE_AMOUNT, 0);
        }
        if(downPressed){
            y = Math.min(y + MOVE_AMOUNT, getHeight() - image.getHeight(this));
        }
    }
    
    
    @Override
    
    public void keyPressed(KeyEvent e){
        int key = e.getKeyCode();
        
        switch (key) {
            case KeyEvent.VK_LEFT:
                leftPressed = true;
                break;
            case KeyEvent.VK_RIGHT:
                rightPressed = true;
                break;
            case KeyEvent.VK_UP:
                upPressed = true;
                break;
            case KeyEvent.VK_DOWN:
                downPressed = true;
                break;
        }
        
        repaint();
    }
    
    
    @Override
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        
        switch (key) {
            case KeyEvent.VK_LEFT:
                leftPressed = false;
                break;
            case KeyEvent.VK_RIGHT:
                rightPressed = false;
                break;
            case KeyEvent.VK_UP:
                upPressed = false;
                break;
            case KeyEvent.VK_DOWN:
                downPressed = false;
                break;
        }
        
        
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // Not used, but required by KeyListener
    }

}


class BlueDot extends JPanel implements ActionListener {
    private int x, y;
    private Image image;
    private final int DOT_SIZE = 10;
    private final int FALL_SPEED = 1;
    private Timer timer;
    private int n = 0;
    
    public BlueDot() {
        setDoubleBuffered(true);
        setPreferredSize(new Dimension(500, 500));
        x = (int) (Math.random()*500);
        y = 0;
        loadImage();
        timer = new Timer(10, this);
        timer.start();
    }
    
    private Image resizeImage(Image originalImage, int newWidth, int newHeight) {
        return originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    }
    
    private void loadImage() {
        ImageIcon ii = new ImageIcon("Basic_blue_dot.png");
        if (ii.getImageLoadStatus() == MediaTracker.ERRORED) {
            System.out.println("Image failed to load.");
        }
        Image originalImage =ii.getImage();
     // Resize the image to the desired dimensions
        int newWidth = 200; // Set the desired width
        int newHeight = 200; // Set the desired height
        image = resizeImage(originalImage, newWidth, newHeight);
    }
    
    public Rectangle getBlueDotBounds() {
        return new Rectangle(x, y, DOT_SIZE, DOT_SIZE);
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.clearRect(0, 0, getWidth(), getHeight());
        g.drawImage(image, x, y, DOT_SIZE, DOT_SIZE, this);
    }
    
    public void actionPerformed(ActionEvent e){
        
        y += FALL_SPEED;
        if (y > getHeight()) {
            n++;
            System.out.println("reset" + n);
            x = (int) (Math.random() * 500);
            y = 0;
        }
        //repaint();
    }
}


public class MyProgram
{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame();
                frame.setTitle("Image Display");
                frame.setSize(500,500);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
                
                Surface surface = new Surface();
                BlueDot blueDot = new BlueDot();
                
                
                frame.setLayout(null);
                
                surface.setBounds(0, 0, 500, 500);
                frame.add(surface);
                
                blueDot.setBounds((int) (Math.random() * 450), 0, 500, 500);
                frame.add(blueDot);
                
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                
                surface.requestFocusInWindow();
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowOpened(WindowEvent e) {
                        surface.requestFocusInWindow();
                    }
                
                });
                
                Timer collisionTimer = new Timer(10, new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        Rectangle redBounds = surface.getRedDotBounds();
                        Rectangle blueBounds = blueDot.getBlueDotBounds();
                        
                        if (redBounds.intersects(blueBounds)) {
                            System.out.println("Collision!!!!");
                        }
                    }
                });
                collisionTimer.start();
            }
        });
    }
    
}

The collision detection is a little off, mainly because the red square and the blue dot are constantly flickering. I’ve tried commenting out each of the three repaint statements, but that either results in one of the two elements being invisible, or some other glaring issue. How can I fix the program so this flickering goes away and the collision detection works flawlessly?

New contributor

Johnny Gord. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật