简体   繁体   中英

How to paint another object instead of repaint() JPanel

I have a JFrame in which I print 3 objects using 3 threads:
Thread-1) Prints Circles
Thread-2) Prints Squares
Thread-3) Prints Triangle
The problem is that I need to print new objects over and over again, and not just repaint them. But my run() function of each thread can't touch the Graphics g variable of the Overrideded method paintComponent(Graphics g) of the class. I can only use repaint() inside the run() function.
This is what I have: ( repaint() method used ) https://prnt.sc/gnxz6iM
This is what I want( WITHOUT THOSE SQUARE BORDERS of the paintImmediatly() method ): https://prnt.sc/gny1qx

package tarefa03;

import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Random;
import javax.swing.OverlayLayout;


public class FigurePlacer extends JPanel implements Runnable{
    final int width = 700;
    final int height = 700;
    int x_pos = 0;
    int y_pos = 0;
    int x_width = 50;
    int y_height = 50;
    String figure;

    public FigurePlacer(String str){
        figure = str;
        randomCoord();
        setOpaque(false);
        setBounds(0, 0, width, height);
        Thread th = new Thread (this);
        th.start();
    }

    private void randomCoord(){
        Random random = new Random();
        x_pos = random.nextInt(width);
        y_pos = random.nextInt(height);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        switch (figure){       
            case "circle":
            g2.setColor(Color.BLUE);
            g2.fillOval(x_pos, y_pos, x_width, y_height);
            break;

            case "square":     
            g2.setColor(Color.GREEN);
            g2.fillRect(x_pos, y_pos, x_width, y_height);
            break;

            case "triangle":
            g2.setColor(Color.ORANGE);
            int xpoints[] = {x_pos, x_pos+25, x_pos+50};
            int ypoints[] = {y_pos, y_pos+50, y_pos};
            g2.fillPolygon(xpoints,ypoints,3);
        }
    }

    @Override
    public void run(){
        while (true){
            randomCoord();
            this.paintImmediately(x_pos, y_pos, x_width, y_height);
            try{
                Thread.sleep (200);
            }
            catch (InterruptedException ex){}
        }
    }  

}

You don't have your rendering indicating that it's drawing routine is not opaque.

When drawing over something, the setOpaque(...) method is a hint to the rendering thread if the background pixels can be directly set, or must be checked for transparency and blended with the existing pixel value.

Note that you will also have to have a transparency set in the color. Lack of such a thing means your blending support might be turned on, but you drew an opaque color.

Also, Swing is not multithreaded safe. It uses a private threads to do rasterization and UI update. Any "multi-threaded" solution needs to dispatch all the UI updates to this drawing thread. Otherwise, you will not get the results you desire (as that rendering thread is integrated into core OS drawing routines, and written specifically to support AWT, Swing, and other technologies.

This means that even small things like presenting the GUI Frame need to be "dispatched". See SwingWorker if you have tasks that need to coordinate between the main Java thread and the drawing thread, and SwingUtilites.invokeAndWait(...) / SwingUtilites.invokeLater(...) if the coordination is not that fine grained.

Yes, you can attempt to do things (like you did) from the main thread. Sometimes it almost works, but generally it won't work properly, and won't continue to work properly over time.

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