500行java编程代码的简单介绍

需要一份500行的java程序 , 期末大作业,最好带详细注释 。Java生成CSV文件简单操作实例
CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件 。在 CSV文件中 , 数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏 。如:
1231,张三,男2,李四,男3,小红,女
Java生成CSV文件(创建与导出封装类)
package com.yph.omp.common.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
/**
* Java生成CSV文件
*/
public class CSVUtil {
/**
* 生成为CVS文件
*
* @param exportData
*源数据List
* @param map
*csv文件的列表头map
* @param outPutPath
*文件路径
* @param fileName
*文件名称
* @return
*/
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map,
String outPutPath, String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdir();
}
// 定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".csv",
new File(outPutPath));
// UTF-8使正确读取分隔符","
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(csvFile), "GBK"), 1024);
// 写入文件头部
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
csvFileOutputStream
.write("\""(String) propertyEntry.getValue() != null ? (String) propertyEntry
.getValue() : """\"");
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
csvFileOutputStream.newLine();
// 写入文件内容
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
/*-------------------------------*/
//以下部分根据不同业务做出相应的更改
StringBuilder sbContext = new StringBuilder("");
if (null != BeanUtils.getProperty(row,(String) propertyEntry.getKey())) {
if("证件号码".equals(propertyEntry.getValue())){
//避免:身份证号码 ,读取时变换为科学记数 - 解决办法:加 \t(用Excel打开时,证件号码超过15位后会自动默认科学记数)
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey())"\t");
}else{
sbContext.append(BeanUtils.getProperty(row,(String) propertyEntry.getKey()));
}
}
csvFileOutputStream.write(sbContext.toString());
/*-------------------------------*/
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
}
}
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
}
}
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 下载文件
*
* @param response
* @param csvFilePath
*文件路径
* @param fileName
*文件名称
* @throws IOException
*/
public static void exportFile(HttpServletRequest request,
HttpServletResponse response, String csvFilePath, String fileName)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/csv;charset=GBK");
response.setHeader("Content-Disposition", "attachment; filename="
new String(fileName.getBytes("GB2312"), "ISO8859-1"));
InputStream in = null;
try {
in = new FileInputStream(csvFilePath);
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
while ((len = in.read(buffer))0) {
out.write(buffer, 0, len);
}
} catch (FileNotFoundException e1) {
System.out.println(e1);
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e1) {
throw new RuntimeException(e1);
}
}
}
}
/**
* 删除该目录filePath下的所有文件
*
* @param filePath
*文件目录路径
*/
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; i) {
if (files[i].isFile()) {
files[i].delete();
}
}
}
}
/**
* 删除单个文件
*
* @param filePath
*文件目录路径
* @param fileName
*文件名称
*/
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; ifiles.length; i) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
}
}
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void createFileTest() {
List exportData = https://www.04ip.com/post/new ArrayListMap();
Map row1 = new LinkedHashMapString, String();
row1.put("1", "11");
row1.put("2", "12");
row1.put("3", "13");
row1.put("4", "14");
exportData.add(row1);
row1 = new LinkedHashMapString, String();
row1.put("1", "21");
row1.put("2", "22");
row1.put("3", "23");
row1.put("4", "24");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "第一列");
map.put("2", "第二列");
map.put("3", "第三列");
map.put("4", "第四列");
String path = "d:/export";
String fileName = "文件导出";
File file = CSVUtil.createCSVFile(exportData, map, path, fileName);
String fileNameNew = file.getName();
String pathNew = file.getPath();
System.out.println("文件名称:"fileNameNew );
System.out.println("文件路径:"pathNew );
}
}
//注:BeanUtils.getProperty(row,(String) propertyEntry.getKey())"\t",只为解决数字格式超过15位后,在Excel中打开展示科学记数问题 。
需要一个java程序,最好是万年历的,要在500行左右的Java-时钟万年历
clock.java代码如下:
/**
* Clock.java
* Summary 数字时间显示
* Created on 2005-8-14
* @author 高?
* remark 如有改动请发一份代码给我,邮箱gkgklovelove@eyou.com
*/
package Clock;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
class Clock extends Canvas implements Runnable{
MainFrame mf;
Thread t;
String time;
Clock(MainFrame mf){
this.mf=mf;
setSize(400,40);
setBackground(Color.white);
t=new Thread(this);//实例化线程
t.start();//调用线程
}
public void run(){
while(true){
try{
t.sleep(1000);//休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime());//得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,100,25);
}
MainFrame.java 的代码如下:
/**
* MainFrame.java
* Summary 万年历主类
* Created on 2005-8-14
* @author 高?
* remark 如有改动请发一份代码给我 , 邮箱gkgklovelove@eyou.com
*/
package Clock;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
class MainFrame extends JFrame{
JPanel panel=new JPanel(new BorderLayout());
JPanel panel1=new JPanel();
JPanel panel2=new JPanel(new GridLayout(7,7));
JPanel panel3=new JPanel();
JLabel []label=new JLabel[49];
JLabel y_label=new JLabel("年份");
JLabel m_label=new JLabel("月份");
JComboBox com1=new JComboBox();
JComboBox com2=new JComboBox();
JButton button=new JButton("查看");
int re_year,re_month;
int x_size,y_size;
String year_num;
Calendar now=Calendar.getInstance();//实例化Calendar
MainFrame(){
super("万年历");
setSize(300,350);
x_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size=(int)(Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size-300)/2,(y_size-350)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
panel1.add(button);
for(int i=0;i49;i){
label[i]=new JLabel("",JLabel.CENTER);//将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1,BorderLayout.NORTH);
panel.add(panel2,BorderLayout.CENTER);
panel.add(panel3,BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int c_year,c_month,c_week;
c_year=Integer.parseInt(com1.getSelectedItem().toString());//得到当前所选年份
c_month=Integer.parseInt(com2.getSelectedItem().toString())-1;//得到当前月份,并减1,计算机中的月为0-11
c_week=use(c_year,c_month);//调用函数use,得到星期几
Resetday(c_week,c_year,c_month);//调用函数Resetday
}});
setContentPane(panel);
setVisible(true);
setResizable(false);
}
public void Init(){
int year,month_num,first_day_num;
String log[]={"日","一","二","三","四","五","六"};
for(int i=0;i7;i){
label[i].setText(log[i]);
}
for(int i=0;i49;i=i 7){
label[i].setForeground(Color.red);//将星期日的日期设置为红色
}
for(int i=6;i49;i=i 7){
label[i].setForeground(Color.green);//将星期六的日期设置为绿色
}
for(int i=1;i10000;i){
com1.addItem("" i);
}
for(int i=1;i13;i){
com2.addItem("" i);
}
month_num=(int)(now.get(Calendar.MONTH));//得到当前时间的月份
year=(int)(now.get(Calendar.YEAR));//得到当前时间的年份
com1.setSelectedIndex(year-1);//设置下拉列表显示为当前年
com2.setSelectedIndex(month_num);//设置下拉列表显示为当前月
first_day_num=use(year,month_num);
Resetday(first_day_num,year,month_num);
}
public int use(int reyear,int remonth){
int week_num;
now.set(reyear,remonth,1);//设置时间为所要查询的年月的第一天
week_num= (int)(now.get(Calendar.DAY_OF_WEEK));//得到第一天的星期
return week_num;
}
public void Resetday(int week_log,int year_log,int month_log){
int month_score_log;//判断是否是闰年的标记
int month_day_score;//存储月份的天数
int count;
month_score_log=0;
month_day_score=0;
count=1;
if(year_log%4==0year_log0!=0||year_log@0==0){//判断是否为闰年
month_score_log=1;
}
month_log=month_log 1;//将传来的月份数加1
switch(month_log){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
month_day_score=31;
break;
case 4:
case 6:
case 9:
case 11:
month_day_score=30;
break;
case 2:
if(month_score_log==1){
month_day_score=29;
}
else{
month_day_score=28;
}
break;
}
for(int i=7;i49;i){//初始化标签
label[i].setText("");
}
week_log=week_log 6;//将星期数加6 , 使显示正确
month_day_score=month_day_score week_log;
for(int i=week_log;imonth_day_score;i,count){
label[i].setText(count "");
}
}
public static void main(String [] args){
JFrame.setDefaultLookAndFeelDecorated(true);
MainFrame start=new MainFrame();
}
}
求java小程序代码,500行左右 。。大作业用 。追加50import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class mypanel extends Panel implements MouseListener
{
int chess[][] = new int[11][11];
boolean Is_Black_True;
mypanel()
{
Is_Black_True = true;
for(int i = 0;i11;i)
{
for(int j = 0;j11;j)
{
chess[i][j] = 0;
}
}
addMouseListener(this);
setBackground(Color.BLUE);
setBounds(0, 0, 360, 360);
setVisible(true);
}
public void mousePressed(MouseEvent e)
{
int x = e.getX();
int y = e.getY();
if(x25 || x33025 ||y25 || y330 25)
{
return;
}
if(chess[x/30-1][y/30-1] != 0)
{
return;
}
if(Is_Black_True == true)
{
chess[x/30-1][y/30-1] = 1;
Is_Black_True = false;
repaint();
Justisewiner();
return;
}
if(Is_Black_True == false)
{
chess[x/30-1][y/30-1] = 2;
Is_Black_True = true;
repaint();
Justisewiner();
return;
}
}
void Drawline(Graphics g)
{
for(int i = 30;i = 330;i= 30)
{
for(int j = 30;j = 330; j = 30)
{
g.setColor(Color.WHITE);
g.drawLine(i, j, i, 330);
}
}
for(int j = 30;j = 330;j= 30)
{
g.setColor(Color.WHITE);
g.drawLine(30, j, 330, j);
}
}
void Drawchess(Graphics g)
{
for(int i = 0;i11;i)
{
for(int j = 0;j11;j)
{
if(chess[i][j] == 1)
{
g.setColor(Color.BLACK);
g.fillOval((i1) * 30 - 8, (j1) * 30 - 8, 16, 16);
}
if(chess[i][j] == 2)
{
g.setColor(Color.WHITE);
g.fillOval((i1) * 30 - 8, (j1) * 30 - 8, 16, 16);
}
}
}
}
void Justisewiner()
{
int black_count = 0;
int white_count = 0;
int i = 0;
for(i = 0;i11;i)//横向判断
{
for(int j = 0;j11;j)
{
if(chess[i][j] == 1)
{
black_count;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i][j] == 2)
{
white_count;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i11;i)//竖向判断
{
for(int j = 0;j11;j)
{
if(chess[j][i] == 1)
{
black_count;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[j][i] == 2)
{
white_count;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
【500行java编程代码的简单介绍】return;
}
}
else
{
white_count = 0;
}
}
}
for(i = 0;i7;i)//左向右斜判断
{
for(int j = 0;j7;j)
{
for(int k = 0;k5;k)
{
if(chess[ik][jk] == 1)
{
black_count;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[ik][jk] == 2)
{
white_count;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
for(i = 4;i11;i)//右向左斜判断
{
for(int j = 6;j = 0;j--)
{
for(int k = 0;k5;k)
{
if(chess[i - k][jk] == 1)
{
black_count;
if(black_count == 5)
{
JOptionPane.showMessageDialog(this, "黑棋胜利");
Clear_Chess();
return;
}
}
else
{
black_count = 0;
}
if(chess[i - k][jk] == 2)
{
white_count;
if(white_count == 5)
{
JOptionPane.showMessageDialog(this, "白棋胜利");
Clear_Chess();
return;
}
}
else
{
white_count = 0;
}
}
}
}
}
void Clear_Chess()
{
for(int i=0;i11;i)
{
for(int j=0;j11;j)
{
chess[i][j]=0;
}
}
repaint();
}
public void paint(Graphics g)
{
Drawline(g);
Drawchess(g);
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
}
class myframe extends Frame implements WindowListener
{
mypanel panel;
myframe()
{
setLayout(null);
panel = new mypanel();
add(panel);
panel.setBounds(0,23, 360, 360);
setTitle("单人版五子棋");
setBounds(200, 200, 360, 383);
setVisible(true);
addWindowListener(this);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
}
public class mywindow
{
public static void main(String argc [])
{
myframe f = new myframe();
}
}
需要一个java小程序要在500行左右的(30分 20分)给你画图小游戏吧;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class Point implements Serializable
{
int x,y;
Color col;
int tool;
int boarder;
Point(int x, int y, Color col, int tool, int boarder)
{
this.x = x;
this.y = y;
this.col = col;
this.tool = tool;
this.boarder = boarder;
}
}
class paintboard extends Frame implements ActionListener,MouseMotionListener,MouseListener,ItemListener
{
int x = -1, y = -1;
int con = 1;//画笔大小
int Econ = 5;//橡皮大小
int toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
//(0--画笔);(1--橡皮);(2--清除);
//(3--直线);(4--圆);(5--矩形);
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
Point cutflag = new Point(-1, -1, c, 6, con);//截断标志
Vector paintInfo = null;//点信息向量组
int n = 1;
FileInputStream picIn = null;
FileOutputStream picOut = null;
ObjectInputStream VIn = null;
ObjectOutputStream VOut = null;
// *工具面板--画笔 , 直线 , 圆,矩形,多边形,橡皮,清除*/
Panel toolPanel;
Button eraser, drLine,drCircle,drRect;
Button clear ,pen;
Choice ColChoice,SizeChoice,EraserChoice;
Button colchooser;
Label 颜色,大小B,大小E;
//保存功能
Button openPic,savePic;
FileDialog openPicture,savePicture;
paintboard(String s)
{
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintInfo = new Vector();
/*各工具按钮及选择项*/
//颜色选择
ColChoice = new Choice();
ColChoice.add("black");
ColChoice.add("red");
ColChoice.add("blue");
ColChoice.add("green");
ColChoice.addItemListener(this);
//画笔大小选择
SizeChoice = new Choice();
SizeChoice.add("1");
SizeChoice.add("3");
SizeChoice.add("5");
SizeChoice.add("7");
SizeChoice.add("9");
SizeChoice.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
EraserChoice.add("5");
EraserChoice.add("9");
EraserChoice.add("13");
EraserChoice.add("17");
EraserChoice.addItemListener(this);
////////////////////////////////////////////////////
toolPanel = new Panel();
clear = new Button("清除");
eraser = new Button("橡皮");
pen = new Button("画笔");
drLine = new Button("画直线");
drCircle = new Button("画圆形");
drRect = new Button("画矩形");
openPic = new Button("打开图画");
savePic = new Button("保存图画");
colchooser = new Button("显示调色板");
//各组件事件监听
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
openPic.addActionListener(this);
savePic.addActionListener(this);
colchooser.addActionListener(this);
颜色 = new Label("画笔颜色",Label.CENTER);
大小B = new Label("画笔大小",Label.CENTER);
大小E = new Label("橡皮大小",Label.CENTER);
//面板添加组件
toolPanel.add(openPic);
toolPanel.add(savePic);
toolPanel.add(pen);
toolPanel.add(drLine);
toolPanel.add(drCircle);
toolPanel.add(drRect);
toolPanel.add(颜色); toolPanel.add(ColChoice);
toolPanel.add(大小B); toolPanel.add(SizeChoice);
toolPanel.add(colchooser);
toolPanel.add(eraser);
toolPanel.add(大小E); toolPanel.add(EraserChoice);
toolPanel.add(clear);
//工具面板到APPLET面板
add(toolPanel,BorderLayout.NORTH);
setBounds(60,60,900,600); setVisible(true);
validate();
//dialog for save and load
openPicture = new FileDialog(this,"打开图画",FileDialog.LOAD);
openPicture.setVisible(false);
savePicture = new FileDialog(this,"保存图画",FileDialog.SAVE);
savePicture.setVisible(false);
openPicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ openPicture.setVisible(false); }
});
savePicture.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ savePicture.setVisible(false); }
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(0);}
});
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
Point p1,p2;
n = paintInfo.size();
if(toolFlag==2)
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; in-1; i)
{
p1 = (Point)paintInfo.elementAt(i);
p2 = (Point)paintInfo.elementAt(i 1);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setColor(p1.col);
g2d.setStroke(size);
if(p1.tool==p2.tool)
{
switch(p1.tool)
{
case 0://画笔
Line2D line1 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line1);
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
break;
case 3://画直线
Line2D line2 = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
g2d.draw(line2);
break;
case 4://画圆
Ellipse2D ellipse = new Ellipse2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(ellipse);
break;
case 5://画矩形
Rectangle2D rect = new Rectangle2D.Double(p1.x, p1.y, Math.abs(p2.x-p1.x) , Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
case 6://截断 , 跳过
i=i 1;
break;
default :
}//end switch
}//end if
}//end for
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==ColChoice)//预选颜色
{
String name = ColChoice.getSelectedItem();
if(name=="black")
{ c = new Color(0,0,0); }
else if(name=="red")
{ c = new Color(255,0,0); }
else if(name=="green")
{ c = new Color(0,255,0); }
else if(name=="blue")
{ c = new Color(0,0,255); }
}
else if(e.getSource()==SizeChoice)//画笔大小
{
String selected = SizeChoice.getSelectedItem();
if(selected=="1")
{
con = 1;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="3")
{
con = 3;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="5")
{ con = 5;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="7")
{ con = 7;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
else if(selected=="9")
{ con = 9;
size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
}
}
else if(e.getSource()==EraserChoice)//橡皮大小
{
String Esize = EraserChoice.getSelectedItem();
if(Esize=="5")
{Econ = 5*2; }
else if(Esize=="9")
{Econ = 9*2; }
else if(Esize=="13")
{Econ = 13*2; }
else if(Esize=="17")
{Econ = 17*3; }
}
}
public void mouseDragged(MouseEvent e)
{
Point p1 ;
switch(toolFlag){
case 0://画笔
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
break;
case 1://橡皮
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, null, toolFlag, Econ);
paintInfo.addElement(p1);
repaint();
break;
default :
}
}
public void mouseMoved(MouseEvent e) {}
public void update(Graphics g)
{
paint(g);
}
public void mousePressed(MouseEvent e)
{
Point p2;
switch(toolFlag){
case 3://直线
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p2 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p2);
break;
default :
}
}
public void mouseReleased(MouseEvent e)
{
Point p3;
switch(toolFlag){
case 0: //画笔
paintInfo.addElement(cutflag);
break;
case 1: //eraser
paintInfo.addElement(cutflag);
break;
case 3: //直线
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 4: //圆
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
case 5: //矩形
x = (int)e.getX();
y = (int)e.getY();
p3 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p3);
paintInfo.addElement(cutflag);
repaint();
break;
default:
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==pen)//画笔
{ toolFlag = 0; }
if(e.getSource()==eraser)//橡皮
{ toolFlag = 1; }
if(e.getSource()==clear)//清除
{
toolFlag = 2;
paintInfo.removeAllElements();
repaint();
}
if(e.getSource()==drLine)//画线
{ toolFlag = 3; }
if(e.getSource()==drCircle)//画圆
{ toolFlag = 4; }
if(e.getSource()==drRect)//画矩形
{ toolFlag = 5; }
if(e.getSource()==colchooser)//调色板
{
Color newColor = JColorChooser.showDialog(this,"调色板",c);
c = newColor;
}
if(e.getSource()==openPic)//打开图画
{
openPicture.setVisible(true);
if(openPicture.getFile()!=null)
{
int tempflag;
tempflag = toolFlag;
toolFlag = 2 ;
repaint();
try{
paintInfo.removeAllElements();
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
repaint();
toolFlag = tempflag;
}
catch(ClassNotFoundException IOe2)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read object");
}
catch(IOException IOe)
{
repaint();
toolFlag = tempflag;
System.out.println("can not read file");
}
}
}
if(e.getSource()==savePic)//保存图画
{
savePicture.setVisible(true);
try{
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
}
catch(IOException IOe)
{
System.out.println("can not write object");
}
}}}
class pb
{
public static void main(String args[])
{
new paintboard("DRAW PICTURES SOFTWARES");
}
}
代码不难理解,好好看看就变成你自己的了
用java 编写500行的程序代码,还可以用6种方法解决的题目有哪些?并附上程序,谢谢 。。。。package test;
public class Complex {
private int realpart;//复数的实数部分
private int imaginprt;//复数的虚数部分
//构造函数,将复数的实部和虚部都置0;
public Complex(){
realpart=0;
imaginprt=0;
}
//构造函数,形参real为实部的初值 , imagin为虚部的初值 。
public Complex(int real,int imagin){
realpart=real;
imaginprt=imagin;
}
//将当前复数对象与形参复数对象相加
public void add(Complex c){
realpart =c.getRealpart();
imaginprt =c.getImaginprt();
}
//把当前复数对象的实部、虚部组合成a bi的字符串形式
public String toString(){
return realpart " " imaginprt "i";
}
//这个为测试函数
public static void main(String[] args){
Complex c=new Complex(1,2);
Complex d=new Complex(2,7);
c.add(c);//此句实现了c对象自身相加
System.out.println(c.toString());//输出结果
c.add(d);//此句实现了将d对象与c对象相加
System.out.println(c.toString());//输出结果
}
//下面四个函数为类的属性的setter方法和getter方法.
public int getImaginprt() {
return imaginprt;
}
public void setImaginprt(int imaginprt) {
this.imaginprt = imaginprt;
}
public int getRealpart() {
return realpart;
}
public void setRealpart(int realpart) {
this.realpart = realpart;
}
}
跪求500行JAVA代码 能运行就可以public class test {
public static void main(String[] args) {
System.out.println("666");
…System.out.println("666");复制494遍…
System.out.println("666");
}
}
正好500行
关于500行java编程代码和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读