怎么没人理我?得,还是自己写的!
初次用JAVA,献丑了。有需要的拿去交作业吧,呵呵。
GraphEdt.java
----------------------------------------------------------------------------
/*
* 基于图形界面的图形交互程序(Applet program)
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.util.*;
/*
* 主程序
*/
public class GraphEdt extends Applet {
DrawPanel panel;
DrawControls controls;
public void init() {
setLayout(new BorderLayout());
panel = new DrawPanel();
controls = new DrawControls(panel, this);
add("Center", panel);
add("South",controls);
}
public void destroy() {
remove(panel);
remove(controls);
}
public static void main(String args[]) {
Frame MainFrame = new Frame("GraphEdt");
GraphEdt GraphEidter = new GraphEdt();
GraphEidter.init();
GraphEidter.start();
MainFrame.add("Center", GraphEidter);
MainFrame.setSize(640, 400);
MainFrame.show();
}
protected Frame getFrame() {
for (Container p = getParent();
p != null;
p = p.getParent())
if (p instanceof Frame) return (Frame) p;
return null;
}
public String getAppletInfo() {
return "基于图形界面的图形交互程序(Applet program).作者:OopsWare";
}
}
class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
public static final intdo
LINE = 0;
public static final intdo
CIRC = 1;
public static final intdo
TRIA = 2;
public static final intdo
MODI = 3;
int mode =do
LINE;
int step = 0;
int a1,b1;
int a2,b2;
int a3,b3;
public Choice shapes = null;
public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}
public void setDrawMode(int mode) {
switch (mode) {
casedo
LINE:
casedo
CIRC:
casedo
TRIA:
casedo
MODI:
this.mode = mode;
step = 0;
break;
default:
throw new IllegalArgumentException();
}
}
public void mouseDragged(MouseEvent e) {
e.consume();
if ((mode ==do
MODI) &&
(step != 0) &&
(shapes != null)){
String[] sarg = shapes.getSelectedItem().split(",");
if (sarg.length == 7){
switch(step){
case 1:
sarg[1] = String.valueOf(e.getX());
sarg[2] = String.valueOf(e.getY());
break;
case 2:
sarg[3] = String.valueOf(e.getX());
sarg[4] = String.valueOf(e.getY());
break;
case 3:
sarg[5] = String.valueOf(e.getX());
sarg[6] = String.valueOf(e.getY());
break;
}
// 不知道为什么 Choice 没有 repleace 用法???
String mds = sarg[0] + "," + sarg[1] + "," + sarg[2] + "," + sarg[3] + ","
+ sarg[4] + "," + sarg[5] + "," + sarg[6];
int i = shapes.getSelectedIndex();
shapes.remove(i);
shapes.insert(mds, i);
shapes.select(i);
repaint();
}
}
}
public void mousePressed(MouseEvent e) {
e.consume();
String[] sarg;
if ((mode ==do
MODI) &&
(step == 0) &&
(shapes != null)){
sarg = shapes.getSelectedItem().split(",");
if (sarg.length == 7){
a1 = Integer.parseInt(sarg[1]);
b1 = Integer.parseInt(sarg[2]);
a2 = Integer.parseInt(sarg[3]);
b2 = Integer.parseInt(sarg[4]);
a3 = Integer.parseInt(sarg[5]);
b3 = Integer.parseInt(sarg[6]);
int x = e.getX();
int y = e.getY();
if((x>(a1-2)) &&
(x<(a1+2)) &&
(y>(b1-2)) &&
(y<(b1+2))) {
step = 1;
} else
if((x>(a2-2)) &&
(x<(a2+2)) &&
(y>(b2-2)) &&
(y<(b2+2))) {
step = 2;
} else
if((x>(a3-2)) &&
(x<(a3+2)) &&
(y>(b3-2)) &&
(y<(b3+2))) {
step = 3;
}
// For Debug:
// if (step != 0) repaint();
}
}
}
public void mouseReleased(MouseEvent e) {
e.consume();
if ((mode ==do
MODI) &&
(step != 0) &&
(shapes != null)){
step = 0;
repaint();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
e.consume();
switch (mode) {
casedo
LINE:
switch (step){
case 0:
a1 = e.getX();
b1 = e.getY();
a2 = a1;
b2 = b1;
step++;
break;
case 1:
a2 = e.getX();
b2 = e.getY();
shapes.addItem( "line,"
+ String.valueOf(a1) + ","
+ String.valueOf(b1) + ","
+ String.valueOf(a2) + ","
+ String.valueOf(b2) + ",0,0" );
step = 0;
break;
}
break;
casedo
CIRC:
switch (step){
case 0:
a1 = e.getX();
b1 = e.getY();
a2 = a1;
b2 = b1;
step++;
break;
case 1:
a2 = e.getX();
b2 = e.getY();
shapes.addItem( "circle,"
+ String.valueOf(a1) + ","
+ String.valueOf(b1) + ","
+ String.valueOf(a2) + ","
+ String.valueOf(b2) + ",0,0" );
step = 0;
break;
}
break;
casedo
TRIA:
switch (step){
case 0:
a1 = e.getX();
b1 = e.getY();
a2 = a1;
b2 = b1;
step++;
break;
case 1:
a2 = e.getX();
b2 = e.getY();
a3 = a2;
b3 = b2;
step++;
break;
case 2:
a3 = e.getX();
b3 = e.getY();
shapes.addItem( "triangle,"
+ String.valueOf(a1) + ","
+ String.valueOf(b1) + ","
+ String.valueOf(a2) + ","
+ String.valueOf(b2) + ","
+ String.valueOf(a3) + ","
+ String.valueOf(b3));
step = 0;
break;
}
break;
casedo
MODI:
break;
}
repaint();
}
public void mouseMoved(MouseEvent e) {
e.consume();
switch (mode) {
casedo
LINE:
switch (step){
case 1:
a2 = e.getX();
b2 = e.getY();
repaint();
break;
}
break;
casedo
CIRC:
switch (step){
case 1:
a2 = e.getX();
b2 = e.getY();
repaint();
break;
}
break;
casedo
TRIA:
switch (step){
case 1:
a2 = e.getX();
b2 = e.getY();
repaint();
break;
case 2:
a3 = e.getX();
b3 = e.getY();
repaint();
break;
}
break;
casedo
MODI:
break;
}
}
public void DrawSign(Graphics g, int x, int y) {
g.setColor(Color.red);
g.drawRect(x-2, y-2, 4, 4);
}
public void paint(Graphics g) {
String s;
String[] sarg;
if (shapes != null){
int j = shapes.getSelectedIndex();
for(int i=0;
i < shapes.getItemCount();
i++ ){
g.setColor( (i==j)?(Color.blue)
Color.black) );
s = shapes.getItem(i);
sarg = s.split(",");
if (sarg.length == 7) {
if (sarg[0].equals("line")){
g.drawLine(Integer.parseInt(sarg[1]),
Integer.parseInt(sarg[2]),
Integer.parseInt(sarg[3]),
Integer.parseInt(sarg[4]));
} else
if (sarg[0].equals("circle")){
int x1 = Math.abs( Integer.parseInt(sarg[1]) - Integer.parseInt(sarg[3]) );
int y1 = Math.abs( Integer.parseInt(sarg[2]) - Integer.parseInt(sarg[4]) );
do
uble dz = Math.sqrt( x1 * x1 + y1 * y1 );
Long z = new Long(Math.round( dz ));
int w = z.intValue();
g.drawOval(Integer.parseInt(sarg[1]) - w,
Integer.parseInt(sarg[2]) - w,
w * 2, w * 2);
} else
if (sarg[0].equals("triangle")){
g.drawLine(Integer.parseInt(sarg[1]),
Integer.parseInt(sarg[2]),
Integer.parseInt(sarg[3]),
Integer.parseInt(sarg[4]));
g.drawLine(Integer.parseInt(sarg[1]),
Integer.parseInt(sarg[2]),
Integer.parseInt(sarg[5]),
Integer.parseInt(sarg[6]));
g.drawLine(Integer.parseInt(sarg[5]),
Integer.parseInt(sarg[6]),
Integer.parseInt(sarg[3]),
Integer.parseInt(sarg[4]));
}
if ((mode==DOMODI)&&(i==j)) {
if (sarg[0].equals("triangle")) {
DrawSign(g, Integer.parseInt(sarg[1]), Integer.parseInt(sarg[2]));
DrawSign(g, Integer.parseInt(sarg[3]), Integer.parseInt(sarg[4]));
DrawSign(g, Integer.parseInt(sarg[5]), Integer.parseInt(sarg[6]));
} else
{
DrawSign(g, Integer.parseInt(sarg[1]), Integer.parseInt(sarg[2]));
DrawSign(g, Integer.parseInt(sarg[3]), Integer.parseInt(sarg[4]));
}
}
} // end of "if (sarg.length == 7)"
} // end of "for loop"
}
g.setColor(Color.red);
switch (mode) {
casedo
LINE:
switch (step){
case 1:
g.drawLine(a1, b1, a2, b2);
break;
}
break;
casedo
CIRC:
switch (step){
case 1:
int x1 = Math.abs( a1 - a2 );
int y1 = Math.abs( b1 - b2 );
do
uble dz = Math.sqrt( x1 * x1 + y1 * y1 );
Long z = new Long(Math.round( dz ));
int w = z.intValue();
g.drawOval(a1 - w, b1 - w, w * 2, w * 2);
break;
}
break;
casedo
TRIA:
switch (step){
case 1:
g.drawLine(a1, b1, a2, b2);
break;
case 2:
g.drawLine(a1, b1, a2, b2);
g.drawLine(a1, b1, a3, b3);
g.drawLine(a3, b3, a2, b2);
break;
}
break;
casedo
MODI:
break;
}
// For Debug:
// s = "Mode: " + String.valueOf(mode) + " Step:" + String.valueOf(step);
// g.drawString(s, 10, 10);
}
}
class DrawControls extends Panel implements ItemListener, ActionListener {
DrawPanel target;
Choice shapes;
GraphEdt editor;
static FileDialog fileDialog = null;
String fname = "";
public DrawControls(DrawPanel target, GraphEdt editor) {
this.target = target;
this.editor = editor;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
setFont(new java.awt.Font("宋体", 0, 12));
Choice operats = new Choice();
operats.addItemListener(this);
operats.addItem("line");
operats.addItem("circle");
operats.addItem("triangle");
operats.addItem("modify");
add(operats);
shapes = new Choice();
shapes.addItemListener(this);
// 设置下拉框宽度 ????
shapes.addItem(" ");
add(shapes);
// shapes.setSize(200, 20);
// repaint();
target.shapes = shapes;
Button b;
b = new Button("删除");
b.addActionListener(this);
add(b);
b = new Button("清除");
b.addActionListener(this);
add(b);
b = new Button("保存");
b.addActionListener(this);
add(b);
b = new Button("读取");
b.addActionListener(this);
add(b);
}
public void itemStateChanged(ItemEvent e) {
String choice = (String) e.getItem();
if (choice.equals("line")) {
target.setDrawMode(DrawPanel.DOLINE);
} else
if (choice.equals("circle")) {
target.setDrawMode(DrawPanel.DOCIRC);
} else
if (choice.equals("triangle")) {
target.setDrawMode(DrawPanel.DOTRIA);
} else
if (choice.equals("modify")) {
target.setDrawMode(DrawPanel.DOMODI);
}
target.repaint();
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("读取")) {
if (fileDialog == null)
fileDialog = new FileDialog(editor.getFrame());
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setTitle("打开...");
fileDialog.show();
if (fileDialog.getFile() != null) {
fname = fileDialog.getDirectory() + fileDialog.getFile();
try {
shapes.removeAll();
Reader fr = new FileReader(fname);
BufferedReader br = new BufferedReader(fr);
String l;
while ( (l = br.readLine()) != null )
shapes.addItem(l);
fr.close();
}
catch (IOException ioerr) {
System.err.println(ioerr.toString());
}
target.repaint();
}
} else
if (label.equals("保存")){
if(fname == ""){
if (fileDialog == null)
fileDialog = new FileDialog(editor.getFrame());
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setTitle("保存为...");
fileDialog.show();
if (fileDialog.getFile() != null) fname = fileDialog.getDirectory() + fileDialog.getFile();
}
if(fname != ""){ // Save to file..
// 注意:默认情况下是无文件写权限的,需要在 {$java.home}/lib/security/java.policy 文件中加入以下信息
// permission java.io.FilePermission "c://java//-", "read";
// permission java.io.FilePermission "c://java//-", "write";
try {
Writer fw = new FileWriter(fname);
String ss;
for(int i=0;
i < shapes.getItemCount();
i++ ){
ss = shapes.getItem(i);
fw.write(ss, 0, ss.length());
fw.write("/r/n", 0, 2);
}
fw.close();
}
catch (IOException ioerr) {
System.err.println(ioerr.toString());
}
}
} else
if (label.equals("删除")){
int di = shapes.getSelectedIndex();
if (di >= 0) shapes.remove(di);
target.repaint();
} else
if (label.equals("清除")){
shapes.removeAll();
target.repaint();
}
}
}
-----------------------------------------------------------------------------
以前可以自己删帖子的,现在怎么不让删了?分少的来领分,24小时内结束此贴。