본문 바로가기
자바

자바 소스코드: Drawing Line by Mouse 예제(repaint)

by 드린 2016. 11. 12.

목차

    반응형
    package javaapplication16;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;
    
    class GraphicsDrawLineMouseEx extends JFrame{
        GraphicsDrawLineMouseEx(){
            this.setTitle("Drawing Line by Mouse 예제");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            MyPanel panel = new MyPanel();
            this.add(panel);
            this.setLocationRelativeTo(null);
            this.setSize(300,300);
            this.setVisible(true);
        }
        class MyPanel extends JPanel{
            Vector<Point> vStart = new Vector<Point>();
            Vector<Point> vEnd = new Vector<Point>();
            
            public MyPanel(){
                this.addMouseListener(new MouseListener(){
                    @Override
                public void mouseClicked(MouseEvent me) {
                }
    
                @Override
                public void mousePressed(MouseEvent me) {
                    Point startP = me.getPoint();
                    vStart.add(startP);
                }
    
                @Override
                public void mouseReleased(MouseEvent me) {
                    Point endP = me.getPoint();
                    vEnd.add(endP);
                    
                    repaint();
                }
    
                @Override
                public void mouseEntered(MouseEvent me) {
                }
    
                @Override
                public void mouseExited(MouseEvent me) {
                }
                });
            }
            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());
                }
            }
        }
    }
    public class JavaApplication16 {
        public static void main(String[] args) {
            new GraphicsDrawLineMouseEx();
        }
    }
    
    

    <결과>

    2016/11/12 - [자바] - 자바 소스코드: Clip 사용 예제(클리핑)

    2016/11/12 - [자바] - 자바 소스코드: drawImage 사용 예제3(이미지의 일부분을 크기 조절하여 그리기

    2016/11/12 - [자바] - 자바 소스코드: drawImage 사용 예제2(패널의 크기에 맞추기)

    #자바 #자바 소스코드 #Drawing Line by Mouse 예제 #Vector #Point #마우스 리스너 #repaint();

    반응형