導航:首頁 > 編程語言 > 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畫圖代碼相關的資料

熱點內容
銀河麒麟字體庫存在哪個文件夾 瀏覽:956
魔獸加丁伺服器的航空叫什麼 瀏覽:152
花冠改裝案例哪個app多 瀏覽:515
成績單app哪個好用 瀏覽:140
北美程序員vs國內程序員 瀏覽:181
php解析xml文檔 瀏覽:121
石墨文檔APP怎麼橫屏 瀏覽:185
牆主鋼筋加密和非加密怎麼看 瀏覽:144
金山區文件夾封套定製 瀏覽:708
soho程序員 瀏覽:672
java位元組截取 瀏覽:525
php提交作業 瀏覽:815
房產還沒解壓可以辦理贈予嗎 瀏覽:224
java毫秒轉分鍾 瀏覽:753
模式識別中文pdf 瀏覽:774
c語言平均數字編譯錯誤 瀏覽:171
單片機算交流 瀏覽:45
php自適應網站 瀏覽:467
2b2t伺服器怎麼獲得許可權 瀏覽:816
c語言javaphp 瀏覽:804