完成画图板编程,贴出代码 & 看见JDK1.5高兴散分!
下面是一个我刚完成的画图板程序,功能有限(可以画矩形,圆,直线;实现了移动,右键删除,保存,读取文件等功能),图片可以用*.jpt格式保存和读取。呵呵,这是我初学Java以来第一个比较大的Java程序(以前搞C/C++的,Java愉快的学习经历是我爱上了她:),希望大家多提意见。
今天我刚安装了J2SDK 1.5 beta2,哇!她的Swing界面好看多了,比Win XP的Forms还PP:) 还有就是运行速度也提高了(我重新编译这个程序可以感觉出来)。呵呵,现在对java又多了一份信心,大家一起交流啊,我的MSN: jellen_2001@163.com QQ: 344030885
/*
* This is a simple drawing program, be compiled with
* J2SDK 1.4.2
*
* Filename: JPaint.java
* Author: jellen
* Date: 8-10-2004
*/
import java.io.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
class Shape {
public static final int LINE = 1;
public static final int SQUARE = 2;
public static final int ELLIPSE = 3;
protected int startX;
protected int startY;
protected int endX;
protected int endY;
protected boolean isFill;
protected boolean isSelected;
protected Color color;
protected int shape;
public Shape() {
isFill = false;
isSelected = false;
color = Color.BLACK;
}
public Shape(int sx, int sy, int ex, int ey) {
this();
startX = sx;
startY = sy;
endX = ex;
endY = ey;
}
public void setStart(int x, int y) {
startX = x;
startY = y;
}
public void setEnd(int x, int y) {
endX = x;
endY = y;
}
public Point2D getStart() {
return new Point2D.Double(startX, startY);
}
public Point2D getEnd() {
return new Point2D.Double(endX, endY);
}
protected int maxX() {
return (startX >= endX) ? startX : endY;
}
protected int minX() {
return (startX <= endX) ? startX : endX;
}
protected int maxY() {
return (startY >= endY) ? startY : endY;
}
protected int minY() {
return (startY <= endY) ? startY : endY;
}
public boolean isContain(int x, int y) {
if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
return true;
else
return false;
}
public void writeData(PrintWriter out)throws IOException {
int fill, rgb;
rgb = color.getRGB();
fill = isFill ? 1 : 0;
out.println(startX + "|" + startY + "|" + endX + "|"
+ endY + "|" + fill + "|" + rgb + "|" + shape);
}
public void readData(BufferedReader in)throws IOException {
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, "|");
startX = Integer.parseInt(t.nextToken());
startY = Integer.parseInt(t.nextToken());
endX = Integer.parseInt(t.nextToken());
endY = Integer.parseInt(t.nextToken());
isFill = (Integer.parseInt(t.nextToken()) == 1) ? true : false;
color = new Color(Integer.parseInt(t.nextToken()));
shape = Integer.parseInt(t.nextToken());
isSelected = false;
}
}
class Line extends Shape {
public Line() {
super();
shape = LINE;
}
public Line(int sx, int sy, int ex, int ey) {
super(sx, sy, ex, ey);
shape = LINE;
}
public boolean isContain(int x, int y) {
double c = Math.sqrt((endX - startX) * (endX - startX) +
(endY - startY) * (endY - startY));
double c1 = Math.sqrt((x - startX) * (x - startX) +
(y - startY) * (y - startY));
double c2 = Math.sqrt((x - endX) * (x - endX) +
(y - endY) * (y - endY));
if(c1+c2-c <= 0.25*c)
return true;
else
return false;
}
}
class Square extends Shape {
public Square() {
super();
shape = SQUARE;
}
public Square(int sx, int sy, int ex, int ey) {
super(sx, sy, ex, ey);
shape = SQUARE;
}
public boolean isContain(int x, int y) {
if(x >= minX() && x <= maxX() && y >= minY() && y <= maxY())
return true;
else
return false;
}
}
class Ellipse extends Shape {
public Ellipse() {
super();
shape = ELLIPSE;
}
public Ellipse(int sx, int sy, int ex, int ey) {
super(sx, sy, ex, ey);
shape = ELLIPSE;
}
public boolean isContain(int x, int y) {
double dx = (double)(startX + endX) / 2;
double dy = (double)(startY + endY) / 2;
double a = (maxX() - minX()) / 2;
double b = (maxY() - minY()) / 2;
double x1 = (x - dx)*(x - dx) / (a*a);
double y1 = (y - dy)*(y - dy) / (b*b);
if(x1+y1 <= 1)
return true;
else
return false;
}
}
class ButtonPanel extends JPanel {
JButton cusorButton = new JButton("Move");
JButton lineButton = new JButton("Line");
JButton squareButton = new JButton("Square");
JButton ellipseButton = new JButton("Ellipse");
JButton colorButton = new JButton("Color");
public ButtonPanel() {
cusorButton.setToolTipText("Move the shape");
lineButton.setToolTipText("Draw a line");
squareButton.setToolTipText("Draw a rectangle");
ellipseButton.setToolTipText("Draw a ecllipse");
colorButton.setToolTipText("Choose the draw color");
add(cusorButton);
add(lineButton);
add(squareButton);
add(ellipseButton);
add(colorButton);
}
}
//未完,下面继续