본문 바로가기
솔루션모음/파워자바 프로그래밍

[파워자바] 15장 프로그래밍 솔루션 답지

by 이얏호이야호 2023. 2. 16.

1.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class MyPanel extends JPanel {

	Font f1, f2, f3, f4, f5;

	public MyPanel() {
		f1 = new Font("Serif", Font.PLAIN, 20);
		f2 = new Font("San Serif", Font.BOLD, 20);
		f3 = new Font("Monospaced", Font.ITALIC, 20);
		f4 = new Font("Dialog", Font.BOLD | Font.ITALIC, 20);
		f5 = new Font("DialogInput", Font.BOLD, 20);
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for(int i=0; i< 10; i++) {
			g.setFont(f1);
			Color color = new Color((int) (Math.random() * 255.0),
					(int) (Math.random() * 255.0), (int) (Math.random() * 255.0));
			g.setColor(color);
			g.drawString("Hello World!", 10, 50+i*20);
		}
	}
}

public class FontTest extends JFrame {
	public FontTest() {
		setSize(300, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Font Test");
		JPanel panel = new MyPanel();
		add(panel);
		setVisible(true);
	}

	public static void main(String[] args) {
		FontTest s = new FontTest();
	}
}

 

2.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class MyPanel extends JPanel {

	Font[] fonts = new Font[5];

	public MyPanel() {
		fonts[0] = new Font("Serif", Font.PLAIN, 20);
		fonts[1] = new Font("San Serif", Font.BOLD, 20);
		fonts[2] = new Font("Monospaced", Font.ITALIC, 20);
		fonts[3] = new Font("Dialog", Font.BOLD | Font.ITALIC, 20);
		fonts[4] = new Font("DialogInput", Font.BOLD, 20);
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for(int i=0; i< 10; i++) {
			g.setFont(fonts[(int)(Math.random()*5.0-0.01)]);
			g.drawString("Hello World!", 10, 50+i*20);
		}
	}
}

public class FontTest extends JFrame {
	public FontTest() {
		setSize(300, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Font Test");
		JPanel panel = new MyPanel();
		add(panel);
		setVisible(true);
	}

	public static void main(String[] args) {
		FontTest s = new FontTest();
	}
}

 

4.

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

class House extends JPanel {
	public House()
	{
		
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2=(Graphics2D) g;
		g2.setStroke(new BasicStroke(3));
		g2.setColor(Color.BLACK);
		g2.draw(new Rectangle2D.Float(50,50,200,200));
		g2.draw(new Rectangle2D.Float(150,150,50,100));
		g2.draw(new Rectangle2D.Float(75,75,20,20));
		g2.draw(new Line2D.Float(150, 20, 50, 50));
		g2.draw(new Line2D.Float(150, 20, 250, 50));
	}
}
class MyFrame extends JFrame{
	public MyFrame()
	{
		setSize(300,300);
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		House H=new House();
		H.setBounds(0, 0, 300, 300);
		add(H);
		setVisible(true);
	}
}
public class Test {
	public static void main(String[] arge)
	{
		MyFrame F=new MyFrame();
	}
}

 

5.

import java.awt.*;
import javax.swing.*;

class BusinessCard extends JPanel {
	public BusinessCard()
	{
		
	}
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		g.drawString("홍길동", 150, 40);
		g.drawString("프로젝트 매니저", 150, 60);
		g.drawString("자바주식회사", 150, 80);
		g.setColor(Color.red);
		g.fillOval(20, 20, 80, 80);
	}
}
class MyFrame extends JFrame{
	public MyFrame()
	{
		setSize(300,150);
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(new BusinessCard());
		setVisible(true);
	}
}
public class Test {
	public static void main(String[] arge)
	{
		MyFrame F=new MyFrame();
	}
}

 

6.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;

class MyPanel extends JPanel 
	{
	 public MyPanel()
		 {
		 super();
		 setBackground( Color.yellow );
		 setSize( 400, 400 );
	 }
	
