一個簡單的畫圖程式:實現畫直線、矩形、圓等基本功能
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;
import javax.swing.JColorChooser;
class point {
int x,y;
Color col;
int tool;
int boarder;
public 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 xx0=0,yy0=0;
int xx1=0,yy1=0;
int type=6;
int x=-1,y=-1;
int con=1;//畫筆大小
int Econ=5;//橡皮大小
int toolflag=0;//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;
//*工具面板--畫筆,直線,圓,矩形,多邊形,橡皮,清除
Panel toolpPanel;
Button eraser,drLine,drCircle,drRect;
Button clear,pen;
Choice colChoice,sizeChoice,erasercChoice;
Button colchooser,tuichu;
Label 顏色,大小B,大小E;
public paintboard(String s) {
super(s);
addMouseMotionListener(this);
addMouseListener(this);
paintinfo=new Vector();
//各工具按鈕及選擇項
//顏色選擇
colChoice= new Choice();
colChoice.add("黑色");
colChoice.add("紅色");
colChoice.add("藍色");
colChoice.add("綠色");
colChoice.addItemListener(this);
//畫筆大小選擇
sizeChoice= new Choice();
sizeChoice.add("1");
sizeChoice.add("3");
sizeChoice.add("5");
sizeChoice.add("7");
sizeChoice.add("9");
sizeChoice.addItemListener(this);
//橡皮大小選擇
erasercChoice=new Choice();
erasercChoice.add("2");
erasercChoice.add("3");
erasercChoice.add("4");
erasercChoice.add("5");
erasercChoice.addItemListener(this);
toolpPanel =new Panel();
clear=new Button("清除");
eraser =new Button("橡皮");
pen =new Button("畫筆");
drLine=new Button("畫直線");
drCircle=new Button("畫圓形");
drRect=new Button("畫矩形");
colchooser=new Button("顯示調色盤");
tuichu = new Button("退出");
//各元件時間監聽
clear.addActionListener(this);
eraser.addActionListener(this);
pen.addActionListener(this);
drLine.addActionListener(this);
drCircle.addActionListener(this);
drRect.addActionListener(this);
colchooser.addActionListener(this);
tuichu.addActionListener(this);
顏色=new Label("畫筆顏色",Label.CENTER);
大小B=new Label("畫筆大小",Label.CENTER);
大小E=new Label("橡皮大小",Label.CENTER);
//面板新增元件
toolpPanel.add(pen);
toolpPanel.add(drLine);
toolpPanel.add(drCircle);
toolpPanel.add(drRect);
toolpPanel.add(顏色);toolpPanel.add(colChoice);
toolpPanel.add(大小B);toolpPanel.add(sizeChoice);
toolpPanel.add(colchooser);
toolpPanel.add(eraser);
toolpPanel.add(大小E);toolpPanel.add(erasercChoice);
toolpPanel.add(clear);
toolpPanel.add(tuichu);
//將工具面板新增到APPLET面板
add(toolpPanel,BorderLayout.NORTH);
setBounds(60, 60, 800,650);
setVisible(true);
validate();//生效
addWindowListener(new WindowAdapter() {
@SuppressWarnings("unused")
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);//清除
switch(type)
{
case 3:
Line2D line=new Line2D.Double(xx0,yy0,xx1,yy1);
g2d.draw(line);
break;
case 4:
Ellipse2D ellipse1= new Ellipse2D.Double(xx0,yy0,Math.abs(xx1-xx0),Math.abs(yy1-yy0));
g2d.draw(ellipse1);
break;
case 5:
Rectangle2D rect1 = new Rectangle2D.Double(xx0,yy0,Math.abs(xx1-xx0),Math.abs(yy1-yy0));
g2d.draw(rect1);
default :break;
}
for (int i = 0; i < n-1; i++) {
p1 =(point)paintinfo.elementAt(i);
p2=(point)paintinfo.elementAt(i+1);
size=new BasicStroke(p1.boarder,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);
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:
break;
}
}
}
}
public void itemStateChanged(ItemEvent e) {
if (e.getSource()==colChoice) {//預選顏色
String name = colChoice.getSelectedItem();
if (name=="黑色") {
c = new Color(0,0,0);}
else if (name=="紅色") {
c = new Color(255,0,0);
}
else if (name=="綠色") {
c = new Color(0,255,0);
}
else if (name=="藍色") {
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()==erasercChoice) {//橡皮大小
String esize= erasercChoice.getSelectedItem();
if (esize=="2") {
Econ=3*3;
}
else if (esize=="3") {
Econ=4*4;
}
else if (esize=="4") {
Econ=5*5;
}
else if (esize=="5") {
Econ=6*6;
}
}
}
@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
}
@Override
public void mousePressed(java.awt.event.MouseEvent e) {
xx0=(int)e.getX();
yy0=(int)e.getY();
point p2;
switch (toolflag) {
case 3://直線
type=3;
x=(int)e.getX();
y=(int)e.getY();
p2 = new point(x, y, c, toolflag, con);
paintinfo.addElement(p2);
break;
case 4://圓
type=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:type=6;
break;
}
}
@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@SuppressWarnings("unchecked")
@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
point p3;
switch (toolflag) {
case 0://畫筆
paintinfo.addElement(cutflag);
break;
case 1:
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:
break;
}
}
@Override
public void mouseExited(java.awt.event.MouseEvent e) {}
@SuppressWarnings("unchecked")
@Override
public void mouseDragged(java.awt.event.MouseEvent e) {
xx1=(int) e.getX();
yy1=(int) e.getY();
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;
case 3:
case 4:
case 5:
repaint();
break;
default:
break;
}
}
@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
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()==tuichu) {
System.exit(0);
}
}
}
public class Huatu {
public static void main(String[] args) {
new paintboard("畫圖程式");
}
}
JAVA日記 (共50篇) 上一篇:UDP實現群聊 下一篇:封裝的實現