如何用Java编写一个绘制图形的小程序?

如何用Java编写一个绘制图形的小程序?,第1张

import java.awt.*

import java.awt.event.*

import java.awt.geom.*

import javax.swing.*

//不规则图形的绘制

public class IrregularShapeDemo extends JFrame {

GeneralPath gPath= new GeneralPath()//GeneralPath对象实例

Point aPoint

//构造函数

public IrregularShapeDemo() {

super("不规则图形的绘制")//调用父类构造函数

enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK)//允许事件

setSize(300, 200)//设置窗口尺寸

setVisible(true)//设置窗口可视

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//关闭窗口时退出程序

}

public void paint(Graphics g) { //重载窗口组件的paint()方法

Graphics2D g2D = (Graphics2D)g//获取图形环境

g2D.draw(gPath)//绘制路径

}

public static void main(String[] args) {

new IrregularShapeDemo()

}

protected void processMouseEvent(MouseEvent e) { //鼠标事件处理

if(e.getID() == MouseEvent.MOUSE_PRESSED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath = new GeneralPath()//重新实例化GeneralPath对象

gPath.moveTo(aPoint.x,aPoint.y)//设置路径点

}

}

protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理

if(e.getID() == MouseEvent.MOUSE_DRAGGED) {

aPoint = e.getPoint()//得到当前鼠标点

gPath.lineTo(aPoint.x, aPoint.y)//设置路径

gPath.moveTo(aPoint.x, aPoint.y)

repaint()//重绘组件

}

}

}

public test_03(){

init()

}

public void init(){

JLabel jl_1=new JLabel("用户名")

JLabel jl_2=new JLabel("密码")

JTextField jtf=new JTextField()

JPasswordField jpf=new JPasswordField()

JButton button_1=new JButton("确定")

JPanel panel=new JPanel()

panel.setLayout(null)

jl_1.setBounds(30, 50, 50, 30)

jl_2.setBounds(30,90,50,30)

jtf.setBounds(100, 50, 100, 30)

jpf.setBounds(100, 90, 100, 30)

button_1.setBounds(110, 130, 80, 30)

panel.add(jl_1)

panel.add(jl_2)

panel.add(jtf)

panel.add(jpf)

panel.add(button_1)

this.add(panel)

this.setSize(300, 250)

this.setLocation(400, 300)

this.setVisible(true)

}

public static void main(String[] args) {

new test_03()

}

package testWrite

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.InputStream

public class write {

InputStream inStream 

FileOutputStream fs 

int bytesum = 0

int byteread = 0

String  data 

StringBuffer dd

public static   byte[] readdata(String filePath) {

   byte[] data = new byte[2048]// 用于存储读取的文件内容

   try {

   File file = new File(filePath)

   if (file.exists()) {

   FileInputStream fis = new FileInputStream(file)

   fis.read(data)

   fis.close()

   }else{       

   System.out.println("文件不存在")

   }

} catch ( Exception e) {  

}

return data

}

public void  writeFile(String fileCopyPath,byte [] data){

   File file = new File(fileCopyPath)

try {

   if (!file.exists()) {

file.createNewFile() //创建文件

FileOutputStream fos = new FileOutputStream(file)

fos.write(data)

fos.close()

}else{          

   System.out.println("文件已经存在")

   }

} catch (Exception e) {

e.printStackTrace()

}

   }

}

package testWrite

public class writeOne  extends write {

public static void main(String[] args) {

//第一种

// writeOne  a=new writeOne ()

// byte [] data=a.readdata("D:\\Users\\workspace\\testWrite\\src\\testWrite\\write.java")

// a.writeFile("D:\\Users\\workspace\\testWrite\\src\\testWrite\\writenew.java",data)

//第二种

writeOne  a2=new writeOne ()

System.out.println(System.getProperty("user.dir"))//user.dir指定了当前的路径

String path=System.getProperty("user.dir")

System.out.println(path)

    String writefile="\\src\\testWrite\\write.java"

    String writenewfile="\\src\\testWrite\\writenew.java"

    String w1=path+writefile   //读取文件的路径

   String w2=path+writenewfile  //写入文件路径

   byte [] data=a2.readdata(w1)  //读取文件

a2.writeFile(w2 ,data) //写入文件

}

}

本地已经调试通过可以复制write.java 文件 ,注意包,和文件的路径 直接运行第二个java文件就可以了,运行后刷新一下就可以看到复制的文件(备注:我是使用myeclipse环境进行调试)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7749524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存