导航:首页 > 编程语言 > java画图代码

java画图代码

发布时间:2022-04-26 20:51:02

❶ 求以java程序代码 题目:画图程序

面板的

PainterPanel.java

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.event.*;

{

intshape=-1;//图案类型

Point[]point=newPoint[2];//记录鼠标拖动的起始点和终点

publicPainterPanel(){

super(); //调用父类构造函数

this.setBackground(Color.white);//设置背景颜色

point[0]=newPoint(-1,-1);//初始化变量

point[1]=newPoint(-1,-1);

addMouseListener(this);//增加鼠标事件

}

publicvoidmouseReleased(MouseEvente){//鼠标释放事件

point[1]=newPoint(e.getX(),e.getY()); //设置终点位置

repaint();//重绘屏幕

}

publicvoidmouseEntered(MouseEvente){}

publicvoidmouseExited(MouseEvente){}

publicvoidmouseClicked(MouseEvente){}

publicvoidmousePressed(MouseEvente){//鼠标按下时事件

point[0]=newPoint(e.getX(),e.getY());//设置起始点位置

}

publicvoidpaint(Graphicsg){

super.paint(g);

switch(shape){//根据shape值绘制图形

case0:

g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y);//绘线

break;

case1:

intwidth=point[1].x-point[0].x;

intheight=point[1].y-point[0].y;

g.drawOval(point[0].x,point[0].y,width,height);//绘椭圆

break;

case2:

width=point[1].x-point[0].x;

height=point[1].y-point[0].y;

g.drawRect(point[0].x,point[0].y,width,height);//绘矩形

break;

}

}

publicvoiddrawShape(intshape){

this.shape=shape;

}

}

PainterDemo.java

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importjavax.swing.event.*;

{

JToggleButton[]button=newJToggleButton[3];//按钮组

PainterPanelpainter=newPainterPanel();//绘图面板

publicPainterDemo(){

super("Java画图程序");//调用父类构造函数

String[]buttonName={"直线","椭圆","矩形"};//按钮文字

=newDrawShapeListener();//按钮事件

JToolBartoolBar=newJToolBar();//实例化工具栏

ButtonGroupbuttonGroup=newButtonGroup();//实例化按钮组

for(inti=0;i<button.length;i++){

button[i]=newJToggleButton(buttonName[i]);//实例化按钮

button[i].addActionListener(buttonListener);//增加按钮事件处理

buttonGroup.add(button[i]);//增加按钮到按钮组

toolBar.add(button[i]); //增加按钮到工具栏

}

Containercontainer=getContentPane();//得到窗口容器

container.add(toolBar,BorderLayout.NORTH);//增加组件到容器上

container.add(painter,BorderLayout.CENTER);

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

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

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

}

{//按钮事件处理

publicvoidactionPerformed(ActionEvente){

for(inti=0;i<button.length;i++){

if(e.getSource()==button[i]){//判断来自于哪个按钮

painter.drawShape(i);//绘制图形

}

}

}

}

publicstaticvoidmain(String[]args){

newPainterDemo();

}

}

❷ java 用鼠标在已有的图片上画图求代码

package guitest.myboard;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
import javax.swing.*;

//the point
//impress the info of one point,the x and y

class OnePoint implements Serializable {
int x;
int y;
int tool;
Color c;
int border;

public OnePoint(int x,int y,int tool,Color cc,int border){
this.x=x;
this.y=y;
this.tool=tool;
this.c=cc;
this.border=border;
}
}

class DrawingBoard extends Frame implements MouseListener,ItemListener,ActionListener,MouseMotionListener{

Button pen;
Button line ;
Button ellipse ;
Button rect ;
Button clear ;
Button colorboard ;
Button storebutton;
Button openbutton;

Choice sizechoice ;
Choice colorchoice ;

Label pensize;
Label pencolor;
Panel panel ;

FileDialog storefile;
FileDialog openfile;

FileInputStream filein;
FileOutputStream fileout;
ObjectInputStream objectin;
ObjectOutputStream objectout;

int flagtool=0;
Color flagcolor;
int border;
BasicStroke size;

OnePoint p1,p2;
Vector<OnePoint> points=new Vector<OnePoint>();

public DrawingBoard(){
pen=new Button("画笔");
line=new Button("直线");
ellipse=new Button("圆");
rect=new Button("矩形");
clear=new Button("清除");
colorboard=new Button("调色板");
storebutton=new Button("存储文件");
openbutton=new Button("打开文件");

pensize=new Label("画笔大小");
pencolor=new Label("画笔颜色");

storefile=new FileDialog(this,"存储文件",FileDialog.SAVE);
storefile.setVisible(false);
storefile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
storefile.setVisible(false);
}
});
openfile=new FileDialog(this,"打开文件",FileDialog.LOAD);
openfile.setVisible(false);
openfile.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
openfile.setVisible(false);
}
});

