2011-05-05 5 views

답변

1

체크 아웃 this

여기

내 센트 :

이 프레지와 캔버스입니다. 그것은 매우 간단한 방법이지만 그림이 어떻게 작동하는지 간단한 예입니다.

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseMotionListener; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 


public class DrawPanel extends JPanel { 
    int mxstart, mystart; 
    ArrayList<GObject> objects = new ArrayList<GObject>(); 
    { 
     objects.add(new Rectangle(50, 100, Color.red, 30, 40)); 
     objects.add(new Circle(150, 200, Color.cyan, 50)); 
    } 

    public DrawPanel() { 
     addMouseListener(new MouseAdapter() { 

      public void mouseClicked(MouseEvent e) { 
       mxstart = e.getX(); 
       mystart = e.getY(); 
      } 

      public void mousePressed(MouseEvent e) { 
      mxstart = e.getX(); 
       mystart = e.getY(); 
      } 
     }); 

     addMouseMotionListener(new MouseMotionListener() { 

      public void mouseDragged(MouseEvent e) { 
       int mx=e.getX(); 
       int my=e.getY(); 
       for (GObject go : objects) { 
        if (go.inShape(mx, my)) { 
         go.setX(go.getX()+ (mx-mxstart)); 
         go.setY(go.getY()+ (my-mystart)); 
        } 
       } 
       repaint(); 
       mxstart=mx; 
       mystart=my; 
      } 

      public void mouseMoved(MouseEvent e) {     
      } 
     }); 

    } 

    @Override 
    public void paint(Graphics g) { 
     g.setColor(Color.white); 
     g.fillRect(0,0,2000,1000); 
     for (GObject go : objects) { 
      go.draw(g); 
     } 
    } 

    public static void main(String[] args) { 
     JFrame jf = new JFrame(); 
     jf.add(new DrawPanel()); 
     jf.setBounds(0,0,1000,700); 
     jf.setVisible(true); 

    } 

} 


class GObject { 
     protected int x,y; 
     protected Color col; 

     protected GObject(int x, int y, Color col) { 
      this.x = x; 
      this.y = y; 
      this.col = col; 
     } 

     public Color getCol() { 
      return col; 
     } 

     public void setCol(Color col) { 
      this.col = col; 
     } 

     public int getX() { 
      return x; 
     } 

     public void setX(int x) { 
      this.x = x; 
     } 

     public int getY() { 
      return y; 
     } 

     public void setY(int y) { 
      this.y = y; 
     } 

     protected void draw(Graphics g) {} 

     protected boolean inShape(int x, int y) {return false;} 
    } 

class Rectangle extends GObject { 
    private int w,h; 

    public Rectangle(int x, int y, Color col, int w, int h) { 
     super(x, y, col); 
     this.w = w; 
     this.h = h; 
    } 

    public int getH() { 
     return h; 
    } 

    public void setH(int h) { 
     this.h = h; 
    } 

    public int getW() { 
     return w; 
    } 

    public void setW(int w) { 
     this.w = w; 
    } 

    public boolean inShape(int mx, int my) { 
     if (mx>=x && mx<=x+w && my>=y && my<=y+h) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public void draw(Graphics g) { 
     g.setColor(col); 
     g.drawRect(x, y, w, h); 
    } 

} 

class Circle extends GObject { 
    private int r; 

    public Circle(int x, int y, Color col, int r) { 
     super(x, y, col); 
     this.r = r; 
    } 

    public int getR() { 
     return r; 
    } 

    public void setR(int r) { 
     this.r = r; 
    } 

    public boolean inShape(int mx, int my) { 
     return Math.sqrt((mx-x)*(mx-x)+(my-y)*(my-y))<r; 
    } 

    public void draw(Graphics g) { 
     g.setColor(col); 
     g.drawOval(x, y, r, r); 
    } 

} 
관련 문제