下面是我以前做的小程序,希望能给你点帮助!
package menu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DaYin extends JFrame implements ActionListener{
public DaYin() {
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
JMenuBar jmb=new JMenuBar();
JMenu jm=null;
jm=new JMenu("文件");
jmb.add(jm);
jm=new JMenu("打印");
JMenuItem jmi=new JMenuItem("打印预览");
jmi.addActionListener(this);
jm.add(jmi);
jm=new JMenu("帮助");
this.setJMenuBar(jmb);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
public static void main(String[] args) {
new DaYin();
public void update(Graphics g){
g.setColor(Color.black);
String title[]={"摘要","科目","子目或户名","借方金额","贷方金额"};
String danwei[]={"千","百","十","万","千","百","十","元","角","分"};
int x=csx;
int y=csy;
int lieju=100;//线与线之间的横向间距
int yy=hangju*row+y;
for(int i=0;i=row;i++){
if(i==0)
else
y+=hangju;
y=csy;
for(int j=0;j=cul;j++){
g.drawLine(x, y, x, row*hangju+y);//此循环用于画出表格的列
x+=lieju;
x=csx;
for(int z=0;zcul;z++){
g.setColor(Color.red);//设置颜色
g.drawString(title[z], a, b);//为表格添加表头
a+=lieju;
g.drawLine(xx, y+hangju, xx, row*hangju+y+hangju);//此循环用于画出表格的列
g.setFont(new Font("",Font.TYPE1_FONT,11));
g.setColor(Color.BLUE);//设置颜色
xx+=10;
g.setColor(Color.black);//设置颜色
for(int i=0;i=1;i++){
g.drawLine(x,yy , cul*lieju+x, yy);//此循环用于画出表格的行
yy+=hangju;
yy=hangju*row+y;
g.drawLine(x, yy, x, (row+1)*hangju+y);//此循环用于画出表格的列
if(j0){
}else{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("打印预览")){
this.repaint();
使用drawLine()做了一个记账凭证.
希望这个小程序能够给你点灵感
import java.awt.geom.*;
//不规则图形的绘制
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); //允许事件
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
public void paint(Graphics g) { //重载窗口组件的paint()方法
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) {
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
import java.applet.*;
import java.util.*;
import java.io.*;
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 toolFlag = 0;//toolFlag:工具标记
//toolFlag工具对应表:
Color c = new Color(0,0,0); //画笔颜色
BasicStroke size = new BasicStroke(con,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);//画笔粗细
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.addItemListener(this);
//橡皮大小选择
EraserChoice = new Choice();
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);
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()
{ savePicture.setVisible(false); }
addWindowListener(new WindowAdapter()
{ System.exit(0);}
public void paint(Graphics g)
n = paintInfo.size();
g.clearRect(0,0,getSize().width,getSize().height);//清除
for(int i=0; in ;i++){
p1 = (Point)paintInfo.elementAt(i);
size = new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
switch(p1.tool)
case 0://画笔
break;
case 1://橡皮
g.clearRect(p1.x, p1.y, p1.boarder, p1.boarder);
i=i+1;
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")
else if(name=="green")
else if(name=="blue")
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(e.getSource()==EraserChoice)//橡皮大小
String Esize = EraserChoice.getSelectedItem();
public void mouseDragged(MouseEvent e)
Point p1 ;
switch(toolFlag){
x = (int)e.getX();
y = (int)e.getY();
p1 = new Point(x, y, c, toolFlag, con);
paintInfo.addElement(p1);
repaint();
p1 = new Point(x, y, null, toolFlag, Econ);
public void mouseMoved(MouseEvent e) {}
public void update(Graphics g)
paint(g);
public void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
paintInfo.addElement(cutflag);
case 1: //eraser
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)//清除
paintInfo.removeAllElements();
if(e.getSource()==drLine)//画线
if(e.getSource()==drCircle)//画圆
if(e.getSource()==drRect)//画矩形
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;
try{
File filein = new File(openPicture.getDirectory(),openPicture.getFile());
picIn = new FileInputStream(filein);
VIn = new ObjectInputStream(picIn);
paintInfo = (Vector)VIn.readObject();
VIn.close();
toolFlag = tempflag;
System.out.println("can not read object");
catch(IOException IOe)
System.out.println("can not read file");
if(e.getSource()==savePic)//保存图画
savePicture.setVisible(true);
File fileout = new File(savePicture.getDirectory(),savePicture.getFile());
picOut = new FileOutputStream(fileout);
VOut = new ObjectOutputStream(picOut);
VOut.writeObject(paintInfo);
VOut.close();
System.out.println("can not write object");
}//end paintboard
public class pb
public static void main(String args[])
{ new paintboard("画图程序"); }
public class TankWar extends JFrame {
public TankWar (String title) {
super(title);
this.setLocationRelativeTo(null);//设置窗口的位置屏幕中间
this.setResizable(false);//设置为不可改变大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭事件
MyJPanel myJPanel = new MyJPanel();//创建一个MyJPanel实例对象myJPanel
myJPanel.setBackground(Color.blue);//设置myJPanel的背景颜色
this.add(myJPanel);
/*内部继承JPanel类*/
class MyJPanel extends JPanel {
protected void paintComponent(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.setColor(c);
public static void main(String args[]){
new TankWar ("坦克大战");
找到了,很久以前写的一个简单画图,呵呵~当时要求用AWT写,很难受.
package net.miqiang.gui;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
/**
* 简单画图板程序
* 好久没用 AWT 了,写起来真别扭,如果用 swing 会很舒服,有空再改写吧.
*
* @author 米强
*/
public class TestMain extends Frame {
// 画板
private Palette palette = null;
// 显示当前颜色的面板
private Panel nonceColor = null;
// 画笔粗细
private Label drawWidth = null;
// 画笔端点的装饰
private Label drawCap = null;
// 选取颜色按钮的监听事件类
private ButtonColorAction buttonColorAction = null;
// 鼠标进入按钮后光标样式的监听事件类
private ButtonCursor buttonCursor = null;
// 画笔样式的监听事件
private ButtonStrokeAction buttonStrokeAction = null;
* 构造方法
public TestMain() {
// 设置标题栏文字
super("简易画图板");
// 构造一个画图板
palette = new Palette();
// 画笔颜色选择器
Color [] color = {Color.black, Color.blue, Color.cyan, Color.darkGray, Color.gray, Color.green, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow};
nonceColor = new Panel();
nonceColor.setBackground(Color.black);
paneColor.add(nonceColor);
buttonColorAction = new ButtonColorAction();
buttonCursor = new ButtonCursor();
for(int i = 0; i buttonColor.length; i++){
buttonColor[i] = new Button();
buttonColor[i].setBackground(color[i]);
buttonColor[i].addActionListener(buttonColorAction);
buttonColor[i].addMouseListener(buttonCursor);
paneColor.add(buttonColor[i]);
pane.add(paneColor);
// 控制画笔样式
buttonStrokeAction = new ButtonStrokeAction();
Button [] buttonStroke = new Button[11];
buttonStroke[0] = new Button("1");
buttonStroke[10] = new Button("●");
drawCap = new Label("●", Label.CENTER);
drawWidth.setBackground(Color.lightGray);
drawCap.setBackground(Color.lightGray);
paneStroke.add(drawWidth);
for(int i = 0; i buttonStroke.length; i++){
paneStroke.add(buttonStroke[i]);
buttonStroke[i].addMouseListener(buttonCursor);
buttonStroke[i].addActionListener(buttonStrokeAction);
buttonStroke[i].setName("width");
buttonStroke[i].setName("cap");
paneStroke.add(drawCap);
pane.add(paneStroke);
// 将画笔颜色选择器添加到窗体中
this.add(pane, BorderLayout.NORTH);
// 将画图板添加到窗体中
this.add(palette);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
// 设置窗体 ICON 图标
this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/palette.png"));
// 设置窗口的大小
// 设置窗口位置,处于屏幕正中央
this.setLocationRelativeTo(null);
// 显示窗口
* 程序入口
* @param args
* 字符串数组参数
new TestMain();
* 选取颜色按钮的监听事件类
class ButtonColorAction implements ActionListener {
Color color_temp = ((Button)e.getSource()).getBackground();
nonceColor.setBackground(color_temp);
palette.setColor(color_temp);
* 鼠标进入按钮变换光标样式监听事件类
class ButtonCursor extends MouseAdapter {
public void mouseEntered(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.HAND_CURSOR));
public void mouseExited(MouseEvent e) {
((Button)e.getSource()).setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
* 设置画笔的监听事件类
class ButtonStrokeAction implements ActionListener {
Button button_temp = (Button) e.getSource();
String name = button_temp.getName();
if(name.equalsIgnoreCase("width")){
drawWidth.setText(button_temp.getLabel());
palette.setStroke(Float.parseFloat(button_temp.getLabel()));
}else if(name.equalsIgnoreCase("cap")){
drawCap.setText(button_temp.getLabel());
if(button_temp.getLabel().equals("■")){
palette.setStroke(BasicStroke.CAP_SQUARE);
}else if(button_temp.getLabel().equals("●")){
palette.setStroke(BasicStroke.CAP_ROUND);
* 画板类
class Palette extends Panel implements MouseListener, MouseMotionListener {
// 鼠标 X 坐标的位置
private int mouseX = 0;
// 上一次 X 坐标位置
private int oldMouseX = 0;
// 鼠标 Y 坐标的位置
private int mouseY = 0;
// 上一次 Y 坐标位置
private int oldMouseY = 0;
// 画图颜色
private Color color = null;
// 画笔样式
private BasicStroke stroke = null;
// 缓存图形
private BufferedImage image = null;
* 构造一个画板类
public Palette() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
// 默认黑色画笔
color = new Color(0, 0, 0);
// 设置默认画笔样式
// 设置颜色
image.getGraphics().setColor(Color.white);
// 画背景
* 重写 paint 绘图方法
public void paint(Graphics g) {
super.paint(g);
// 图形抗锯齿
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 设置绘图颜色
bg.setColor(color);
// 设置画笔样式
bg.setStroke(stroke);
// 画线,从上一个点到新的点
bg.drawLine(oldMouseX, oldMouseY, mouseX, mouseY);
// 将缓存中的图形画到画板上
* 重写 update 方法
public void update(Graphics g) {
this.paint(g);
* @return stroke
public BasicStroke getStroke() {
return stroke;
* @param stroke 要设置的 stroke
public void setStroke(BasicStroke stroke) {
this.stroke = stroke;
* 设置画笔粗细
* @param width
public void setStroke(float width) {
this.stroke = new BasicStroke(width, stroke.getEndCap(), stroke.getLineJoin());
* 设置画笔端点的装饰
* @param cap 参考 java.awt.BasicStroke 类静态字段
public void setStroke(int cap) {
this.stroke = new BasicStroke(stroke.getLineWidth(), cap, stroke.getLineJoin());
* @return color
public Color getColor() {
return color;
* @param color 要设置的 color
public void setColor(Color color) {
this.color = color;
public void mouseClicked(MouseEvent mouseEvent) {
* 鼠标按下
public void mousePressed(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX = mouseEvent.getX();
this.oldMouseY = this.mouseY = mouseEvent.getY();
public void mouseReleased(MouseEvent mouseEvent) {
* 鼠标进入棋盘
public void mouseEntered(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
* 鼠标退出棋盘
public void mouseExited(MouseEvent mouseEvent) {
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
* 鼠标拖动
public void mouseDragged(MouseEvent mouseEvent) {
this.oldMouseX = this.mouseX;
this.oldMouseY = this.mouseY;
this.mouseX = mouseEvent.getX();
this.mouseY = mouseEvent.getY();
public void mouseMoved(MouseEvent mouseEvent) {
以上就是土嘎嘎小编为大家整理的画图的java代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!