sizechoice=new Choice();
sizechoice.add("1");
sizechoice.add("2");
sizechoice.add("4");
sizechoice.add("6");
sizechoice.add("8");
sizechoice.addItemListener(this);

colorchoice=new Choice();
colorchoice.add("black");
colorchoice.add("red");
colorchoice.add("blue");
colorchoice.add("green");
colorchoice.addItemListener(this);

pen.addActionListener(this);
line.addActionListener(this);
ellipse.addActionListener(this);
rect.addActionListener(this);
clear.addActionListener(this);
colorboard.addActionListener(this);
storebutton.addActionListener(this);
openbutton.addActionListener(this);

panel=new Panel();

panel.add(storebutton);
panel.add(openbutton);

panel.add(pen);
panel.add(line);
panel.add(ellipse);
panel.add(rect);
panel.add(clear);

panel.add(sizechoice);
panel.add(pensize);

panel.add(colorchoice);
panel.add(pencolor);
panel.add(colorboard);

add(panel,BorderLayout.NORTH);
setBounds(100,100,700,600);
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

/**
* 添加鼠标事件的监听器,否则,鼠标的移动和点击都将无法识别!
* */
addMouseListener(this);
addMouseMotionListener(this);
}

public void paint(Graphics g){

Graphics2D g2d=(Graphics2D)g;
if(flagtool==2){ //qing chu
g.clearRect(0,0,getSize().width,getSize().height);
}

for(int i=0;i<points.size()-1;i++){
p1=(OnePoint)points.elementAt(i);
p2=(OnePoint)points.elementAt(i+1);

g2d.setColor(p1.c); //////////////需要使用Graphics2D从Graphics类中继承下来的方法 setColor()设置当前的颜色
size=new BasicStroke(p1.border,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
g2d.setStroke(size);

if(p1.tool==p2.tool){
switch(p1.tool){
case 0:
Line2D.Double line1=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line1);
break;
case 1:
Line2D.Double line2=new Line2D.Double(p1.x,p1.y,p2.x,p2.y);
g2d.draw(line2);
break;
case 3:
Ellipse2D.Double 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 4:
Rectangle2D.Double rect=new Rectangle2D.Double(p1.x,p1.y,Math.abs(p2.x-p1.x),Math.abs(p2.y-p1.y));
g2d.draw(rect);
break;
default:
}
}
}
}

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) { //鼠标点下时候,将当前的点信息记录

OnePoint pp1=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp1);
//repaint();
}

public void mouseReleased(MouseEvent e) {//鼠标松开时候,如果是画笔,则当前截断,是其余状态记下一枚点信息
if(flagtool==0){
points.addElement(new OnePoint(-1,-1,22,flagcolor,border));
}
else{
OnePoint pp2=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp2);
points.add(new OnePoint(-1,-1,22,flagcolor,border));
}
repaint();
}

