分享
 
 
 

简单画笔

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Command.java

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public interface Command {

public final static int LINE = 2;

public final static int CIRCLE = 4;

public final static int RECTANGLE = 8;

}

ICircle.java

import java.awt.*;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public class ICircle implements Shape {

private Color color;

private int x;

private int y;

private int width;

private int height;

public ICircle(int x, int y, int width, int height, Color color) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

this.color = color;

}

public void draw(Graphics g) {

g.setColor(color);

g.drawArc(x, y, width, height, 0, 360);

}

}

ILine.java

import java.awt.*;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public class ILine implements Shape {

private int x1;

private int y1;

private int x2;

private int y2;

private Color color;

public ILine(int x1, int y1, int x2, int y2, Color color) {

this.x1 = x1;

this.y1 = y1;

this.x2 = x2;

this.y2 = y2;

this.color = color;

}

public void draw(Graphics g) {

g.setColor(color);

g.drawLine(x1, y1, x2, y2);

}

}

IRectangle.java

import java.awt.*;

import java.awt.event.*;

import java.util.Vector;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public class IRectangle implements Shape {

private int x;

private int y;

private int width;

private int height;

private Color color;

public IRectangle(int x, int y, int width, int height, Color color) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;

this.color = color;

}

public void draw(Graphics g) {

g.setColor(color);

g.drawRect(x, y, width, height);

}

}

PaintApp.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public class PaintApp extends Frame {

Panel commandPanel = new Panel();

Panel colorPanel = new Panel();

Panel shapePanel = new Panel();

Button btnRed = new Button("Red");

Button btnGreen = new Button("Green");

Button btnBlue = new Button("Blue");

Button btnLine = new Button("Line");

Button btnRectangle = new Button("Rectangle");

Button btnCircle = new Button("Circle");

Button btnUndo = new Button("Undo");

Button btnRedo = new Button("Redo");

Button btnExit = new Button("Exit");

PaintBoard paintBoard = new PaintBoard();

public PaintApp() {

this.setLayout(new BorderLayout());

btnLine.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnLine_actionPerformed(e);

}

});

btnRectangle.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnRectangle_actionPerformed(e);

}

});

btnCircle.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnCircle_actionPerformed(e);

}

});

btnExit.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnExit_actionPerformed(e);

}

});

btnUndo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnUndo_actionPerformed(e);

}

});

btnRedo.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnRedo_actionPerformed(e);

}

});

shapePanel.setLayout(new FlowLayout());

shapePanel.add(btnLine, null);

shapePanel.add(btnRectangle, null);

shapePanel.add(btnCircle, null);

shapePanel.add(btnUndo, null);

shapePanel.add(btnRedo, null);

shapePanel.add(btnExit,null);

btnRed.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnRed_actionPerformed(e);

}

});

btnGreen.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnGreen_actionPerformed(e);

}

});

btnBlue.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

btnBlue_actionPerformed(e);

}

});

colorPanel.setLayout(new FlowLayout());

colorPanel.add(btnRed, null);

colorPanel.add(btnGreen, null);

colorPanel.add(btnBlue, null);

commandPanel.setLayout(new BorderLayout());

commandPanel.add(shapePanel, BorderLayout.NORTH);

commandPanel.add(colorPanel, BorderLayout.CENTER);

this.add(commandPanel, BorderLayout.SOUTH);

this.add(paintBoard, BorderLayout.CENTER);

///////////////////////////////////////////////////////////////////////////

btnLine.setForeground(Color.red);

paintBoard.setCommand(Command.LINE);

btnRed.setForeground(Color.red);

paintBoard.setColor(Color.red);

///////////////////////////////////////////////////////////////////////////

}

void btnExit_actionPerformed(ActionEvent e) {

System.exit(0);

}

void btnUndo_actionPerformed(ActionEvent e) {

paintBoard.undo();

}

void btnRedo_actionPerformed(ActionEvent e) {

paintBoard.redo();

}

void btnLine_actionPerformed(ActionEvent e) {

btnLine.setForeground(Color.red);

btnRectangle.setForeground(Color.black);

btnCircle.setForeground(Color.black);

paintBoard.setCommand(Command.LINE);

}

void btnRectangle_actionPerformed(ActionEvent e) {

btnLine.setForeground(Color.black);

btnRectangle.setForeground(Color.red);

btnCircle.setForeground(Color.black);

paintBoard.setCommand(Command.RECTANGLE);

}

void btnCircle_actionPerformed(ActionEvent e) {

btnLine.setForeground(Color.black);

btnRectangle.setForeground(Color.black);

btnCircle.setForeground(Color.red);

paintBoard.setCommand(Command.CIRCLE);

}

void btnRed_actionPerformed(ActionEvent e) {

btnRed.setForeground(Color.red);

btnGreen.setForeground(Color.black);

btnBlue.setForeground(Color.black);

paintBoard.setColor(Color.red);

}

