mojo's Blog
명품 JAVA Programming 제 12장 실습문제 (3~4 제외) 본문
1번 문제 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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
int op=0;
MyFrame(){
setTitle("12장 Open Challenge");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
Container c = getContentPane();
c.setLayout(new FlowLayout());
MyButton b =new MyButton("hide/Show");
b.setOpaque(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(op==0) {
op=1;
icon = new ImageIcon("");
img = icon.getImage();
}
else {
op=0;
icon = new ImageIcon("C:\\\\Temp/lion.gif");
img = icon.getImage();
}
repaint();
}
});
c.add(b);
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyButton extends JButton{
public MyButton(String s) {
super(s);
}
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
2번 문제 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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Point p = new Point(-100,-100);
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
p.x = e.getX();
p.y = e.getY();
repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
p.x = -100;
p.y = -100;
repaint();
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
g.setColor(Color.GREEN);
g.fillOval(p.x-20, p.y-40, 30, 30);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
5번 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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Point p = new Point(30,30);
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if(c == '+') {
p.x += (int)(p.x * 0.1);
p.y += (int)(p.y * 0.1);
}
else if(c == '-') {
p.x -= (int)(p.x * 0.1);
p.y -= (int)(p.y * 0.1);
}
repaint();
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 10, 10, p.x, p.y, this);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
6 - (1) 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Point p = new Point(30,30);
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if(c == '+') {
p.x += (int)(p.x * 0.1);
p.y += (int)(p.y * 0.1);
}
else if(c == '-') {
p.x -= (int)(p.x * 0.1);
p.y -= (int)(p.y * 0.1);
}
repaint();
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=1;i<=10;i++) {
int x = getWidth() * i / 11;
int y = getHeight() * i / 11;
g.drawLine(x, 0, x, getHeight());
g.drawLine(0, y, getWidth(), y);
}
repaint();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
6 - (2) 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Point p = new Point(30,30);
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
if(c == '+') {
p.x += (int)(p.x * 0.1);
p.y += (int)(p.y * 0.1);
}
else if(c == '-') {
p.x -= (int)(p.x * 0.1);
p.y -= (int)(p.y * 0.1);
}
repaint();
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=1;i<=10;i++) {
int gap = 10*(i-1);
int x[] = {gap, getWidth()/2, getWidth()-gap, getWidth()/2};
int y[] = {getHeight()/2, gap, getHeight()/2, getHeight()-gap};
for(int j=0; j<4; j++) {
g.drawLine(x[j], y[j], x[j+1==4?0:j+1], y[j+1==4?0:j+1]);
}
}
repaint();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
7번 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Vector<Point> v = new Vector<Point>();
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
v.add(new Point(x,y));
repaint();
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(v.size() >= 2) {
for(int i=0; i<v.size(); i++) {
int x1 = v.get(i).x, y1 = v.get(i).y;
int x2 = v.get(i+1 == v.size() ? 0 : i+1).x;
int y2 = v.get(i+1 == v.size() ? 0 : i+1).y;
g.drawLine(x1, y1, x2, y2);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
8번 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
private Vector<Point> []v = new Vector[100];
private Vector<Point> tmp = new Vector<Point>();
private int size = 0, drag_Op = -1;
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
tmp.add(new Point(e.getX(),e.getY()));
repaint();
drag_Op=1;
}
});
addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
v[size] = new Vector<Point>();
int x1 = tmp.get(0).x, y1 = tmp.get(0).y;
int x2 = tmp.get(tmp.size()-1).x, y2 = tmp.get(tmp.size()-1).y;
v[size].add(new Point(x1,y1));
v[size].add(new Point(x2,y2));
tmp.clear();
repaint();
size++;
drag_Op=0;
}
});
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(drag_Op==1 && tmp.size()>=2) {
int firstX = tmp.get(0).x, firstY = tmp.get(0).y;
int endX = tmp.get(tmp.size()-1).x, endY = tmp.get(tmp.size()-1).y;
int width = endX-firstX, height = endY-firstY;
g.drawOval(firstX, firstY, width, height);
}
if(size>=1) {
for(int i=0; i<size; i++) {
int firstX = v[i].get(0).x, firstY = v[i].get(0).y;
int endX = v[i].get(1).x, endY = v[i].get(1).y;
int width = endX-firstX, height = endY-firstY;
g.drawOval(firstX, firstY, width, height);
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
9번 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel la1 = new BlueLabel(10, Color.YELLOW);
JLabel la2 = new BlueLabel(50, Color.RED);
la1.setText("hello");
la2.setText("Big Hello");
c.add(la1);
c.add(la2);
la2.setBackground(Color.RED);
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class BlueLabel extends JLabel{
public BlueLabel(int size, Color c) {
super.setFont(new Font("Arial",Font.ITALIC,size));
super.setBackground(Color.blue);
super.setForeground(c);
super.setOpaque(true);
}
public void setBackground(Color c) {
super.setBackground(Color.blue);
}
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
10번 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private MyPanel panel = new MyPanel();
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(400, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("C:\\\\Temp/lion.gif");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth()/2-5, getHeight()/2-5,
0, 0, img.getWidth(this)/2-5, img.getHeight(this)/2-5, this);
g.drawImage(img, getWidth()/2+5, 0, getWidth(), getHeight()/2-5,
img.getWidth(this)/2+5, 0, img.getWidth(this), img.getHeight(this)/2-5, this);
g.drawImage(img, 0, getHeight()/2+5, getWidth()/2-5, getHeight(),
0, img.getHeight(this)/2+5, img.getWidth(this)/2-5, img.getHeight(this), this);
g.drawImage(img, getWidth()/2+5, getHeight()/2+5, getWidth(), getHeight(),
img.getWidth(this)/2+5, img.getHeight(this)/2+5, img.getWidth(this), img.getHeight(this), this);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
11번 코드
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.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class MyFrame extends JFrame{
private JTextField tf[] = new JTextField[4];
private JLabel la[] = new JLabel[4];
private JLabel la1[] = new JLabel[4];
private String fruits[] = {"apple","cherry","strawberry","prune"};
private int percent[] = new int[4];
private int sum = 0;
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
int arr[] = new int[4];
sum = 0;
for(int i=0; i<4; i++) {
arr[i] = Integer.parseInt(tf[i].getText());
sum += arr[i];
}
if(sum!=0) {
int tmpSum = 0;
for(int i=0; i<4; i++) {
percent[i] = (int)(((double)arr[i]/(double)sum) * 100);
tmpSum += percent[i];
}
if(tmpSum != 100) percent[3] += (100-tmpSum);
for(int i=0; i<4; i++) {
la1[i].setText(fruits[i]+" "+Integer.toString(percent[i])+"%");
}
}
repaint();
}
}
class NorthPanel extends JPanel{
NorthPanel() {
setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
setBackground(Color.LIGHT_GRAY);
for(int i=0; i<4; i++) {
la[i] = new JLabel(fruits[i]);
add(la[i]);
tf[i] = new JTextField(Integer.toString(0), 6);
tf[i].addActionListener(new MyActionListener());
add(tf[i]);
}
}
}
class CenterPanel extends JPanel{
private int arr[] = new int[4];
CenterPanel(){
setLayout(new FlowLayout(FlowLayout.CENTER,15,10));
for(int i=0; i<4; i++) {
arr[i] = Integer.parseInt(tf[i].getText());
la1[i] = new JLabel(fruits[i]+" %");
la1[i].setForeground(Color.BLACK);
add(la1[i]);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(sum!=0) {
int before = 0;
for(int i=0; i<4 ;i++) {
arr[i] = before + (int)(percent[i]*3.6);
before = arr[i];
System.out.println(arr[i]);
}
la1[0].setForeground(Color.RED); la1[1].setForeground(Color.BLUE);
la1[2].setForeground(Color.MAGENTA); la1[3].setForeground(Color.YELLOW);
g.setColor(Color.RED);
g.fillArc(200, 50, 200, 200, 0, arr[0]);
g.setColor(Color.BLUE);
g.fillArc(200, 50, 200, 200, arr[0], arr[1]-arr[0]);
g.setColor(Color.MAGENTA);
g.fillArc(200, 50, 200, 200, arr[1], arr[2]-arr[1]);
g.setColor(Color.YELLOW);
g.fillArc(200, 50, 200, 200, arr[2], arr[3]-arr[2]);
}
else {
g.setColor(Color.BLACK);
g.fillArc(200, 50, 200, 200, 0, 360);
}
}
}
MyFrame(){
setTitle("12장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(new NorthPanel(), BorderLayout.NORTH);
c.add(new CenterPanel(), BorderLayout.CENTER);
setSize(600, 400);
setVisible(true);
setFocusable(true);
requestFocus();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyFrame();
}
}
'Java' 카테고리의 다른 글
스레드 만들어보기 (0) | 2021.08.08 |
---|---|
Multitasking (0) | 2021.08.08 |
명품 JAVA Programming 제 12장 Open Challenge (0) | 2021.08.04 |
마우스를 이용하여 선 그리기 (0) | 2021.08.04 |
이미지 그리기 / Clipping (0) | 2021.08.04 |
Comments