我成功地渲染了一个多边形窗口。但是,我想用细笔画出它的轮廓。
是否可以在 Java 中勾勒出一个形状的窗口?
这是我的工作代码,我正在使用 componentResized 方法来设置窗口的形状。但是,如果有任何其他方法可以获取大纲,无论是在选项卡窗口最小化时还是选项卡窗口最大化时,请帮助。
//LongTab.java
//Desktop Tab
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class LongTab extends JWindow implements MouseListener{
static LongTab t;
Boolean isVisible = false;
final static BasicStroke stroke = new BasicStroke(2.0f);
GeneralPath path;
public LongTab(){
addMouseListener(this);
setSize(500, 1080);
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e){
Polygon polygon = new Polygon();
polygon = new Polygon();
polygon.addPoint(40, 1080);
polygon.addPoint(40, 700);
polygon.addPoint(20, 690);
polygon.addPoint(20, 400);
polygon.addPoint(40, 390);
polygon.addPoint(40, 0);
polygon.addPoint(500, 0);
polygon.addPoint(500, 1080);
path = new GeneralPath();
path.append(polygon, true);
setShape(path);
}
});
setSize(40, 1080);
setLocation(1880, 0);
}
public void mouseClicked (MouseEvent me) {
if(!isVisible) {
isVisible=true;
t.setSize(400, 1080);
t.setLocation(1520, 0);
return;
}
if(isVisible) {
isVisible=false;
t.setSize(40, 1080);
t.setLocation(1880, 0);
}
return;
}
public void mouseEntered (MouseEvent me) {
}
public void mousePressed (MouseEvent me) {
}
public void mouseReleased (MouseEvent me) {
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(stroke);
//if(!isVisible)
//g2.draw(path);
//repaint();
}
public void mouseExited (MouseEvent me) {
}
public static void main (String[] args){
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If shaped windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {
System.err.println("Shaped windows are not supported");
System.exit(0);
} else {
t = new LongTab();
t.setVisible(true)
}
}
});
}
}