Java形状窗口与大纲

时间:2012-05-20 19:37:50

标签: java swing shaped-window

我成功渲染出多边形窗口。但是,我想用一个薄的笔画勾勒出它。

是否可以在Java中勾勒出形状窗口?

这是我的工作代码,我使用componentResized方法设置窗口的形状。但是,如果还有其他方法可以获得大纲,那么当Tab-Window最小化时以及Tab-Window最大化时,请帮助。

//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)
        }
      }
    });
  }
}

3 个答案:

答案 0 :(得分:1)

我修改了你的代码以显示形状的窗口轮廓。

    //LongTab.java
//Desktop Tab

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;

public class LongTab extends JFrame{

private Polygon polygon;

public LongTab() {
    setSize(500, 720);
    setLocation(10, 10);

    setUndecorated(true);


    polygon = new Polygon();
    polygon = new Polygon();
    polygon.addPoint(40, 720);
    polygon.addPoint(40, 700);
    polygon.addPoint(20, 690);
    polygon.addPoint(20, 400);
    polygon.addPoint(40, 390);
    polygon.addPoint(40, 20);
    polygon.addPoint(500, 20);
    polygon.addPoint(500, 720);

    GeneralPath path = new GeneralPath();
    path.append(polygon, true);
    setShape(path);
}

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setColor(Color.RED);
    g2.draw(polygon);
}

public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();

    // If shaped windows aren't supported, exit.
    if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
        System.err.println("Shaped windows are not supported");
        System.exit(0);
    } else {
        new LongTab().setVisible(true);
    }
}
}

请注意,我还将多边形设置得更小,因为我没有适合您原始多边形的高清屏幕。

最后我删除了事件处理代码,因为我无法理解您尝试使用鼠标点击进行的操作。有了这个工作起点,您可以再次添加事件处理代码。

答案 1 :(得分:1)

好的,添加了导入,并取出了Translucency验证。当我运行这个Java6代码时,红色轮廓显示<1秒,然后轮廓消失!不知道为什么?

//LongTab.java
//Desktop Tab

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
//import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;

import javax.swing.JFrame;
import com.sun.awt.AWTUtilities.*;

public class LongTab extends JFrame{

private Polygon polygon;

public LongTab() {
    setSize(500, 720);
    setLocation(10, 10);

    setUndecorated(true);


    polygon = new Polygon();
    polygon = new Polygon();
    polygon.addPoint(40, 720);
    polygon.addPoint(40, 700);
    polygon.addPoint(20, 690);
    polygon.addPoint(20, 400);
    polygon.addPoint(40, 390);
    polygon.addPoint(40, 20);
    polygon.addPoint(500, 20);
    polygon.addPoint(500, 720);

    GeneralPath path = new GeneralPath();
    path.append(polygon, true);
    com.sun.awt.AWTUtilities.setWindowShape(this, path);
}

public void paint(Graphics g) {
    super.paint(g);

    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setColor(Color.RED);
    g2.draw(polygon);
}

public static void main(String[] args) {
    //GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //GraphicsDevice gd = ge.getDefaultScreenDevice();

    // If shaped windows aren't supported, exit.
    //if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
    //    System.err.println("Shaped windows are not supported");
    //    System.exit(0);
    //} else {
        new LongTab().setVisible(true);
    //}
}
}

答案 2 :(得分:0)

我设法弄清楚如何用Java 6解决问题。看起来只是简单地在我的paint方法中取出antialias行解决了图形渲染故障,其中轮廓不够干净。这是完全有效的代码......

此致 奥布里。

 //LongTab.java
 //Desktop Tab

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    import com.sun.awt.AWTUtilities.*;

    public class LongTab extends JWindow implements MouseListener{

    static LongTab t;
    Boolean isVisible = false;

    GeneralPath closed;

    final static BasicStroke stroke = new BasicStroke(2.0f);
    GeneralPath path;


    //Constructor
    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);
    com.sun.awt.AWTUtilities.setWindowShape(t, path);

    }});

    setSize(40, 1080);
    setLocation(1880, 0);

    }//end of constructor.

     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 mouseExited (MouseEvent me) {}  

     public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        //antialias commented out to fix outline glitch.
        //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setStroke(stroke);

    g2.drawLine(40, 1080, 40, 700);
    g2.drawLine(40, 700, 20, 690);
    g2.drawLine(20, 690, 20, 400);
    g2.drawLine(20, 400, 40,390);
    g2.drawLine(40, 390, 40, 0);



    }
    public static void main (String[] args){

                t = new LongTab();
                t.setVisible(true);
    }

}