public void itemStateChanged(ItemEvent e) {
if(e.getSource()==colorchoice){
String selected=colorchoice.getSelectedItem();
if(selected=="black"){
flagcolor=new Color(0,0,0);
}
else if(selected=="red"){
flagcolor=new Color(255,0,0);
}
else if(selected=="blue"){
flagcolor=new Color(0,0,255);
}
else if(selected=="green"){
flagcolor=new Color(0,255,0);
}
}
else if(e.getSource()==sizechoice){
String selected=sizechoice.getSelectedItem();
if (selected=="1"){
border=1;
}
else if(selected=="2"){
border=2*2;
}
else if(selected=="4"){
border=4*2;
}
else if(selected=="6"){
border=6*2;
}
else if(selected=="8"){
border=8*2;
}
}

}
public void update(Graphics g) { //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
paint(g);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==pen){
flagtool=0;
}
else if(e.getSource()==line){
flagtool=1;
}
else if(e.getSource()==clear){
flagtool=2;
points.removeAllElements();
repaint(); //此语要有,否则今生无法删除!
}
else if(e.getSource()==ellipse){
flagtool=3;
}
else if(e.getSource()==rect){
flagtool=4;
}
else if(e.getSource()==colorboard){
/*
* 使用 javax.swing.×包中的 JColorChooser 类的静态方法showDialog(Component component,String title,Color color ),
* 该方法的参数,component是当前显示对话框的父框架,color是设置调色板初始的被选颜色
*
* 该方法返回被选的颜色,类型为Color
* */
Color color=JColorChooser.showDialog(this, "调色板",flagcolor);
flagcolor=color;
}
else if(e.getSource()==openbutton){
openfile.setVisible(true);
if(openfile.getFile()!=null){
int temp=flagtool;
flagtool=2;
repaint();
try{
points.removeAllElements();
File file=new File(openfile.getDirectory(),openfile.getFile());
filein=new FileInputStream(file);
objectin=new ObjectInputStream(filein);
points=(Vector)objectin.readObject();
objectin.close();
filein.close();
flagtool=temp;
repaint();
}
catch(Exception ee){
System.out.println(ee.toString());
}
}
}
else if(e.getSource()==storebutton){
storefile.setVisible(true);
if(storefile.getFile()!=null){
try {
File file=new File(storefile.getDirectory(),storefile.getFile());
fileout=new FileOutputStream(file);
objectout=new ObjectOutputStream(fileout);
objectout.writeObject(points);
objectout.close();
fileout.close();
repaint();
}
catch (FileNotFoundException e1) {
System.out.println(e1.toString());
e1.printStackTrace();
} catch (IOException ee) {
System.out.println(ee.toString());
ee.printStackTrace();
}
}
}
}

public void mouseDragged(MouseEvent e) {//鼠标拖动时候,//当且仅当 flagtool==0,或者表示为橡皮的时候
//才将拖动过程中涉及到的点全部记录下来,并且调用repain()方法,重画当前
// TODO Auto-generated method stub
if(flagtool==0){
OnePoint pp3=new OnePoint(e.getX(),e.getY(),flagtool,flagcolor,border);
points.addElement(pp3);
repaint();
}
}

public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}

}

public class PaintBoard{
public static void main(String[] args){
DrawingBoard oneBorder=new DrawingBoard();
}
}

❸ 用java编写一个简单的画图程序。不用复杂

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代码

//画布类
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JPanel;

public class MyPanel extends JPanel{

public void drawRect(Graphics g, int x, int y, int width, int height){
g.drawRect(x, y, width, height);
}

public void drawOval(Graphics g, int x, int y, int width ,int height){
g.drawOval(x, y, width, height);
}

public void drawLine(Graphics g, int x1 ,int y1 ,int x2, int y2){
g.drawLine(x1, y1, x2, y2);
}

public void drawTriangle(Graphics g, int x, int y, int borderSize){
int[] xx = {borderSize,x,borderSize*2};
int[] yy = {y,borderSize*2,borderSize*2};
g.drawPolygon(new Polygon(xx,yy,3));
}
}

