#How can I get my screen to not flash when Im calling the repain(); function

25 messages · Page 1 of 1 (latest)

obsidian gale
#

I'm working on a school project right now and we were tasked with making a program that spawns a shape when you click the screen, the shape will then move in a random direction and bounce all over the place.

I have an issue right now where when I spawn the shape, the screen flashes which I assume is happening because I am repainting the screen every 200ms

misty fogBOT
#

This post has been reserved for your question.

Hey @obsidian gale! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

obsidian gale
#
this.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                // Get mouse position
                int x = evt.getX();
                int y = evt.getY();
                String randomColor = Randomizer.getRandomColor();
                int randomSpeed = Randomizer.getRandomNumber(1, 10);
                int randomWidth = Randomizer.getRandomNumber(10, 100);
                int randomHeight = Randomizer.getRandomNumber(10, 100);
                int randomNumber = Randomizer.getRandomNumber(10, 100);


                int randomNum = Randomizer.getRandomNumber(1,3);
                Shape newShape = null;

                switch(randomNum){
                    case 1:
                        newShape = new Square(x, y, randomWidth, randomHeight, randomSpeed, randomColor);
                        break;
                    case 2:
                        newShape = new Triangle(x, y, randomWidth, randomHeight, randomSpeed, randomColor);
                        break;
                    case 3:
                        newShape = new Circle(x, y, randomWidth, randomHeight, randomSpeed, randomColor);
                        break;
                }
                shapes.add(newShape);








            }
        });

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Shape shape : shapes) {
            shape.draw(g2);
        }
    }

    // Timer to move the shapes
    public void startTimer() {
        Timer timer = new Timer(200, e -> {
            System.out.println("Timer");
            for (Shape shape : shapes) {
                shape.move();
            }
            repaint();
        });
        timer.start();
    }
sly otter
obsidian gale
sly otter
obsidian gale
obsidian gale
#

idk if that makes any sense

#
package classes;
import javax.swing.*;
import java.awt.*;

public class ProgramWindow extends JFrame{
    public ProgramWindow() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);
        this.setResizable(false);
        this.setVisible(true);

        //Make our own JPanel
        DrawingPanel drawing = new DrawingPanel();
        drawing.setBackground(Color.RED);

        this.add(drawing );
        drawing.setLayout(null);
    }
}
sly otter
#
public class ProgramWindow extends JFrame{
    public ProgramWindow() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);
        this.setResizable(false);
        this.setLayout(new BorderLayout());

        //Make our own JPanel
        DrawingPanel drawing = new DrawingPanel();
        drawing.setBackground(Color.RED);

        this.add(drawing,BorderLayout.CENTER);
        drawing.setLayout(null);
        this.setVisible(true);
    }
}
obsidian gale
#

that works, do you think you can tell me exactly what I did wrong so I know for next time!

#

And that also fixes my flashing problem 🙂

#

Like I see you added a border Layout but what does that do specifically?

sly otter
#

are you still overriding paint in the JPanel ?

obsidian gale
#

I changed it from paint and made it paintComponent

#

but yes

#

i am overiding it

sly otter
#

yeah that's what I would expect.

obsidian gale
#
@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (Shape shape : shapes) {
            shape.draw(g2);
        }
    }
sly otter
# obsidian gale Like I see you added a border Layout but what does that do specifically?

A LayoutManager does the job of resizing the components that have been added to them. In our case BorderLayout divides the frame into 5 compartments. NORTH, EAST, SOUTH, WEST, CENTER. I am adding to center because then the component gets stretched to fit the entire window and resizing as well as collisions with other elements registered to this layout manager are taken care of.

obsidian gale
#

thats awesome, okay thank you very much! i wish i could upvote you some how cause you rrly saved me a lot of struggles

misty fogBOT
obsidian gale
#

thank you very much im closing the post now!