java高手请进:求一编程:

2024-12-04 09:55:01
推荐回答(3个)
回答1:

import java.awt.event.*;
import java.awt.*;
import java.applet.*;

import java.util.Vector;

public class DrawTest extends Applet{
DrawPanel panel;
DrawControls controls;

public void init() {
setLayout(new BorderLayout());
panel = new DrawPanel();
controls = new DrawControls(panel);
add("Center", panel);
add("South",controls);
}

public void destroy() {
remove(panel);
remove(controls);
}

public static void main(String args[]) {
Frame f = new Frame("DrawTest");
DrawTest drawTest = new DrawTest();
drawTest.init();
drawTest.start();

f.add("Center", drawTest);
f.setSize(300, 300);
f.show();
}
public String getAppletInfo() {
return "A simple drawing program.";
}
}

class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
public static final int LINES = 0;
public static final int POINTS = 1;
int mode = LINES;
Vector lines = new Vector();
Vector colors = new Vector();
int x1,y1;
int x2,y2;

public DrawPanel() {
setBackground(Color.white);
addMouseMotionListener(this);
addMouseListener(this);
}

public void setDrawMode(int mode) {
switch (mode) {
case LINES:
case POINTS:
this.mode = mode;
break;
default:
throw new IllegalArgumentException();
}
}

public void mouseDragged(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x2 = e.getX();
y2 = e.getY();
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x1 = e.getX();
y1 = e.getY();
break;
}
repaint();
}

public void mouseMoved(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
x1 = e.getX();
y1 = e.getY();
x2 = -1;
break;
case POINTS:
default:
colors.addElement(getForeground());
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
x1 = e.getX();
y1 = e.getY();
repaint();
break;
}
}

public void mouseReleased(MouseEvent e) {
e.consume();
switch (mode) {
case LINES:
colors.addElement(getForeground());
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
x2 = -1;
break;
case POINTS:
default:
break;
}
repaint();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) {
int np = lines.size();

/* draw the current lines */
g.setColor(getForeground());
for (int i=0; i < np; i++) {
Rectangle p = (Rectangle)lines.elementAt(i);
g.setColor((Color)colors.elementAt(i));
if (p.width != -1) {
g.drawLine(p.x, p.y, p.width, p.height);
} else {
g.drawLine(p.x, p.y, p.x, p.y);
}
}
if (mode == LINES) {
g.setColor(getForeground());
if (x2 != -1) {
g.drawLine(x1, y1, x2, y2);
}
}
}
}

class DrawControls extends Panel implements ItemListener {
DrawPanel target;

public DrawControls(DrawPanel target) {
this.target = target;
setLayout(new FlowLayout());
setBackground(Color.lightGray);
target.setForeground(Color.red);
CheckboxGroup group = new CheckboxGroup();
Checkbox b;
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.red);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.green);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.blue);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.pink);
add(b = new Checkbox(null, group, false));
b.addItemListener(this);
b.setForeground(Color.orange);
add(b = new Checkbox(null, group, true));
b.addItemListener(this);
b.setForeground(Color.black);
target.setForeground(b.getForeground());
Choice shapes = new Choice();
shapes.addItemListener(this);
shapes.addItem("Lines");
shapes.addItem("Points");
shapes.setBackground(Color.lightGray);
add(shapes);
}

public void paint(Graphics g) {
Rectangle r = getBounds();
g.setColor(Color.lightGray);
g.draw3DRect(0, 0, r.width, r.height, false);

int n = getComponentCount();
for(int i=0; i Component comp = getComponent(i);
if (comp instanceof Checkbox) {
Point loc = comp.getLocation();
Dimension d = comp.getSize();
g.setColor(comp.getForeground());
g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
}
}
}

public void itemStateChanged(ItemEvent e) {
if (e.getSource() instanceof Checkbox) {
target.setForeground(((Component)e.getSource()).getForeground());
} else if (e.getSource() instanceof Choice) {
String choice = (String) e.getItem();
if (choice.equals("Lines")) {
target.setDrawMode(DrawPanel.LINES);
} else if (choice.equals("Points")) {
target.setDrawMode(DrawPanel.POINTS);
}
}
}
}
这是一个applet,把它放在html里面就可以了!



回答2:

package com;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class IOTest {
// 复制图片 d盘复制到e盘
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
// 字节流
//读文件类:FileInputStream,BufferedInputStream
//写文件类:FileOutputStream,BufferedOutputSteam

//写文件
File file=new File("d:\\字节流写.txt");
String str="学校非常好!!!";
FileOutputStream out=new FileOutputStream(file);
out.write(str.getBytes());
out.flush();
out.close();
//读文件
FileInputStream in=new FileInputStream(file);
byte[] b =new byte[1024];
int len=0;
while((len=in.read(b))!=-1){
String s=new String(b,0,len);
System.out.println(s);
}
new BufferedInputStream(new FileInputStream (file));

//字符流
//读文件类:FileReader,BufferedReader
//写文件类:FileWriter,BufferedWriter
FileWriter out=new FileWriter(file,true);
out.write("中国人!!");
out.flush();
out.close();
FileReader in=new FileReader(file);
char[] b =new char[1024];
int len=0;
while((len=in.read(b))!=-1){
String s=new String(b,0,len);
System.out.println(s);
}
in.close();
BufferedReader buff=new BufferedReader(new FileReader(file));
String stri=null;
while((stri=buff.readLine())!=null){
System.out.println(stri);
}
}

}

回答3:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class Write_Read {

FileWriter filewriter;
BufferedWriter bufferedwriter;
FileReader filereader;
BufferedReader bufferedreader;

private void write() {
try {
File f = new File("C://aaa.text");
filewriter = new FileWriter(f);
filewriter.write("班级:1班" + "\r\n");
filewriter.write("姓名:王三" + "\r\n");
filewriter.write("学号:01" + "\r\n");
filewriter.write("年龄:20" + "\r\n");
filewriter.write("籍贯:四川" + "\r\n");
filewriter.flush();
System.out.println("***文件写入成功!***\n");
} catch (Exception e) {
System.out.println("写入失败!" + e);
}
}

private void read() {

String str = null;
try {
File f = new File("C://aaa.text");
filereader = new FileReader(f);
bufferedreader = new BufferedReader(filereader);
} catch (Exception e) {
System.out.println("文件夹不存在\n" + e);
}
try {
System.out.println("文件的内容为:\n");
while ((str = bufferedreader.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
System.out.println(e);
}

}

public static void main(String[] args) {
Write_Read wr = new Write_Read();
wr.write();
wr.read();

}

}