//主界面类
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPaint extends JFrame implements ActionListener{
private JPanel toolArea;
private MyPanel imageArea;
private JButton rect;
private JButton oval;
private JButton line;
private JButton triangle;
public MyPaint() {
rect = new JButton("矩形");
rect.addActionListener(this);
oval = new JButton("圆形");
oval.addActionListener(this);
line = new JButton("直线");
line.addActionListener(this);
triangle = new JButton("三角");
triangle.addActionListener(this);

toolArea = new JPanel(new GridLayout(4,1));
toolArea.add(rect,0);
toolArea.add(oval,1);
toolArea.add(line,2);
toolArea.add(triangle,3);

imageArea = new MyPanel();

this.add("West",toolArea);
this.add("Center",imageArea);
this.setVisible(true);
this.setBounds(112, 84, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void actionPerformed(ActionEvent e) {
Graphics g = imageArea.getGraphics();
if(e.getSource() == rect){
imageArea.drawRect(g, 10, 10, 500, 300);
}else if(e.getSource() == oval){
imageArea.drawOval(g, 50, 50, 100, 100);
}else if(e.getSource() == line){
imageArea.drawLine(g, 20, 20, 80, 80);
}else if(e.getSource() == triangle){
imageArea.drawTriangle(g, 0, 40, 100);
}
}

public static void main(String[] args) {
new MyPaint();
}

}

有点麻烦,没写完,先给你开个头吧。你现在自己写写,有什么不懂的知识点再问我好啦。我吃早餐去了。

❺ java绘图,求代码

上代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.Paint;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.*;
public class YuanYiDong extends JFrame{
private static int BANJIN=0;
private static int X=0;
private static int Y=0;
JTextField rTxt=new JTextField(5);
JTextField xField=new JTextField(5);
JTextField yField=new JTextField(5);
JButton paintBt=new JButton("画");
JLabel huaban=new huaban();
JPanel jPanel=new JPanel();
JLabel banjingLabel,xLabel,yLabel;
public YuanYiDong(){
banjingLabel=new JLabel("半径");
xLabel=new JLabel("X坐标");
yLabel=new JLabel("Y坐标");
this.setTitle("圆的移动");
this.setLocation(300,100);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.add(rTxt);
jPanel.setLayout(new FlowLayout());
add(huaban,BorderLayout.CENTER);
jPanel.add(banjingLabel);
jPanel.add(rTxt);
jPanel.add(xLabel);
jPanel.add(xField);
jPanel.add(yLabel);
jPanel.add(yField);
jPanel.add(paintBt);
add(jPanel,BorderLayout.NORTH);
paintBt.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
BANJIN=Integer.parseInt(rTxt.getText());
X=Integer.parseInt(xField.getText());
Y=Integer.parseInt(yField.getText());
huaban.repaint();
}
});
}
private void drawCirlce(Graphics g) {
g.setColor(Color.blue);
g.fillOval(X, Y, BANJIN,BANJIN);
}
public static void main(String[] args) {
YuanYiDong frame = new YuanYiDong();
}
public class huaban extends JLabel{
public huaban(){}
public void paint(Graphics g) {
Image image = createImage(getWidth(), getHeight());
drawCirlce(image.getGraphics());
g.drawImage(image, 0, 0, null);
}
}
}
给分吧!

❻ java绘图代码

画笔定义

importjava.awt.*;

/**

*@authorHardneedl

*/

interfaceBrush{

voiddoPaint(Graphicsg);

}

画笔工厂

importjava.awt.*;

/**

*@authorHardneedl

*/

classBrushFactory{

staticfinalintLINE_BRUSH=0;

staticfinalintELLIPSE_BBRUSH=1;

staticfinalintRECTANGLE_BRUSH=2;

staticfinalintPOLYGON_BRUSH=3;

staticfinalintELLIPSE_FILL_BRUSH=4;

staticfinalintRECTANGLE_FILL_BRUSH=5;

staticfinalBrushNO=newNONE();

staticfinalBrushLINE=newLineBrush();

staticfinalBrushELLIPSE=newEllipseBrush();

staticfinalBrushELLIPSE_FILL=newEllipseFillBrush();

staticfinalBrushRECTANGLE=newRectangleBrush();

staticfinalBrushRECTANGLE_FILL=newRectangleFillBrush();

staticfinalBrushPOLYGON=newPolygonBrush();

BrushgetBrush(intbrushIndex){

switch(brushIndex){

caseLINE_BRUSH:returnLINE;

caseELLIPSE_BBRUSH:returnELLIPSE;

caseRECTANGLE_BRUSH:returnRECTANGLE;

caseRECTANGLE_FILL_BRUSH:returnRECTANGLE_FILL;

casePOLYGON_BRUSH:returnPOLYGON;

caseELLIPSE_FILL_BRUSH:returnELLIPSE_FILL;

default:returnNO;

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLACK);

gg.drawLine(70,70,200,200);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.drawOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.PINK);

gg.fillOval(100,100,200,50);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawPolygon(newint[]{48,50,244,483,310},newint[]{36,192,281,302,77},5);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.drawRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.BLUE);

gg.fillRect(70,70,100,100);

gg.dispose();

}

}

{

publicvoiddoPaint(Graphicsg){

Graphicsgg=g.create();

gg.setColor(Color.RED);

gg.drawString("Nobrushselected!",20,30);

gg.dispose();

}

}

}

图形界面

