게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[java swing] 이 소스 좀 봐주예요
게시물ID : programmer_10690짧은주소 복사하기
작성자 : 必*痕
추천 : 0
조회수 : 1142회
댓글수 : 2개
등록시간 : 2015/06/01 21:31:51
옵션
  • 창작글
  • 베스트금지
  • 본인삭제금지
간단한 그림판을 만들기 위한 소스입니다.

많은 조언들을 참고하여 다시 수정해 봤는데요

액션부분에 대한 이해도가 떨어져서 

어떻게 해야 할지 몰르겠습니다.

그래서 일단 오픈소스를 활용하여 그림판을 그리려고 하는데

오류없이 잘 돌아가긴 합니다.

다만 그림이 안 그려질뿐입니다..

혼자서 생각하는게 거기서 거기라 피드백이 불가능 하네요 

어디 물어보고 하소연 할 데가 없어 여기에 풉니다 ㅜㅜ

몇 주 전부터 계속 그림판만 물어보던 사람이 저예요 ㅜㅜ

알아보기 편하시라고 조금 이나마 표시 해둡니다.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

class Paint extends JFrame implements ActionListener, MouseListener,
MouseMotionListener {
private static final long serialVersionUID = 1L;

JPanel mt = new JPanel();// 도구 패널
JPanel ct = new JPanel();// 색깔 패널
String str = "펜";
JButton pen, circle, square, straight, go, full;// 도구 버튼
ImageIcon im1, im2, im3, im4, im5, im6, img; // 도구 이미지

Container con;// 컨테이너

Canvas can = new Canvas();
Color c;
Graphics g; // 그리기를 수행하는 변수
Point p1, p2, p3;
int DRAW_MODE = 0;
// 프레임 출력 메소드
public Paint(String title) {
super("Painting");
this.init();
this.start();
this.setIconImage(im1.getImage());
this.setSize(1000, 1000);
setVisible(true);
}

public void init() {// 초기화 메소드
im1 = new ImageIcon("그림판.png");
im2 = new ImageIcon("수정됨_연필.png");
im3 = new ImageIcon("수정됨_CriCle.png");
im4 = new ImageIcon("수정됨_사각형.png");
im5 = new ImageIcon("수정됨_직선.png");
im6 = new ImageIcon("수정됨_채우기.png");

con = this.getContentPane();
con.setBackground(Color.lightGray);
setLayout(null);
mt();
ct();
canvas();
}

// 캔버스 추가
public void canvas() {
can.setSize(900, 900);
can.setBackground(Color.WHITE);
can.setLocation(40, 50);
add(can);
}

// make tool 버튼 패널
public void mt() { 
pen = new JButton("pen", im2);
circle = new JButton(im3);
square = new JButton(im4);
straight = new JButton(im5);
full = new JButton(im6);

Dimension di = pen.getPreferredSize();
// 도구 상자 크기
pen.setSize(di);
circle.setSize(di);
square.setSize(di);
straight.setSize(di);
full.setSize(di);

// 도구 상자 위치
pen.setLocation(275, 10);
circle.setLocation(375, 10);
square.setLocation(475, 10);
straight.setLocation(575, 10);
full.setLocation(675, 10);

// 도구상자 추가
con.add("North", pen);
con.add("North", circle);
con.add("North", square);
con.add("North", straight);
}

public void ct() { // Color tool 패널
go = new JButton("Other Color...");
Dimension di2 = go.getPreferredSize();
// 색깔 상자 크기
go.setSize(di2);
// 색깔 상자 위치
go.setLocation(850, 10);
// 색깔 상자
go.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
c = JColorChooser.showDialog(
((Component) e.getSource()).getParent(),
"Color Spectrum", c);
if (c == null)
c = Color.black;
}
});
con.add("East", go);
}

public void start() { // 시작 액션 메소드
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.addMouseListener(this);
con.addMouseMotionListener(this);
pen.addActionListener(this);
circle.addActionListener(this);
square.addActionListener(this);
straight.addActionListener(this);
full.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str = e.getActionCommand();
if (str.equals("pen")) {
DRAW_MODE = 1;
} else if (e.getActionCommand().equals(im3)) {
DRAW_MODE = 2;
g.setColor(Color.black);
} else if (e.getActionCommand().equals("im4")) {
DRAW_MODE = 3;
g.setColor(Color.black);
} else if (e.getActionCommand().equals("im5")) {
DRAW_MODE = 4;
g.setColor(Color.black);
}

}

public void DrawLine() {
g = can.getGraphics();
g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2.getX(),
(int) p2.getY());
}

public void DrawRect() {
g = can.getGraphics();
g.draw3DRect((int) p1.getX(), (int) p1.getY(), (int) p2.getX(),
(int) p2.getY(), true);
repaint();
}

public void DrawOval() {
g = can.getGraphics();
g.drawOval((int) p1.getX(), (int) p1.getY(), (int) p2.getX(),
(int) p2.getY());

}

public void mouseDragged(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }

public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}

public void mouseReleased(MouseEvent e) {
p3 = e.getPoint();
switch (DRAW_MODE) {
case 1:
DrawLine();
break;
case 2:
DrawOval();
break;
case 3:
DrawRect();
break;
}
}

public static void main(String[] args) {
Paint painted = new Paint("Painting");
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호