	 public void paintComponent( Graphics g )
		 {
		 super.paintComponent(g);
		 int xPoints[] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
		 int yPoints[] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
		
		 Graphics2D g2d = ( Graphics2D ) g;
		
		 GeneralPath star = new GeneralPath();
		
		 star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
		
		 for ( int k = 1; k < xPoints.length; k++ )
			 star.lineTo( xPoints[ k ], yPoints[ k ] );
		
		 star.closePath();
		
		 g2d.translate( 100, 100 );
		
		 for ( int j = 1; j <= 5; j++ ) 
			 {
			 g2d.rotate( Math.PI / 30.0 );
			 g2d.setColor(new Color( ( int ) ( Math.random() * 256 ),( int ) ( Math.random() * 256 ), 
			 ( int ) ( Math.random() * 256 ) ) );
			 g2d.fill( star ); // draw a filled star
		 }
	 }
	
}
public class MyFrame extends JFrame{
	public MyFrame()
	{
		setSize(300,300);
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(new MyPanel());
		setVisible(true);
	}
	public static void main(String[] arge)
	{
		MyFrame F=new MyFrame();
	}
}

 

 

7.

/* SkyscraperApplet.java */

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Skyscraper {
	public int x;
	public int y;
	public int width;
	public int height;

	Skyscraper(int x, int y, int w, int h) {
		this.x = x;
		this.y = y;
		this.width = w;
		this.height = h;
	}

	void draw(Graphics g) {
		Color color = new Color((int) (Math.random() * 255.0),
				(int) (Math.random() * 255.0), (int) (Math.random() * 255.0));
		g.setColor(color);

		g.fillRect(x, y, width, height);
	}
}

class MyPanel extends JPanel {
	Skyscraper[] s = new Skyscraper[10];

	public MyPanel() {
		super();
		for (int i = 0; i < 10; i++) {
			s[i] = new Skyscraper((int) (Math.random() * 500),
					(int) (Math.random() * 300), (int) (Math.random() * 500),
					(int) (Math.random() * 300));
		}
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int i = 0; i < 10; i++) {
			s[i].draw(g);
		}

	}
}

public class MyFrame extends JFrame {
	public MyFrame() {
		setSize(500, 300);
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(new MyPanel());
		setVisible(true);
	}

	public static void main(String[] arge) {
		MyFrame F = new MyFrame();
	}
}

 

9.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import javax.swing.JFrame;
import javax.swing.JPanel;

class DoubleDice extends JPanel implements MouseListener {
	int die1 = 4;
	int die2 = 3;

	public DoubleDice() {
		super();
		addMouseListener(this);
		setBackground(new Color(200, 200, 255));
	}

	public void paintComponent(Graphics g) {
		drawDie(g, die1, 10, 10);
		drawDie(g, die2, 55, 55);
	}

	void drawDie(Graphics g, int val, int x, int y) {
		g.setColor(Color.white);
		g.fillRect(x, y, 35, 35);
		g.setColor(Color.black);
		g.drawRect(x, y, 34, 34);
		if (val > 1)
			g.fillOval(x + 3, y + 3, 9, 9);
		if (val > 3)
			g.fillOval(x + 23, y + 3, 9, 9);
		if (val == 6)
			g.fillOval(x + 3, y + 13, 9, 9);
		if (val % 2 == 1)
			g.fillOval(x + 13, y + 13, 9, 9);
		if (val == 6)
			g.fillOval(x + 23, y + 13, 9, 9);
		if (val > 3)
			g.fillOval(x + 3, y + 23, 9, 9);
		if (val > 1)
			g.fillOval(x + 23, y + 23, 9, 9);
	}

	void roll() {
		die1 = (int) (Math.random() * 6) + 1;
		die2 = (int) (Math.random() * 6) + 1;
		repaint();
	}

	public void mousePressed(MouseEvent evt) {
		roll();
	}

	public void mouseReleased(MouseEvent evt) {
	}

	public void mouseClicked(MouseEvent evt) {
	}

	public void mouseEntered(MouseEvent evt) {
	}

	public void mouseExited(MouseEvent evt) {
	}
}

public class MyFrame extends JFrame {
	public MyFrame() {
		setSize(100, 180);
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		add(new DoubleDice());
		setVisible(true);
	}

	public static void main(String[] arge) {
		MyFrame F = new MyFrame();
	}
}

 

10.

// awt와 swing 패키지 포함
import java.awt.*;
import javax.swing.*;

