10 장 awt(2)

19
10 장 AWT(2)

Upload: truong

Post on 05-Jan-2016

41 views

Category:

Documents


4 download

DESCRIPTION

10 장 AWT(2). AWT(2). Canvas 클래스 Graphics 클래스 Color 클래스 Font 클래스 내부클래스를 사용하는 이벤트 처리. AWT(2). Canvas 클래스 그래픽 처리를 할 수 있는 특정한 모양을 가지지 않는 컴포넌트 void paint(Graphics) 메서드를 오버라이딩하여 직접 그래픽 작업을 처리를 할 수 있고 , getGraphics() 메서드로 Graphics 객체를 얻어와 그래픽 처리를 할 수도 있음. AWT(2). Graphics 클래스 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 10 장  AWT(2)

10 장 AWT(2)

Page 2: 10 장  AWT(2)

2/20

AWT(2)

Canvas 클래스 Graphics 클래스 Color 클래스 Font 클래스 내부클래스를 사용하는 이벤트 처리

Page 3: 10 장  AWT(2)

3/20

AWT(2)

Canvas 클래스 그래픽 처리를 할 수 있는 특정한 모양을 가지지 않는 컴포넌트 void paint(Graphics) 메서드를 오버라이딩하여 직접 그래픽

작업을 처리를 할 수 있고 , getGraphics() 메서드로 Graphics 객체를 얻어와 그래픽 처리를 할 수도 있음

Page 4: 10 장  AWT(2)

4/20

AWT(2)

Graphics 클래스 drawRect() 와 fillRect()

모두 사각형을 그리는 메서드지만 fillRect 메서드는 현재 설정되어있는 색으로 내부를 채운 형태로 그려짐 . 시작점 x, y 좌표와 사각형의 폭과 높이를 인수로 받음

DrawPolygon() 과 fillPolygon() 다각형을 그려주는 메서드로 각 꼭지점의 x 좌표 배열 , y 좌표 배열 ,

꼭지점의 숫자를 인수로 받음

drawRect(int x, int y, int width, int height)fillRect(int x, int y, int width, int height)

drawPolygon(int[] xPoints, int[] yPoints, int nPoint)fillPolygon(int[] xPoints, int[] yPoints, int nPoint)

Page 5: 10 장  AWT(2)

5/20

AWT(2)

drawRoundRect() 와 fillRoundRect() 메서드는 모서리가 둥근 사각형을 그리는데 시작점의 좌표와 폭 ,

높이 , 폭과 높이에 대한 둥근 정도를 인수로 받아 그림

drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

Page 6: 10 장  AWT(2)

6/20

AWT(2)

drawOval(i) 와 fillOval() 원을 그리는 메서드로 x 와 y 를 시작점으로 width 크기의 폭과

height 크기의 높이를 가지는 가상의 사각형 내부에 채워지는 원을 그림

drawOval(int x, int y, int width, int height)fillOval(int x, int y, int width, int height)

drawOval() 메서드로 그려지는 원의 모양

Page 7: 10 장  AWT(2)

7/20

AWT(2)

drawArc() 와 fillArc() 메서드 시작점의 좌표와 폭과 높이 0 도를 기준으로 시작 각과 호의 내부각의

값을 입력받아 호를 그리는 메서드

drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

drawArc() 메서드로 그려지는 호의 모양

Page 8: 10 장  AWT(2)

8/20

AWT(2)

drawLine() 두지점의 좌표를 연결하는 직선을 그리는 메서드이다 .

clearRect() 시작점 , 폭과 높이를 입력받아 선택된 영역의 내용을 지우는

메서드이다 .

drawLine(int xp, int yp, int xr, int yr)

clearRect(int x, int y, int width, int height)

Page 9: 10 장  AWT(2)

9/20

AWT(2)

