简体   繁体   中英

Is it possible to write paint on screen program in java/swing?

I'm writing paint on screen program using Java Swing. It working on ubuntu linux. But windows shows black screen instead of transparent panel. I included similar example code. What is wrong in my code?

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

public class Example {

public static final Color COLOR_TRANSPARENT = new Color(0,0,0,0);

public Example() {
    Canvas drawArea = new Canvas();
    drawArea.setBackground(COLOR_TRANSPARENT);
    drawArea.setOpaque(true);

    JWindow drawingFrame = new JWindow();
    drawingFrame.setBackground(COLOR_TRANSPARENT);
    drawingFrame.setContentPane(drawArea);
    drawingFrame.pack();
    drawingFrame.setSize(640, 460);
    drawingFrame.setVisible(true);
    drawingFrame.setLocationRelativeTo(null);
    drawingFrame.setAlwaysOnTop(true);
}

public static void main(String[] args){
    SwingUtilities.invokeLater(Example::new);
}

class Canvas extends JPanel{

    private Image image;
    private Graphics2D g2;

    public Canvas() {
        super();
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                g2.setPaint(Color.RED);
                g2.fillOval(x-10, y-10, 20, 20);
                repaint(x-10, y-10, 20, 20);
            }
        });
    }

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

        if (image == null){
            image = createImage(getWidth(), getHeight());
            g2 = (Graphics2D) image.getGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setBackground(COLOR_TRANSPARENT);
            clear();
        }
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(image, 0,0, null);
    }

    public void clear(){
        System.out.println("clearing canvas ");
        g2.setComposite(AlphaComposite.Clear);
        g2.setBackground(COLOR_TRANSPARENT);
        g2.setColor(COLOR_TRANSPARENT);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.clearRect(0, 0, getWidth(), getHeight());
        g2.setPaint(Color.RED);
        g2.setComposite(AlphaComposite.SrcOver);
        repaint();
    }
}
}

Here is screenshot what I wanted. ubuntu linux中工作示例的屏幕截图

Example code updated. Now code should work without any other additional code.

For windows I made a couple of changes:

image = createImage(getWidth(), getHeight());
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);

I used a BufferedImage to you can set the alpha values of the image to be transparent.

//public static final Color COLOR_TRANSPARENT = new Color(0,0,0,0);
public static final Color COLOR_TRANSPARENT = new Color(0,0,0,1);

I made the alpha value non-zero, because a value of zero means the Java application won't receive the MouseEvent because it is passed to the application under the window.

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