完成画图板编程,贴出代码 & 看见JDK1.5高兴散分!

jellen 2004-08-19 10:19:00
下面是一个我刚完成的画图板程序,功能有限(可以画矩形,圆,直线;实现了移动,右键删除,保存,读取文件等功能),图片可以用*.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);
}
}

//未完,下面继续
...全文
465 39 打赏 收藏 转发到动态 举报
写回复
用AI写文章
39 条回复
切换为时间正序
请发表友善的回复…
发表回复
edsonwen 2004-12-08
  • 打赏
  • 举报
回复
有没有功能更强大一点的。我还急要呀!
ruowang 2004-08-21
  • 打赏
  • 举报
回复
还不错啊,就是失真现象不叫严重。你可以用java把计算机图形学中的画线算法直接表示在源代码中,虽然比较烦琐,但能较好的处理失真
chinamao 2004-08-21
  • 打赏
  • 举报
回复
羡慕
flashstar 2004-08-21
  • 打赏
  • 举报
回复
对楼主的景仰真是涛涛江水啊!
贴出这么长的代码,但没有注释叫我们如何看啊,这是不行的
jellen 2004-08-21
  • 打赏
  • 举报
回复
To asklxf(xuefeng) :

我是一个刚学Java的小菜鸟(以前有C/C++的编程经验),说实话,我还不知道什么叫MVC(Model View Control?),所以就那刚学的Swing知识编了这个程序。
fengyun1314 2004-08-21
  • 打赏
  • 举报
回复
很好阿
swiminthesea 2004-08-21
  • 打赏
  • 举报
回复
还不错:)
minghuitian 2004-08-20
  • 打赏
  • 举报
回复
up
steel007 2004-08-20
  • 打赏
  • 举报
回复
Up
同桌老王 2004-08-20
  • 打赏
  • 举报
回复
我也要去down1.5
不过楼顶,“move”只能move第一个画的对象。
廖雪峰 2004-08-20
  • 打赏
  • 举报
回复
楼主你不用MVC就写出来了???
pchobby 2004-08-20
  • 打赏
  • 举报
回复
up
Xiaobtutu 2004-08-20
  • 打赏
  • 举报
回复
(2004-08-20 20:47:14) 兔子
(2004-08-20 19:51:23) 兔子
你在吗 我是兔子 我在网上看见你的画图版程序
我有个十万火急的编程任务 和这类似但 圆 线
分装在不同的类 还要在 主类中实现方法
我把它发往你的邮箱了 description.zip是任务要求
Startcode.zip多谢拉 编程天才7谢 谢谢谢谢谢谢

qq 19155994
FxAndyLee 2004-08-20
  • 打赏
  • 举报
回复
你最好加点注释!!!
stamp80 2004-08-20
  • 打赏
  • 举报
回复
感谢共享
liuchuntao 2004-08-20
  • 打赏
  • 举报
回复
up

jamesqsj 2004-08-20
  • 打赏
  • 举报
回复
up
jellen 2004-08-20
  • 打赏
  • 举报
回复
Up!
zsh168 2004-08-20
  • 打赏
  • 举报
回复
好帮你顶一下!
疯云码农 2004-08-20
  • 打赏
  • 举报
回复
不错 支持您~~~~~~
加载更多回复(19)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