gpt4 book ai didi

Java AWT 在半透明框架上绘制

转载 作者:行者123 更新时间:2023-12-02 04:13:33 24 4
gpt4 key购买 nike

我在半透明框架上绘图时遇到问题。当“alphaValue”为 255 时,一切都会按预期进行。但我需要一个半透明的框架。我在下面创建了一个小型测试类来演示该问题。正如您所看到的,“中间”矩形始终出现。但是“DRAW”矩形仅在“alphaValue”为255时出现。当它<=254时,您可以通过打印行看到该方法仍然被调用,但图像似乎没有刷新。预先感谢您的帮助。

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TransparencyTest {
private static Point startPoint = new Point();
private static Point endPoint = new Point();

public static void main(String[] args) {
new TransparencyTest().test();
}

@SuppressWarnings("serial")
private void test() {
int alphaValue = 255;

Frame myFrame = new Frame();
myFrame.setUndecorated(true);
myFrame.setBackground(new Color(0, 0, 0, alphaValue));
myFrame.setSize(800, 800);

Panel myPanel = new Panel() {
public void paint(Graphics g) {
super.paint(g);
System.out.println("PAINT");
g.setColor(new Color(255, 0, 0, 255));
if(startPoint.equals(new Point())) {
System.out.println("MIDDLE");
g.drawRect(200, 200, 400, 400);
}
else {
System.out.println("DRAW");
g.drawRect(
(int)Math.min(startPoint.getX(), endPoint.getX()),
(int)Math.min(startPoint.getY(), endPoint.getY()),
(int)Math.abs(startPoint.getX() - endPoint.getX()),
(int)Math.abs(startPoint.getY() - endPoint.getY())
);
}
}
};
myFrame.add(myPanel);

MouseAdapter myMouseAdapter = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}

public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
myPanel.repaint();
}
};
myPanel.addMouseListener(myMouseAdapter);
myPanel.addMouseMotionListener(myMouseAdapter);

myFrame.setVisible(true);
}
}

最佳答案

AWT 组件本身没有透明的概念,它们始终是不透明的。

您使用了 JPanel,并使用 setOpaque 来控制不透明度(打开或关闭)。这将使面板变得透明,然后您应该能够看到直接应用于框架的 Alpha 效果...

Transparent

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TransparencyTest {

private static Point startPoint = new Point();
private static Point endPoint = new Point();

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

new TransparencyTest().test();
}
});
}

@SuppressWarnings("serial")
private void test() {
int alphaValue = 128;

JFrame myFrame = new JFrame();
myFrame.setUndecorated(true);
myFrame.setBackground(new Color(0, 0, 0, alphaValue));
// myFrame.setOpacity(0.1f);
myFrame.setSize(800, 800);
myFrame.setLocation(100, 100);

JPanel myPanel = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);

System.out.println("PAINT");
g.setColor(new Color(255, 0, 0, 255));
if (startPoint.equals(new Point())) {
System.out.println("MIDDLE");
g.drawRect(200, 200, 400, 400);
} else {
System.out.println("DRAW");
g.drawRect(
(int) Math.min(startPoint.getX(), endPoint.getX()),
(int) Math.min(startPoint.getY(), endPoint.getY()),
(int) Math.abs(startPoint.getX() - endPoint.getX()),
(int) Math.abs(startPoint.getY() - endPoint.getY())
);
}
}
};
myPanel.setOpaque(false);
myFrame.add(myPanel);

MouseAdapter myMouseAdapter = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}

public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
myPanel.repaint();
}
};
myPanel.addMouseListener(myMouseAdapter);
myPanel.addMouseMotionListener(myMouseAdapter);

myFrame.setVisible(true);
}
}

关于Java AWT 在半透明框架上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33554128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com