Minggu, 14 September 2025

JAVA - Test Grid JFrame






package testgrid;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.GeneralPath;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.UnsupportedLookAndFeelException;


public class TestGrid {


    public static void main(String[] args) {

        new TestGrid();

    }


    public TestGrid() {

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                try {

                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {

                    ex.printStackTrace();

                }


                JFrame frame = new JFrame("Testing");

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(new GridPane());

                frame.pack();

                frame.setLocationRelativeTo(null);

                frame.setVisible(true);

            }

        });

    }


    public class GridPane extends JPanel {


        private WaveShape waveShape;


        public GridPane() {

            waveShape = new WaveShape();

        }


        @Override

        public Dimension getPreferredSize() {

            return new Dimension(200, 200);

        }


        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();

            g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());

            g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);

            g2d.dispose();


            // I don't trust you

            g2d = (Graphics2D) g.create();

            waveShape.draw(g2d, this);

            g2d.dispose();

        }


    }


    public interface GridShape {

        public void draw(Graphics2D g2d, JComponent parent);

    }


    public class WaveShape implements GridShape {


        @Override

        public void draw(Graphics2D g2d, JComponent parent) {

            g2d.setColor(Color.RED);


            int xDiff = parent.getWidth() / 4;

            int height = parent.getHeight() - 1;


            int xPos = 0;


            GeneralPath path = new GeneralPath();

            path.moveTo(0, 0);

            path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);

            xPos += xDiff;

            path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);

            xPos += xDiff;

            path.curveTo(xPos + xDiff, 0, xPos, height, xPos + xDiff, height);

            xPos += xDiff;

            path.curveTo(xPos + xDiff, height, xPos, 0, xPos + xDiff, 0);

            g2d.draw(path);

        }


    }


}

 

Tidak ada komentar: