1 / 19

10 장 AWT(2)

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

truong
Download Presentation

10 장 AWT(2)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 10장 AWT(2)

  2. AWT(2) • Canvas 클래스 • Graphics 클래스 • Color 클래스 • Font 클래스 • 내부클래스를 사용하는 이벤트 처리

  3. AWT(2) • Canvas 클래스 • 그래픽 처리를 할 수 있는 특정한 모양을 가지지 않는 컴포넌트 • void paint(Graphics) 메서드를 오버라이딩하여 직접 그래픽 작업을 처리를 할 수 있고, getGraphics() 메서드로 Graphics 객체를 얻어와 그래픽 처리를 할 수도 있음

  4. AWT(2) • Graphics 클래스 • drawRect()와 fillRect() • 모두 사각형을 그리는 메서드지만 fillRect 메서드는 현재 설정되어있는 색으로 내부를 채운 형태로 그려짐. 시작점 x, y 좌표와  사각형의 폭과 높이를 인수로 받음 • DrawPolygon()과 fillPolygon() • 다각형을 그려주는 메서드로 각 꼭지점의 x좌표 배열, y좌표 배열, 꼭지점의 숫자를 인수로 받음

  5. AWT(2) • drawRoundRect()와 fillRoundRect() • 메서드는 모서리가 둥근 사각형을 그리는데 시작점의 좌표와 폭, 높이, 폭과 높이에 대한 둥근 정도를 인수로 받아 그림

  6. AWT(2) • drawOval(i)와 fillOval() • 원을 그리는 메서드로  x와 y를 시작점으로 width 크기의 폭과 height 크기의 높이를 가지는 가상의 사각형 내부에 채워지는 원을 그림 drawOval() 메서드로 그려지는 원의 모양

  7. AWT(2) • drawArc()와 fillArc() 메서드 • 시작점의 좌표와 폭과 높이 0도를 기준으로 시작 각과 호의 내부각의 값을 입력받아 호를 그리는 메서드 drawArc() 메서드로 그려지는 호의 모양

  8. AWT(2) • drawLine() • 두지점의 좌표를 연결하는 직선을 그리는 메서드이다. • clearRect() • 시작점, 폭과 높이를 입력받아 선택된 영역의 내용을 지우는 메서드이다.

  9. 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); } }

  10. AWT(2) • Color 클래스 • RGB값을 사용하여 색 객체를 생성할 수 있음 • setColor(Color)와 같은 색상 관련 메서드를 사용해 컴포넌트나 그래픽 객체의 색을 설정할 수 있음

  11. AWT(2) • 상수로 정의된 Color 객체

  12. 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);     } }        

  13. AWT(2) • Font 클래스 • Color 클래스와 마찬가지로 Font 클래스의 생성자를 호출해 폰트 객체를 만들고 setFont(Font) 메서드를 사용해 설정  public Font(String name, int style, int size) • name은 폰트의 이름, 스타일, 크기를 지정하며 플랫폼마다 지원되는 폰트가 다르기 때문에 GraphicsEnvironment 클래스의 getAvailableFontFamilyNames() 메서드를 사용해 지원되는 폰트를 먼저 확인해야 함

  14. 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); } } 

  15. TextField(20); setLayout(new GridLayout(4,4)); • 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

  16. 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]); } } }

  17. 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(); } }

  18. TextArea(); MenuBar mbar = new MenuBar(); Menu menu = new Menu("파일"); MenuItem mitem = new MenuItem("새파일"); menu.add(mitem); mbar.add(menu); Frame.setMenuBar(mbar);

  19. 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(); } }

More Related