I'm successfully rendering a polygon shaped window. However, I would like to outline it with a thin stroke.
Is it possible to outline a shaped window in Java?
Here's my working code, I'm using the componentResized method to set the shape for the window. However, if there is any other way to go in order to get the outline, both for when the Tab-Window is minimized and when the Tab-Window is maximized, please help.
//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)
}
}
});
}
}
I managed to figure out how to solve the problem with Java 6. It seems just simply taking out the antialias line in my paint method solves the graphic rendering glitch, where the outline isnt clean enough. Here's the fully working code...
Regards Aubrey.
//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);
}
}