importjavax.swing.*;

importjavax.swing.border.*;

importjava.awt.*;

importjava.awt.event.*;

/**

*@authorHardneedl

*/

finalclassDrawextendsJFrame{

publicStringgetTitle(){return"frametitle";}

=newDimension(600,400);

(){returnsize;}

publicDimensiongetMaximumSize(){returnsize;}

publicDimensiongetMinimumSize(){returnsize;}

publicDimensiongetSize(){returnsize;}

=newLineAction();

=newRectangleAction();

=newRectangleFillAction();

=newEllipseAction();

=newEllipseFillAction();

=newPolygonAction();

=newBrushFactory();

privatestaticBrushbrush;

=newJComponent(){

protectedvoidpaintComponent(Graphicsg){

super.paintComponent(g);

if(brush!=null){

brush.doPaint(g);

}

}

publicBordergetBorder(){returnBorderFactory.createLineBorder(Color.BLACK,2);}

};

Draw()throwsHeadlessException{

init();

attachListeners();

doLay();

}

privatevoidinit(){

JMenuBarmenuBar=newJMenuBar();

menuBar.add(newJMenu(lineAction));

JMenuelp=newJMenu("椭圆");

elp.add(ellipseAction);

elp.add(ellipseFillAction);

menuBar.add(elp);

JMenurct=newJMenu("矩形");

rct.add(rectangleAction);

rct.add(rectangleFillAction);

menuBar.add(rct);

menuBar.add(newJMenu(polygonAction));

setJMenuBar(menuBar);

}

privatevoidattachListeners(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

privatevoiddoLay(){

Containercontainer=getContentPane();

container.add(canvas,BorderLayout.CENTER);

JPanelbuttonsPane=newJPanel(newGridLayout(2,3));

buttonsPane.add(newJButton(lineAction));

buttonsPane.add(newJButton(ellipseAction));

buttonsPane.add(newJButton(rectangleAction));

buttonsPane.add(newJButton(polygonAction));

buttonsPane.add(Box.createHorizontalBox());

buttonsPane.add(newJButton(ellipseFillAction));

buttonsPane.add(newJButton(rectangleFillAction));

container.add(buttonsPane,BorderLayout.SOUTH);

pack();

setVisible(true);

}

{

privateRectangleAction(){super("空心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_BRUSH);

canvas.repaint();

}

}

{

privateRectangleFillAction(){super("实心矩形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.RECTANGLE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseFillAction(){super("实心椭圆");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_FILL_BRUSH);

canvas.repaint();

}

}

{

privateEllipseAction(){super("空心椭圆");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.ELLIPSE_BBRUSH);

canvas.repaint();

}

}

{

privatePolygonAction(){super("多边形");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.POLYGON_BRUSH);

canvas.repaint();

}

}

{

privateLineAction(){super("直线");}

publicvoidactionPerformed(ActionEvente){

brush=brushFactory.getBrush(BrushFactory.LINE_BRUSH);

canvas.repaint();

}

}

publicstaticvoidmain(String[]args){newDraw();}

}

❼ java 实现 简单画图功能(简单点的)

楼主给你一个我编的,直接保存成pb.java编译运行,就是你要的画图功能
____________________________________________________________________

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
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 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; i<n ;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");
}

}
}
}//end paintboard

public class pb
{
public static void main(String args[])
{ new paintboard("画图程序"); }
}

阅读全文

与java画图代码相关的资料

热点内容
车来了app如何解绑银行卡 浏览:226
安卓手机桌面上的文件夹怎么删 浏览:270
三星空调压缩机不工作 浏览:209
仿闲鱼APP源码JAVA 浏览:738
便签怎么加密荣耀 浏览:349
信捷文本编程软件下载 浏览:229
路由器端口开启命令 浏览:946
vi上一个命令 浏览:431
pdf语音朗读器 浏览:556
程序员笔试选择题 浏览:786
如何制作竖文件夹 浏览:451
旧冻库压缩机值多少钱 浏览:40
资源站源码怎么转成 浏览:10
java中的乘法 浏览:267
分数的乘除运算法则 浏览:651
java8lambda 浏览:494
90安卓系统怎么锁app 浏览:551
北大青鸟职业学校编程怎么样 浏览:605
linux的permgenspace 浏览:713
服务器如何批量离线打补丁 浏览:276