/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.graphics;

import javax.swing.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;

public class GraphicsExample extends JPanel {

	GraphicsExample() {
		super();
	}

	@Override
	public void paint(Graphics g) {
		final Graphics2D graphics2D = (Graphics2D) g;
		Stroke stroke = new BasicStroke(1f,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 0.f);
		Paint paint = new GradientPaint(0,0,Color.BLUE,100,0,Color.GREEN);

		graphics2D.setStroke(stroke);
		graphics2D.setPaint(paint);

		Path2D path = new GeneralPath(GeneralPath.WIND_NON_ZERO);
		path.moveTo(20,20);
		path.lineTo(100,20);
		path.lineTo(100,100);

		graphics2D.draw(path);


		graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		Font font = graphics2D.getFont().deriveFont(14.f);
		graphics2D.setFont(font);
		FontMetrics fontMetrics = graphics2D.getFontMetrics(font);
		int textHeight = fontMetrics.getHeight();
		int textWidth = fontMetrics.stringWidth("Hello");
		graphics2D.drawString("Hello", 100, 100);
		graphics2D.drawRect(100,100-textHeight,textWidth,textHeight);


		HashMap<TextAttribute,Object> map = new HashMap<>();
		map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
		font = font.deriveFont(map);

		AffineTransform transformX = graphics2D.getTransform();
		AffineTransform transform = new AffineTransform();
		transform.rotate(45);
		graphics2D.transform(transform);
		graphics2D.draw(new Rectangle2D.Float(0,0,100,100));
		graphics2D.setTransform(transformX);

	}

	public static void main(String... args) {

		// 컨텐트 패널에 버튼 등의 컴포넌트를 구성한다.
		final JPanel contentPane = new JPanel();
		contentPane.setBackground(Color.PINK);

		// 프레임을 생성한다.
		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new GraphicsExample());

		// 이벤트 디스패치 스레드에서 프레임을 표시한다.
		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});

	}
}
