/*
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
private MyPanel panel = new MyPanel();
public Main() {
setTitle("fillXXX 사용 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(100,350);
setVisible(true);
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillRect(10,10,50,50);
g.setColor(Color.BLUE);
g.fillOval(10, 70, 50, 50);
g.setColor(Color.GREEN);
g.fillRoundRect(10, 130, 50, 50, 20, 20);
g.setColor(Color.MAGENTA);
g.fillArc(10,190,50,50,0,270);
g.setColor(Color.ORANGE);
int [] x = {30,10,30,60};
int [] y = {250,275,300,275};
g.fillPolygon(x,y,4);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
private MyPanel panel = new MyPanel();
public Main() {
setTitle("4개 도형");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(600,600);
setVisible(true);
}
class MyPanel extends JPanel{
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
g.drawLine(10, 10, 300, 300);
g.setColor(Color.BLACK);
g.drawOval(20, 20, 200, 100);
g.setColor(Color.RED);
int [] x= {500,250,500};
int [] y= {10,10,300};
g.fillPolygon(x,y, 3);
g.setColor(Color.YELLOW);
g.fillArc(300,300, 100, 100, 330, 120);
g.setColor(Color.RED);
g.fillArc(300,300, 100, 100, 90, 120);
g.setColor(Color.BLUE);
g.fillArc(300,300, 100, 100, 210, 120);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame{
private MyPanel panel = new MyPanel();
public Main() {
setTitle("원본 크기로 원하는 위치에 이미지 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(600,600);;
setVisible(true);
}
class MyPanel extends JPanel{
private ImageIcon icon = new ImageIcon("forest.png");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img,20,20,this);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
private MyPanel panel = new MyPanel();
public Main() {
setTitle("패널의 크기에 맞추어 이미지 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(200,300);
setVisible(true);
}
class MyPanel extends JPanel{
private ImageIcon icon= new ImageIcon("forest.png");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
class Main extends JFrame{
private MyPanel panel = new MyPanel();
public Main() {
setTitle("패널의 크기에 맞추어 이미지 그리기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(200,300);
setVisible(true);
}
class MyPanel extends JPanel{
private ImageIcon icon= new ImageIcon("forest.png");
private Image img = icon.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img,20,20,250,100,100,50,200,200,this);
}
}
public static void main(String[] args) {
new Main();
}
}
*/
/*
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
class Main extends JFrame{
private MyPanel panel= new MyPanel();
public Main() {
setTitle("drawing Line by Mouse 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
class MyPanel extends JPanel{
private Vector<Point> vStart = new Vector<Point>();
private Vector<Point> vEnd = new Vector<Point>();
public MyPanel() {
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
Point startP= e.getPoint();
vStart.add(startP);
}
public void mouseReleased(MouseEvent e) {
Point endP= e.getPoint();
vEnd.add(endP);
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
for(int i=0;i<vStart.size();i++) {
Point s= vStart.elementAt(i);
Point e= vEnd.elementAt(i);
g.drawLine((int)s.getX(), (int)s.getY(), (int)e.getX(),(int)e.getY());
}
}
}
}
*/
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
class Main extends JFrame{
private MyPanel panel= new MyPanel();
public Main() {
setTitle("drawing Line by Mouse 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(panel);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
class MyPanel extends JPanel{
int x,y,r,x1;
public MyPanel() {
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
x=getX();
y=getY();
}
public void mouseReleased(MouseEvent e) {
x1=getX();
if(x1>x) {
r=x1-x;
}
else {
r=-(x1-x);
}
repaint();
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawOval(x, y, r*2,r*2);
}
}
}