티스토리 뷰
반응형
✏️ 12장 실습문제 - 1번
// 1번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
public SolveProblem() {
setTitle("이미지 그리기 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400, 200);
}
class MyPanel extends JPanel {
private JButton HideShow = new JButton("Hide/Show");
private ImageIcon icon = new ImageIcon("images/티스토리.jpg");
private Image image = icon.getImage();
public MyPanel() {
setLayout(new FlowLayout());
add(HideShow);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
//2번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
public SolveProblem() {
setTitle("이미지 그리기 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400, 200);
}
class MyPanel extends JPanel {
private JButton HideShow = new JButton("Hide/Show");
private ImageIcon icon = new ImageIcon("images/티스토리.jpg");
private Image image = icon.getImage();
public MyPanel() {
setLayout(new FlowLayout());
add(HideShow);
HideShow.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(image==null)
image = icon.getImage();
else
image=null;
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 2번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
public SolveProblem() {
setTitle("이미지 위에 원 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400, 200);
}
class MyPanel extends JPanel {
private JButton HideShow = new JButton("Hide/Show");
private ImageIcon icon = new ImageIcon("images/티스토리.jpg");
private Image image = icon.getImage();
private Point point = new Point(50, 50);
public MyPanel() {
setLayout(new FlowLayout());
add(HideShow);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
point = e.getPoint();
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
g.setColor(Color.GREEN);
g.fillOval((int)point.getX(),(int)point.getY(),20,20);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 3번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class SolveProblem extends JFrame {
private ImageIcon icon = new ImageIcon("images/네블.jpg");
private JLabel label = new JLabel(icon);
public SolveProblem() {
setTitle("이미지 레이블 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
label.setLocation(50,50);
label.setSize(icon.getIconWidth(),icon.getIconHeight());
c.add(label);
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
Point p = e.getPoint();
label.setLocation((int)p.getX(),(int)p.getY());
}
});
setSize(400, 200);
setVisible(true);
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 4번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private ImageIcon appleIcon = new ImageIcon("images/1.png");
private Image appleImage = appleIcon.getImage();
private int x = 50;
private int y = 50;
private int width = appleImage.getWidth(this);
private int height = appleImage.getHeight(this);
public SolveProblem() {
setTitle("이미지 레이블 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(500, 500);
setVisible(true);
}
class MyPanel extends JPanel {
boolean okayToMove = false;
public MyPanel() {
setFocusable(true);
requestFocus();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { //마우스가 눌러졌을 때
Point startP = e.getPoint(); //현재 마우스의 위치
if (startP.getX() >= x && startP.getX() <= x + width)
if (startP.getY() >= y && startP.getY() <= y + height)
okayToMove = true;
//마우스가 이미지 영역 안에 있는지 확인하고 이동 가능하다는 것을 표현
}
public void mouseReleased(MouseEvent e) { //눌렀던 버튼을 뗐을 때
if (okayToMove == true) { //이동 가능한 상태였다면
Point endP = e.getPoint(); //현재 마우스 위치
x = (int) endP.getX() - width / 2;
y = (int) endP.getY() - height / 2; //이동할 위치 설정
okayToMove = false;
repaint(); //변경된 위치에 출력
}
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(appleImage, x, y, width, height, this);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 5번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private ImageIcon appleIcon = new ImageIcon("images/가위.jpg");
private Image appleImage = appleIcon.getImage();
private int x = 50;
private int y = 50;
private int width = appleImage.getWidth(this);
private int height = appleImage.getHeight(this);
public SolveProblem() {
setTitle("이미지 레이블 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(500, 500);
setVisible(true);
}
class MyPanel extends JPanel {
public MyPanel() {
setFocusable(true);
requestFocus();
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()=='+'){
width *=1.1;
height*=1.1;
}
else if(e.getKeyChar()=='-'){
width*=0.9;
height*=0.9;
}
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(appleImage, x, y, width, height, this);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 6번
// 1번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private ImageIcon appleIcon = new ImageIcon("images/가위.jpg");
private Image appleImage = appleIcon.getImage();
private int x = 50;
private int y = 50;
private int width = appleImage.getWidth(this);
private int height = appleImage.getHeight(this);
public SolveProblem() {
setTitle("이미지 레이블 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(500, 500);
setVisible(true);
}
class MyPanel extends JPanel {
int width = this.getWidth() / 10;
int height = this.getHeight() / 10;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 10; i++) {
g.drawLine(0, (this.getHeight() / 10) * i, this.getWidth(), (this.getHeight() / 10) * i);
g.drawLine((this.getWidth() / 10) * i, 0, (this.getWidth() / 10) * i, this.getHeight());
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
// 2번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private ImageIcon appleIcon = new ImageIcon("images/가위.jpg");
private Image appleImage = appleIcon.getImage();
private int x = 50;
private int y = 50;
private int width = appleImage.getWidth(this);
private int height = appleImage.getHeight(this);
public SolveProblem() {
setTitle("이미지 레이블 드래깅 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(500, 500);
setVisible(true);
}
class MyPanel extends JPanel {
int width = this.getWidth() / 2;
int height = this.getHeight() / 2;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int w = this.getWidth();
int h = this.getHeight();
int[] x = { w / 2, 0, w / 2, w }; // 위 왼쪽 아래 오른쪽
int[] y = { 0, h / 2, h, h / 2 };
int xSpace = (int) this.getWidth() / 30;
int ySpace = (int) this.getHeight() / 30;
g.drawPolygon(x, y, 4);
for (int i = 1; i < 10; i++) {
y[0] += ySpace;
x[1] += xSpace;
y[2] -= ySpace;
x[3] -= xSpace;
g.drawPolygon(x, y, 4);
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 7번
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private int []x = new int[100];
private int []y = new int[100];
private int index=0;
public SolveProblem(){
setTitle("마우스로 폐다각형 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400,200);
}
class MyPanel extends JPanel{
public MyPanel(){
setFocusable(true);
requestFocus();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
x[index]=(int)p.getX();
y[index]=(int)p.getY();
index++;
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawPolygon(x,y,index);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 8번
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private Vector<Circle> v = new Vector<>();
private int x, y, width, height;
Point p;
public SolveProblem() {
setTitle("마우스로 원 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400, 200);
}
class MyPanel extends JPanel {
Point startP, endP;
public MyPanel() {
setFocusable(true);
requestFocus();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) { // 중심 좌표를 Circle 객체에 만들어서 저장
startP = e.getPoint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
endP = e.getPoint();
x = (int) startP.getX();
y = (int) endP.getY();
width = (int) (Math.sqrt((Math.pow(startP.x - endP.x, 2) + Math.pow(startP.y - endP.y, 2))) * 2);
height = (int) (Math.sqrt((Math.pow(startP.x - endP.x, 2) + Math.pow(startP.y - endP.y, 2))) * 2);
Circle circle = new Circle(x,y,width,height);
v.add(circle);
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i<v.size();i++){
g.drawOval(v.get(i).x,v.get(i).y,v.get(i).width,v.get(i).height);
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
class Circle {
public int x, y, width, height;
public Circle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
✏️ 12장 실습문제 - 9번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame{
public SolveProblem() {
setTitle("Blue Label 만들기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c =getContentPane();
c.setLayout(new FlowLayout());
MyPanel helloLabel = new MyPanel("hello");
helloLabel.setForeground(Color.YELLOW);
helloLabel.setFont(new Font("Arial",Font.ITALIC,10));
helloLabel.setOpaque(true);
MyPanel BigHelloLabel = new MyPanel("Big Hello");
BigHelloLabel.setForeground(Color.YELLOW);
BigHelloLabel.setFont(new Font("Arial",Font.ITALIC,10));
BigHelloLabel.setOpaque(true);
c.add(helloLabel);
c.add(BigHelloLabel);
setVisible(true);
setSize(400,200);
}
class MyPanel extends JLabel {
public MyPanel(String s){
super(s);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.blue);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 10번
package ex;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame {
private MyPanel panel = new MyPanel();
private ImageIcon icon = new ImageIcon("images/인스타.jpg");
private Image image = icon.getImage();
private int width, height;
private int[] x = new int[4]; // 이미지를 그리기 시작할 좌표
private int[] y = new int[4]; // 이미지를 그리기 시작할 좌표
public SolveProblem() {
setTitle("그래픽 이미지 연습");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setVisible(true);
setSize(400, 400);
}
class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int imgwidth = this.getWidth();
int imgheight = this.getHeight();
g.drawImage(image, 0, 0, (width-10)/2, (height-10)/2, 0, 0, imgwidth/2, imgheight/2, this);
g.drawImage(image, (width+10)/2, 0, width, (height-10)/2, imgwidth/2, 0, imgwidth, imgheight/2, this);
g.drawImage(image, 0, (height+10)/2, (width-10)/2, height, 0, imgheight/2, imgwidth/2, imgheight, this);
g.drawImage(image, (width+10)/2, (height+10)/2, width, height, imgwidth/2, imgheight/2, imgwidth, imgheight, this);
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
✏️ 12장 실습문제 - 11번
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SolveProblem extends JFrame{
JTextField[] amount=new JTextField[4];
int sum=0;
String[] fruit= {"apple", "cherry", "strawverry", "prune"};
ImageIcon image=new ImageIcon("image/blue_square.png");
Image img=image.getImage();
SolveProblem(){
setSize(550,400);
setTitle("파이 차트 그리기");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(new MyKeyListener());
JPanel base=new JPanel();
base.setLayout(new FlowLayout());
JLabel[] specise=new JLabel[4];
base.setBackground(Color.LIGHT_GRAY);
base.setOpaque(true);
for(int i=0; i<4; i++) {
amount[i]=new JTextField(7);
specise[i]=new JLabel(fruit[i]);
amount[i].addKeyListener(new MyKeyListener());
amount[i].setText("0");
base.add(specise[i]);
base.add(amount[i]);
}
MyPanel graph=new MyPanel();
setContentPane(graph);
add(base, BorderLayout.NORTH);
setVisible(true);
requestFocus();
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
int apple=0, cherry=0, strawberry=0, prune=0;
super.paintComponent(g);
for(int i=0; i<4; i++) {
sum+=Integer.parseInt(amount[i].getText());
}
if(sum!=0) {
apple=(int)((Integer.parseInt(amount[0].getText())*360)/sum);
cherry=(int)((Integer.parseInt(amount[1].getText())*360)/sum);
strawberry=(int)((Integer.parseInt(amount[2].getText())*360)/sum);
prune=(int)((Integer.parseInt(amount[3].getText())*360)/sum);
g.setFont(new Font("arial", Font.BOLD, 10));
g.setColor(Color.RED);
g.drawString("apple "+(int)(apple*100/360)+"%", 60,60);
g.fillArc(125,100,200,200,0,apple);
g.setColor(Color.BLUE);
g.drawString("cherry "+(int)(cherry*100/360)+"%", 160,60);
g.fillArc(125,100,200,200,apple,cherry);
g.setColor(Color.MAGENTA);
g.drawString("strawberry "+(int)(strawberry*100/360)+"%", 260,60);
g.fillArc(125,100,200,200,apple+cherry,+strawberry);
g.setColor(Color.YELLOW);
g.drawString("prune "+(int)(prune*100/360)+"%", 360, 60);
g.fillArc(125,100,200,200,apple+cherry+strawberry,prune);
}
}
}
class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent e) {
if(e.getKeyChar()=='\n') {
repaint();
}
}
}
public static void main(String[] args) {
new SolveProblem();
}
}
반응형
'PL > JAVA' 카테고리의 다른 글
명품 자바 프로그래밍 14장 실습 문제 (0) | 2024.06.26 |
---|---|
명품 자바 프로그래밍 11장 실습 문제 (0) | 2024.06.22 |
명품 자바 프로그래밍 10장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 9장 실습 문제 (0) | 2024.06.21 |
명품 자바 프로그래밍 8장 실습 문제 (0) | 2024.06.21 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- js
- 에라토스테네스의 체
- 알고리즘
- 백준 풀이
- 자바
- java
- C++ Stack
- 유니온 파인드
- Do it!
- 백준
- HTML5
- 자료구조
- c++ string
- 자바스크립트
- 스프링 부트 crud 게시판 구현
- BFS
- 반복문
- CSS
- C++
- 세그먼트 트리
- 투 포인터
- 카운팅 정렬
- 우선순위 큐
- 스택
- DFS
- 알고리즘 공부
- html
- 유클리드 호제법
- DP
- 이분 매칭
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함