mojo's Blog

도형 그리기와 칠하기 본문

Java

도형 그리기와 칠하기

_mojo_ 2021. 8. 4. 00:09

선 그리기 code

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;

public class GraphicsDrawLineEx extends JFrame{
	
	private MyPanel panel = new MyPanel();
	
	public GraphicsDrawLineEx() {
		setTitle("12장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(panel);
		
		setSize(350, 470); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
	}
	
	class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.drawLine(20, 20, 100, 100);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GraphicsDrawLineEx();
	}

}

 

도형 칠하기 code

import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;

public class GraphicsFillEx extends JFrame{
	
	private MyPanel panel = new MyPanel();
	
	public GraphicsFillEx() {
		setTitle("12장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(panel);
		
		setSize(350, 500); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
	}
	
	class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.RED);
			g.fillRect(10, 10, 50, 50); //(10,10)에서 50x50 사각형 칠하기
			g.setColor(Color.BLUE);
			g.fillOval(10, 70, 50, 50); //(10,70) 에서 50x50 타원 칠하기
			g.setColor(Color.GREEN);
			g.fillRoundRect(10, 130, 50, 50, 20, 20);
			g.setColor(Color.MAGENTA);
			g.fillArc(10, 190, 50, 50, 0, 270);
			g.setColor(Color.ORANGE);
			int []x= {30,10,30,60};
			int []y= {250,275,300,275};
			g.fillPolygon(x,y,4); // x와 y의 좌표를 가지고 그리기
			
			int []x1= {30,10,60};
			int []y2= {300,350,325};
			g.fillPolygon(x1,y2,3);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GraphicsFillEx();
	}

}

Comments