Java中全透明窗口上的形状刷新
这是我的第一个问题,因为到目前为止,我可以通过其他问题找到任何东西。我的问题来了: 我设计了一个窗户,上面有两部分。左边的部分是完全透明的,我想在上面画一个可重构的形状。(在下面,形状是正方形。)右边的部分总是不透明的,我不会在上面画任何形状。一切正常,但对于左侧部分,背景形状不可重新绘制Java中全透明窗口上的形状刷新,java,swing,user-interface,transparent,paintcomponent,Java,Swing,User Interface,Transparent,Paintcomponent,这是我的第一个问题,因为到目前为止,我可以通过其他问题找到任何东西。我的问题来了: 我设计了一个窗户,上面有两部分。左边的部分是完全透明的,我想在上面画一个可重构的形状。(在下面,形状是正方形。)右边的部分总是不透明的,我不会在上面画任何形状。一切正常,但对于左侧部分,背景形状不可重新绘制 package haxbot; import java.awt.*; import java.awt.Robot.*; import javax.swing.*;
package haxbot;
import java.awt.*;
import java.awt.Robot.*;
import javax.swing.*;
import java.awt.image.*;
// import com.sun.awt.AWTUtilities;
public class drawScreen extends javax.swing.JFrame {
public drawScreen() {
initComponents();
}
//-------------------- automatically created ---------------------------------
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
setUndecorated(true);
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
formMouseMoved(evt);
}
});
jLabel1.setText("Position:");
jLabel2.setForeground(new java.awt.Color(255, 0, 0));
jLabel2.setText("[ 123, 134]");
jLabel5.setText("Color:");
jLabel6.setForeground(new java.awt.Color(255, 0, 0));
jLabel6.setText("[ 255, 255, 255]");
jButton1.setBackground(new java.awt.Color(255, 0, 0));
jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton1.setForeground(new java.awt.Color(255, 0, 0));
jButton1.setText("X");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(853, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(jLabel5))
.addContainerGap(338, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
//-------------------- automatically created (end) ---------------------------------
private void formMouseMoved(java.awt.event.MouseEvent evt) {
MousePos = evt.getPoint();
int mX = (int) MousePos.getX();
int mY = (int) MousePos.getY();
MousePos = evt.getPoint();
jLabel2.setText("[ " + mX + ", " + mY + " ]");
getColor(mX + (int) wind.getLocation().getX(), mY + (int) wind.getLocation().getY());
paint(this.getGraphics());
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
public Color getColor(int mX, int mY) {
try {
robot = new Robot();
} catch (AWTException awtE) {
awtE.printStackTrace();
}
Rectangle captureSize = new Rectangle(mX - 15, mY - 15, 30, 30);
img = robot.createScreenCapture(captureSize);
currColor = robot.getPixelColor(mX, mY);
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
int x = img.getRGB(i, j);
}
}
jLabel6.setText("[ " + currColor.getRed() + ", " + currColor.getGreen() + ", "
+ currColor.getBlue() + "]");
return currColor;
}
public static void main(String args[]) {
graphicsObtained = false;
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
wind = new drawScreen();
wind.setVisible(true);
wind.setLocation(182, 154);
wind.setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(wind);
} catch (Exception ex) {
ex.printStackTrace();
}
// Add Transparency
// AWTUtilities.setWindowOpacity(wind, (float) 0.6);
currColors = new Color[10][10];
}
});
}
public void paint(Graphics g) {
leftScreen = (Graphics2D) g.create();
leftScreen.setClip(0, 0, 840, 410);
if (MousePos != null) {
//leftScreen.clearRect((int) MousePos.getX() - 150, (int) MousePos.getY() - 150, 300, 300);
leftScreen.drawRect((int) MousePos.getX() - 15, (int) MousePos.getY() - 15, 30, 30);
}
rightScreen = (Graphics2D) g.create();
rightScreen.setClip(850, 0, this.getWidth(), 410);
super.paint(rightScreen);
rightScreen.dispose();
leftScreen.dispose();
if (MousePos != null) {
//g2d1.clearRect(0, 0, 520, 430);
}
/*g2d.setColor(Color.red);
g2d.clearRect(55, 430, 90, 10);
if (MousePos != null)
g2d.drawString("[ " + MousePos.getX() + ", " + MousePos.getY() + " ]", 55, 440);
else
g2d.drawString("[ 0, 0 ]", 55, 440);*/
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
// End of variables declaration
private Point MousePos;
private Point PlayerPos;
private Color currColor;
private static Color[][] currColors;
private static drawScreen wind;
private BufferedImage img;
private static boolean graphicsObtained;
private Graphics initialSection;
private Graphics2D leftScreen, rightScreen;
Robot robot = null;
}
对于Swing,您需要重写
paintComponent()
,而不是paint()
。您直接在顶级容器上绘制。考虑使用JPAND。覆盖JPanel的paintComponent
编辑
在阅读您的反馈后,我想您需要做的是:
首先,要记住以下几点:
JFrame
也定义了许多窗格,其顶部是JRootPane
。JRootPane由玻璃窗格
、内容窗格
和分层窗格
组成。通常我们所做的只是创建一个类,它扩展了一个组件,比如JPanel。在该类中,我们重写了paintComponent()
方法,这就是我们绘制东西的方式。然后我们将它添加到JFrame
的内容窗格中JComponent
的类。这里使用您想要绘制的任何内容重写paintComponent()方法。现在,在您的类(扩展JFrame)中,使用setContentPane
将contentPane设置为您刚刚创建的扩展JComponent类。setContentPane
的结构是
public void setContentPane(Container contentPane)
因此,您可以传递扩展JComponent
的类的对象,因为JComponent
是容器的子类
您所做的只是在使用
getContentPane
之后设置内容窗格的布局。但是,您还需要设置它,以便它按照您希望的方式运行。通常我们不会这样做,因为我们直接使用add()
将组件添加到内容窗格中。我希望这能起作用。对于Swing,您需要重写paintComponent()
,而不是paint()
。您直接在顶级容器上绘制。考虑使用JPAND。覆盖JPanel的paintComponent
编辑
在阅读您的反馈后,我想您需要做的是:
首先,要记住以下几点:
JFrame
也定义了许多窗格,其顶部是JRootPane
。JRootPane由玻璃窗格
、内容窗格
和分层窗格
组成。通常我们所做的只是创建一个类,它扩展了一个组件,比如JPanel。在该类中,我们重写了paintComponent()
方法,这就是我们绘制东西的方式。然后我们将它添加到JFrame
的内容窗格中JComponent
的类。这里使用您想要绘制的任何内容重写paintComponent()方法。现在,在您的类(扩展JFrame)中,使用setContentPane
将contentPane设置为您刚刚创建的扩展JComponent类。setContentPane
的结构是
public void setContentPane(Container contentPane)
因此,您可以传递扩展JComponent
的类的对象,因为JComponent
是容器的子类
您所做的只是在使用getContentPane
之后设置内容窗格的布局。但是,您还需要设置它,以便它按照您希望的方式运行。通常我们不会这样做,因为我们直接使用add()
将组件添加到内容窗格中。我希望这能奏效。一些建议:
- 绘制一个扩展JPanel或JComponent的类,而不是JFrame
- 在替代的paintComponent方法中绘制,而不是绘制方法
- paintComponent方法的第一行应该是调用
super.paintComponent(g)代码>
- 不要处理在paintComponent(或paint)参数中传递给您的图形对象。仅处置您自己创建的图形或Graphics2D对象
- 绘制一个扩展JPanel或JComponent的类,而不是JFrame
- 在替代的paintComponent方法中绘制,而不是绘制方法
- paintComponent方法的第一行应该是调用
super.paintComponent(g)代码>
- 不要处理在paintComponent(或paint)参数中传递给您的图形对象。仅处置您自己创建的图形或Graphics2D对象
- 一些建议: