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

Python014

如何用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()//重绘组件

}

}

}

java编写图形界面需要用到swing等组件,可以在eclipse中安装windowbuilder来开发窗体,自动生成窗体代码,然后自己再根据需要修改,如:

package mainFrame

import java.awt.EventQueue

import java.awt.event.MouseAdapter

import java.awt.event.MouseEvent

import javax.swing.ImageIcon

import javax.swing.JButton

import javax.swing.JCheckBox

import javax.swing.JFrame。

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

我真的是抽风了,手痒了,给你写了这段代码,如果楼主、、、

嘻嘻,追点分给我吧

import java.awt.*

import java.awt.event.*

import java.io.*

import javax.swing.*

public class baidu_9 extends JFrame implements ActionListener

{

static final String OUTPUT="C://Test.txt"

JPanel pnl

JLabel lbl

JTextField txt1,txt2

JButton btnCopy,btnClear,btnOutput,btnColor

public baidu_9()

{

super("百度题目")

pnl=new JPanel()

this.setContentPane(pnl)

pnl.setLayout(null)

pnl.setBackground(Color.WHITE)

lbl=new JLabel("百度")

txt1=new JTextField(10)

txt2=new JTextField(10)

btnCopy=new JButton("复制")

btnCopy.addActionListener(this)

btnClear=new JButton("清除")

btnClear.addActionListener(this)

btnOutput=new JButton("写入")

btnOutput.addActionListener(this)

btnColor=new JButton("变色")

btnColor.addActionListener(this)

lbl.setBounds(100, 10, 80, 20)

txt1.setBounds(10, 40, 100, 20)

txt2.setBounds(120, 40, 100, 20)

btnCopy.setBounds(10, 70, 60, 20)

btnClear.setBounds(75, 70, 60, 20)

btnOutput.setBounds(140, 70, 60, 20)

btnColor.setBounds(205, 70, 60, 20)

pnl.add(lbl)

pnl.add(txt1)

pnl.add(txt2)

pnl.add(btnCopy)

pnl.add(btnClear)

pnl.add(btnOutput)

pnl.add(btnColor)

setSize(300,150)

setVisible(true)

}

public void Copy()

{

txt2.setText(txt1.getText())

}

public void Clear()

{

txt1.setText("")

txt2.setText("")

pnl.setBackground(Color.WHITE)

}

public void Color()

{

pnl.setBackground(Color.BLACK)

}

public void Ouput()

{

try

{

File fl=new File("C:\\Test.txt")

FileWriter fw = new FileWriter(fl)

BufferedWriter bw = new BufferedWriter(fw)

bw.write(txt1.getText())

bw.close()

}

catch (IOException e)

{

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==btnCopy)

this.Copy()

if(e.getSource()==btnClear)

this.Clear()

if(e.getSource()==btnColor)

this.Color()

if(e.getSource()==btnOutput)

this.Ouput()

}

public static void main(String[] args)

{

new baidu_9()

}

}