// JComponent를 상속 받는 Mycomponent 클래스 선언
class Car extends JComponent {
	// 자동차의 색상과 위치(좌표), 크기를 받을 필드 선언
	private String color;
	private int size1;
	private int size2;
	private int location1;
	private int location2;
	
	// 각 필드를 매개변수로 받는 생성자 선언
	public Car(String color, int location1, int location2, int size1, int size2){
		this.color = color;
		this.size1 = size1;
		this.size2 = size2;
		this.location1 = location1;
		this.location2 = location2;
	}
	// paint() 메소드를 재정의하여 자동차의 그림을 그림
	public void paint(Graphics g){
		// 자동차 차체의 색상 설정
		g.setColor(Color.green);
		// fillRect()메소드를 사용하여 사용자로부터 입력받은 좌표와 크기로 몸체(사각형)를 그림
		g.fillRect(location1, location2, size1, size2);
		// 창문의 색상 설정
		g.setColor(Color.white);
		/* fillRect()메소드를 사용하여 사용자로부터 입력받은 좌표를 수정해 몸체안에 폭 30, 
                   너비 30의 창문(사각형)을 2개 만듬*/
		g.fillRect(location1+10, location2+10, 30, 30);
		g.fillRect(location1+50, location2+10, 30, 30);
		// 자동차 바퀴의 색상 설정
		g.setColor(Color.black);
		/* fillOval()메소드를 사용하여 사용자로부터 입력받은 좌표를 수정해  폭 25, 너비 25의
                   바퀴(원)을 2개 만듬*/
		g.fillOval(location1, location2+size2, 25, 25);
		g.fillOval(location1+size1-25, location2+size2, 25, 25);
	}
}
public class Test {							// Test 클래스 선언
	public static void main(String args[]){ // 메인 메소드 선언
		// Jframe 타입의 참조변수 frame을 선언해 객체를 가르키도록 함
		JFrame frame = new JFrame();
		frame.setSize(1200, 500);	// 프레임의 사이즈 설정
		frame.setTitle("그리기");		// 타이틀을 "그리기"로 설정
          	// 컴포넌트를 격자모양으로 배치하는 배치관리자 GridLayout 설정
		frame.setLayout(new GridLayout());
		// x버튼을 누를시 프로그램이 종료되도록 설정
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		Car car1 = new Car("blue", 10, 10, 200, 100);	// 첫 번째 car 객체 생성
		Car car2 = new Car("red", 10, 300, 150, 80); 	// 두 번째 car 객체 생성
		Car car3 = new Car("green", 5, 150, 250, 150); 	// 세 번째 car 객체 생성
		Car car4 = new Car("black", 100, 200, 100, 70); 	// 네 번째 car 객체 생성

		// car객체들을 프레임에 포함
		frame.add(car1);
		frame.add(car2);
		frame.add(car3);
		frame.add(car4);
		
		// 프레임을 화면에 보이도록 시각화 함
		frame.setVisible(true);
	}
}

 

 

11.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

class MyPanel extends JPanel implements ActionListener {
	boolean flag = false;
	private int light_number = 0;

	public MyPanel() {
		setLayout(new BorderLayout());
		JButton b = new JButton("신호변경");
		b.addActionListener(this);
		add(b, BorderLayout.SOUTH);
	}

	@Override
	protected void paintComponent(Graphics g) {
	// TODO Auto-generated method stub
		super.paintComponent(g);
		g.setColor(Color.BLACK);
		g.drawOval(100, 100, 100, 100);
		g.drawOval(100, 200, 100, 100);
		g.drawOval(100, 300, 100, 100);
		if (light_number == 0) {
			g.setColor(Color.RED);
			g.fillOval(100, 100, 100, 100);
		} else if (light_number == 1) {
			g.setColor(Color.GREEN);
			g.fillOval(100, 200, 100, 100);
		} else {
			g.setColor(Color.YELLOW);
			g.fillOval(100, 300, 100, 100);
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (++light_number >= 3)
			light_number = 0;
		repaint();
	}
}

public class MyFrame extends JFrame {
	public MyFrame() {
		add(new MyPanel());
		setSize(300, 500);
		setVisible(true);
	}

	public static void main(String[] arg) {
		new MyFrame();
	}	
}

 

 

댓글