mojo's Blog

JPanel을 상속받아 도형 그리기 / drawString() 메소드 이용 본문

Java

JPanel을 상속받아 도형 그리기 / drawString() 메소드 이용

_mojo_ 2021. 8. 3. 23:55

JPanel 을 상속받아서 도형을 그리는 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 paintJPanelEx extends JFrame{
	
	private MyPanel panel = new MyPanel();
	
	public paintJPanelEx() {
		setTitle("12장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(panel);
		
		setSize(300, 300); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
	}
	
	class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.setColor(Color.BLUE);
			g.drawRect(10, 10, 50, 50); // (10,10) 위치에 50x50 크기의 사각형
			g.drawRect(50, 50, 50, 50);
			g.setColor(Color.MAGENTA);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new paintJPanelEx();
	}

}

 

drawString() 메소드를 이용하여 문자열을 출력하는 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 GraphicsDrawStringEx extends JFrame{
	
	private MyPanel panel = new MyPanel();
	
	public GraphicsDrawStringEx() {
		setTitle("12장 연습");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(panel);
		
		setSize(300, 300); // 프레임 크기 300x300
		setVisible(true); // 프레임 출력
	}
	
	class MyPanel extends JPanel{
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawString("자바는 정말 재밌군", 30, 30);
			g.drawString("좋은 회사 들어가고 싶다", 60, 60);
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GraphicsDrawStringEx();
	}

}

 

Color와 Font를 이용하여 문자열 그리기 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 GraphicsDrawStringEx extends JFrame{
	
	private MyPanel panel = new MyPanel();
	
	public GraphicsDrawStringEx() {
		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.BLUE);
			g.drawString("자바는 정말 재밌군", 30, 30);
			g.setColor(new Color(255,0,0));
			g.setFont(new Font("Arial",Font.ITALIC,30));
			g.drawString("How much?", 30, 60);
			g.setColor(new Color(0x00ff00ff));
			for(int i=1;i<=5;i++) {
				g.setFont(new Font("Jokerman",Font.ITALIC,i*10));
				g.drawString("This much!!",30, 60+i*60);
			}
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GraphicsDrawStringEx();
	}

}

Comments