Java JFrame create stop sign shaped window

Description

Java JFrame create stop sign shaped window

import java.awt.Color;
import java.awt.Font;
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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class StopPanel extends JPanel {
   public StopPanel() {
      this.setBackground(Color.red);
      this.setForeground(Color.red);
      this.setLayout(null);
      JButton okButton = new JButton("YES");
      JButton cancelButton = new JButton("NO");
      okButton.setBounds(90, 225, 65, 50);
      cancelButton.setBounds(150, 225, 65, 50);

      okButton.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent event) {
            System.exit(0);//  w  w  w.j a va2  s.c  om
         }
      });
      cancelButton.addActionListener(new ActionListener() {

         public void actionPerformed(ActionEvent event) {
            System.exit(0);
         }
      });

      this.add(okButton);
      this.add(cancelButton);

   }

   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      int pageHeight = this.getHeight();
      int pageWidth = this.getWidth();
      int bigHeight = (pageHeight + 80) / 2;
      int bigWidth = (pageWidth - 305) / 2;
      int smallHeight = (pageHeight + 125) / 2;
      int smallWidth = (pageWidth - 225) / 2;

      Font bigFont = new Font("Castellar", Font.BOLD, 112);
      Font smallFont = new Font("Castellar", Font.PLAIN, 14);

      g2d.setFont(bigFont);
      g2d.setColor(Color.white);
      g2d.drawString("STOP", bigWidth, bigHeight);
      g2d.setFont(smallFont);
      g2d.drawString("Are you sure you want to continue?", smallWidth, smallHeight);
   }
}

public class Main extends JFrame {

   public Main() {
      this.setTitle("Example");
      this.setSize(300, 300);
      this.setLocationRelativeTo(null);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      this.add(new StopPanel());
      final Polygon myShape = getPolygon();

      this.addComponentListener(new ComponentAdapter() {

         @Override
         public void componentResized(ComponentEvent e) {
            setShape(myShape);
            ((JFrame) e.getSource()).setForeground(Color.red);
            ((JFrame) e.getSource()).setBackground(Color.red);
         }
      });

      this.setUndecorated(true);
   }

   private Polygon getPolygon() {
      int x1Points[] = { 0, 0, 100, 200, 300, 300, 200, 100 };
      int y1Points[] = { 100, 200, 300, 300, 200, 100, 0, 0 };
      Polygon polygon = new Polygon();
      for (int i = 0; i < y1Points.length; i++) {
         polygon.addPoint(x1Points[i], y1Points[i]);
      }
      return polygon;
   }

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

      if (!device.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
         System.out.println("Shaped windows are not supported on" + "your system.");
         System.exit(0);
      }

      SwingUtilities.invokeLater(new Runnable() {

         @Override
         public void run() {
            Main window = new Main();
            window.setVisible(true);
         }
      });

   }
}



PreviousNext

Related