void btnGreen_actionPerformed(ActionEvent e) {

btnRed.setForeground(Color.black);

btnGreen.setForeground(Color.red);

btnBlue.setForeground(Color.black);

paintBoard.setColor(Color.green);

}

void btnBlue_actionPerformed(ActionEvent e) {

btnRed.setForeground(Color.black);

btnGreen.setForeground(Color.black);

btnBlue.setForeground(Color.red);

paintBoard.setColor(Color.blue);

}

public static void main(String[] args) {

PaintApp paintApp = new PaintApp();

paintApp.setSize(600, 500);

paintApp.setVisible(true);

}

}

PaintBoard.java

import java.awt.*;

import java.awt.event.*;

import java.util.*;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public class PaintBoard extends Canvas implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener {

int srcX = 0;

int srcY = 0;

int lineToX = 0;

int lineToY = 0;

boolean bMousePressing = false;

java.util.Stack vShapes = new java.util.Stack();

java.util.Stack vDelShapes = new java.util.Stack();

private int command = 0;

private Color color;

public void undo() {

if (vShapes.size() > 0) {

Object shape = vShapes.pop();

vDelShapes.push(shape);

repaint();

}

}

public void redo() {

if (vDelShapes.size() > 0) {

Object shape = vDelShapes.pop();

vShapes.push(shape);

repaint();

}

}

public void setCommand(int command) {

this.command = command;

}

public void setColor(Color color) {

this.color = color;

}

public PaintBoard() {

this.addMouseListener(this);

this.addMouseMotionListener(this);

}

public void paint(Graphics g) {

Dimension size = size();

int width = size.width ;

int height = size.height;

g.setColor(Color.white);

g.fillRect(0,0, width,height);

Shape shape = null;

java.util.Enumeration enum = vShapes.elements();

g.setColor(Color.blue);

while (enum.hasMoreElements()) {

shape = (Shape) enum.nextElement();

shape.draw(g);

}

if (bMousePressing) {

g.setColor(color);

switch (command) {

case Command.LINE:

g.drawLine(srcX, srcY, lineToX, lineToY);

break;

case Command.RECTANGLE:

if (lineToX < srcX) {

if (lineToY < srcY) {

g.drawRect(lineToX, lineToY, srcX - lineToX , srcY - lineToY);

}

else {

g.drawRect(lineToX, srcY, srcX - lineToX, lineToY - srcY);

}

}

else {

if (lineToY < srcY) {

g.drawRect(srcX, lineToY, lineToX - srcX, srcY - lineToY);

}

else {

g.drawRect(srcX, srcY, lineToX - srcX, lineToY - srcY);

}

}

break;

case Command.CIRCLE:

int w = (int)java.lang.Math.sqrt((srcX - lineToX) * (srcX - lineToX) + (srcY - lineToY) * (srcY - lineToY));

g.drawArc(srcX - w/2, srcY - w/2, w, w, 0, 360);

break;

}//End switch(command)

}

}

public void mouseClicked(MouseEvent e) {

}

public void mousePressed(MouseEvent e) {

srcX = e.getX();

srcY = e.getY();

bMousePressing = true;

}

public void mouseReleased(MouseEvent e) {

lineToX = e.getX();

lineToY = e.getY();

bMousePressing = false;

switch (command) {

case Command.LINE:

ILine line = new ILine(srcX, srcY, lineToX, lineToY, color);

vShapes.push(line);

break;

case Command.RECTANGLE:

IRectangle rectangle = null;

if (lineToX < srcX) {

if (lineToY < srcY) {

rectangle = new IRectangle(lineToX, lineToY, srcX - lineToX , srcY - lineToY, color);

}

else {

rectangle = new IRectangle(lineToX, srcY, srcX - lineToX, lineToY - srcY, color);

}

}

else {

if (lineToY < srcY) {

rectangle = new IRectangle(srcX, lineToY, lineToX - srcX, srcY - lineToY, color);

}

else {

rectangle = new IRectangle(srcX, srcY, lineToX - srcX, lineToY - srcY, color);

}

}

vShapes.push(rectangle);

break;

case Command.CIRCLE:

int w = (int)java.lang.Math.sqrt((srcX - lineToX) * (srcX - lineToX) + (srcY - lineToY) * (srcY - lineToY));

ICircle circle = new ICircle(srcX - w/2, srcY - w/2, w, w, color);

vShapes.push(circle);

break;

} //End switch(command)

repaint();

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

public void mouseDragged(MouseEvent e) {

lineToX = e.getX();

lineToY = e.getY();

repaint();

}

public void mouseMoved(MouseEvent e) {

}

}

Shape.java

import java.awt.*;

import java.awt.event.*;

/**

* @author aassd

*

* To change this generated comment edit the template variable "typecomment":

* Window>Preferences>Java>Templates.

* To enable and disable the creation of type comments go to

* Window>Preferences>Java>Code Generation.

*/

public interface Shape {

public void draw(Graphics g);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有