import java.awt.*; class MyCanvas extends Canvas { // Canvas 클래스 상속     public void paint(Graphics g) { // Canvas 클래스의 paint(Graphics g) 메서드 오버라이딩         g.drawString("Test", 10, 20);         g.drawRect(50, 10, 30, 20);         g.fillRect(100, 10, 30, 20);         int xPos[] = {150, 165, 180, 170, 160}; // 각 꼭지점의 X 좌표를 int 형 배열로 설정한다 .         int yPos[] = {20, 10, 20, 30, 30}; // 각 꼭지점의 Y 좌표를 int 형 배열로 설정한다 .         g.drawPolygon(xPos, yPos, 5);         g.drawRoundRect(10, 50, 40, 40, 10, 10);         g.drawOval(60, 50, 50, 20);         g.fillArc(100, 50, 40, 40, 45, -75);         g.drawLine(10, 110, 170, 130); } } public class CanvasExam {     public static void main(String[] args) {         Frame frm = new Frame("Canva Exam");         Canvas canvas = new MyCanvas();                 frm.setSize(200,180);         frm.add(canvas, "Center");         frm.setVisible(true); } }

Page 10: 10 장  AWT(2)

10/20

AWT(2)

Color 클래스 RGB 값을 사용하여 색 객체를 생성할 수 있음 setColor(Color) 와 같은 색상 관련 메서드를 사용해 컴포넌트나

그래픽 객체의 색을 설정할 수 있음

public Color(int r, int g, int b)

Page 11: 10 장  AWT(2)

11/20

AWT(2)

상수로 정의된 Color 객체

정정적 멤버 변수 기기능

Color.black, Color.BLACK 검정색

Color.blue, Color.BLUE 파란색

Color.cyan Color.CYAN 하늘색

Color.darkGray, Color.DARK_GRAY 짙은 회색

Color.gray, Color.GRAY 회색

Color.green, Color.GREEN 녹색

Color.lightGray, Color.LIGHT_GRAY 옅은 회색

Color.magenta, Color.MAGENTA 진홍색

Color.orange, Color.ORANGE 주황색

Color.pink, Color.PINK 분홍색

Color.red, Color.RED 빨간색

Color.white, Color.WHITE 하얀색

Color.yellow, Color.YELLOW 노란색

Page 12: 10 장  AWT(2)

12/20

AWT(2)

import java.awt.*;

class ColorCanvas extends Canvas {     public void paint(Graphics g) {         setBackground(Color.YELLOW);         setForeground(Color.BLACK);         g.drawString("Color 클래스의 정적멤버변수를 사용하여 ", 10, 20);         g.drawString(" 색상을 설정 할 수 있다 .", 10, 40);

        int color[] = {10, 50, 100, 150, 200, 250 };         for(int index=0; index < color.length; index++) {             g.setColor(new Color(color[index], 0, color[5-index]));             g.drawString("RGB 색상을 선택할 수 있다 .", 10, 70+(20*index)); }  } } public class ColorExam {     public static void main(String args[]) {         Frame frm = new Frame("Color 예제 ");         ColorCanvas canvas = new ColorCanvas();         frm.setSize(300,220);         frm.add(canvas, "Center");         frm.setVisible(true);     } }        

Page 13: 10 장  AWT(2)

13/20

AWT(2)

Font 클래스 Color 클래스와 마찬가지로 Font 클래스의 생성자를 호출해

폰트 객체를 만들고 setFont(Font) 메서드를 사용해 설정 public Font(String name, int style, int size)

name 은 폰트의 이름 , 스타일 , 크기를 지정하며 플랫폼마다 지원되는 폰트가 다르기 때문에 GraphicsEnvironment 클래스의 getAvailableFontFamilyNames() 메서드를 사용해 지원되는 폰트를 먼저 확인해야 함

public Font(String name, int style, int size)

Page 14: 10 장  AWT(2)

14/20

AWT(2)import java.awt.*;

class FontCanvas extends Canvas { public void paint(Graphics g) { GraphicsEnvironment ge = GraphicsEnvironment

.getLocalGraphicsEnvironment(); String fonts[] = ge.getAvailableFontFamilyNames();

for(int index = 0; index < 50; index++) { g.setFont(new Font(fonts[index], Font.BOLD, 20)); g.drawString(fonts[index], 10, 15*index); } }}

public class FontExam { public static void main(String args[]) { Frame frm = new Frame("Font "); frm.setSize(300,900); frm.add(new FontCanvas(), "Center"); frm.setVisible(true); }} 

Page 15: 10 장  AWT(2)

15/20

setLayout(new GridLayout(4,4));

TextField(20);

• Frame.setBounds(10, 10, 200, 250);• TextField

• Panel.setBounds(0, 30, 200, 40);• Button

• Panel.setBounds(0, 70, 200, 180);

public void setBounds(int x, int y, int width, int height)

x - 이 컴퍼넌트의 새로운 x 좌표

y - 이 컴퍼넌트의 새로운 y 좌표

width - 이 컴퍼넌트의 새로운 width

height - 이 컴퍼넌트의 새로운 height

Page 16: 10 장  AWT(2)

16/20

import java.awt.*;

class ButtonPane extends Panel { private Button[] butt = null; private final String[] buttName = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "c", "=", "+" };

public ButtonPane() { setLayout(new GridLayout(4,4)); butt = new Button[buttName.length]; for(int i = 0; i < butt.length; i++) { butt[i] = new Button(buttName[i]); add(butt[i]); } }}

Page 17: 10 장  AWT(2)

17/20

class Calculator { public Calculator() { Frame mainFrame = new Frame("전자계산기 "); mainFrame.setLayout(new BorderLayout()); mainFrame.setBounds(10, 10, 200, 250); TextField display = new TextField(20); display.setText("0"); display.setEditable(false); Panel displayPane = new Panel(); displayPane.setBounds(0, 30, 200, 40); displayPane.add(display); ButtonPane buttonPane = new ButtonPane(); buttonPane.setBounds(0, 70, 200, 180); mainFrame.add("North", displayPane); mainFrame.add("Center", buttonPane); mainFrame.setVisible(true); }

public static void main(String args[]) {

Calculator calculator = new Calculator();

} }

Page 18: 10 장  AWT(2)

18/20

TextArea();

MenuBar mbar = new MenuBar();

Menu menu = new Menu(" 파일 ");

MenuItem mitem = new MenuItem("새파일 ");

menu.add(mitem);

mbar.add(menu);

Frame.setMenuBar(mbar);

Page 19: 10 장  AWT(2)

19/20

import java.awt.*; public class Memojang {

public Memojang() {Frame frame = new Frame();TextArea ta = new TextArea();frame.add(ta,"Center");

MenuBar mbar = new MenuBar();Menu menu = new Menu("파일 ");

MenuItem mitem1 = new MenuItem("새파일 ");MenuItem mitem2 = new MenuItem("열기 ");MenuItem mitem3 = new MenuItem("저장 ");MenuItem mitem4 = new MenuItem("종료 ");menu.add(mitem1);menu.add(mitem2);menu.add(mitem3);menu.add(mitem4);

mbar.add(menu);frame.setMenuBar(mbar);

frame.setSize(600,500);frame.setVisible(true);

}

public static void main(String[] args) {

Memojang memo = new Memojang();

}

}