📄 painter.java
字号:
public void clear() {
gList.clear();
curNumber = 0;
}
public void draw(Graphics g) {
for(int i=0; i<curNumber; ++i)
((Graphic)gList.get(i)).draw(g);
}
}
abstract class AShape extends Graphic implements Cloneable
{
protected int x1, y1, x2, y2;
protected Color c = null;
public AShape() {c = Color.black;}
public AShape(Color c) {this.c = c;}
public void setColor(Color c) {this.c = c;}
public Color getColor() {return c;}
public void setFirstPoint(int x, int y) {
x2=x1 = x; y2=y1 =y;
}
public void setSecondPoint(int x, int y) {
x2 = x; y2 =y;
}
public Object clone() {
Object obj = null;
try{
obj = super.clone();
} catch (CloneNotSupportedException e) {}
return obj;
}
}
class Line extends AShape
{
public Line() {}
public Line(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawLine(x1, y1, x2, y2);
}
}
class Rect extends AShape
{
public Rect() {}
public Rect(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawRect(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class FillRect extends AShape
{
public FillRect() {}
public FillRect(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.fillRect(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class Oval extends AShape
{
public Oval() {}
public Oval(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.drawOval(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class FillOval extends AShape
{
public FillOval() {}
public FillOval(Color c) {super(c);}
public void draw(Graphics g) {
g.setColor(c);
g.fillOval(x2>x1?x1:x2, y2>y1?y1:y2, Math.abs(x2-x1)+1, Math.abs(y2-y1)+1);
}
}
class ToolButton extends JToggleButton
{
private Tool tool;
public ToolButton( Icon icon, Tool t) {
super(icon, false);
tool = t;
}
public Tool getTool() {return tool;}
}
class ToolPanel extends JToolBar implements ItemListener
{
private WidgeMediator mediator;
private Color c = Color.black;
private Tool curTool;
private ButtonGroup toolGrp = new ButtonGroup();
private String sizes[] = {"5","10","15","20"};
private JComboBox sizeBox = new JComboBox(sizes);
private Component cp;
private int size = 5;
public ToolPanel(WidgeMediator m, Component cp) {
super(SwingConstants.VERTICAL);
this.cp = cp;
mediator = m;
ToolButton tools[] ={
new ToolButton(new ImageIcon("res/pen.gif"), new PenTool(c)),
new ToolButton(new ImageIcon("res/line.gif"), new NormalTool(new Line(c))),
new ToolButton(new ImageIcon("res/brush.gif"), new BrushTool(c,size)),
new ToolButton(new ImageIcon("res/rect.gif"), new NormalTool(new Rect(c))),
new ToolButton(new ImageIcon("res/fillrect.gif"), new NormalTool(new FillRect(c))),
new ToolButton(new ImageIcon("res/oval.gif"), new NormalTool(new Oval(c))),
new ToolButton(new ImageIcon("res/filloval.gif"), new NormalTool(new FillOval(c))),
new ToolButton(new ImageIcon("res/spray.gif"), new SprayTool(c,size)),
new ToolButton(new ImageIcon("res/rubber.gif"), new RubberTool(size))
};
setLayout(new GridLayout(15,1));
add(new JLabel(" Draw Tools "));
for(int i=0; i<tools.length; ++i) {
add(tools[i]);
toolGrp.add(tools[i]);
tools[i].addItemListener(this);
}
add(new JLabel("Size"));
add(sizeBox);
sizeBox.addItemListener(this);
curTool = tools[0].getTool();
}
public void setColor(Color c) { curTool.setColor(this.c = c); }
public Tool getTool() {return curTool;}
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == sizeBox) {
size = Integer.parseInt(((String)sizeBox.getSelectedItem()));
curTool.setSize(size);
} else {
curTool = ((ToolButton)e.getSource()).getTool();
curTool.setSize(size);
curTool.setGraphics(cp.getGraphics());
curTool.setColor(c);
mediator.widgeChanged(this);
}
}
}
class DrawPanel extends JPanel implements MouseListener,MouseMotionListener
{
private Picture picture = new Picture();
private Tool tool = null;
private BufferedImage orgimg, bufimg, offimg;
private Graphics gbufimg, goffimg;
private int width = 800, height = 600, sx = 5, sy=5;
private int type = BufferedImage.TYPE_3BYTE_BGR;
private static boolean init = true;
public DrawPanel(){
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.gray);
setDoubleBuffered(true);
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
}
public void paint(Graphics g){
super.paint(g);
if(init == true) {
newPicture();
init = false;
}
goffimg.drawImage(bufimg,0,0,width,height,0,0,width,height,null);
if(tool != null)
tool.draw(goffimg);
g.drawImage(offimg,0,0,width,height,0,0,width,height,null);
}
public void newPicture() {
width = 800; height = 600;
type = BufferedImage.TYPE_3BYTE_BGR;
orgimg = new BufferedImage(width, height, type);
initBufimg();
Graphics g = orgimg.getGraphics();
g.setColor(Color.pink);
g.fillRect(0, 0, width, height);
g.setColor(Color.WHITE);
g.fillRect(sx, sy, width-2*sx, height-2*sy);
clear();
}
private void initBufimg() {
width = orgimg.getWidth();
height = orgimg.getHeight();
type = orgimg.getType();
bufimg = new BufferedImage(width, height, type);
offimg = new BufferedImage(width, height, type);
gbufimg = bufimg.getGraphics();
goffimg = offimg.getGraphics();
}
private void draw(Graphics g){
g.drawImage(orgimg,0,0,width,height,0,0,width,height,null);
picture.draw(g);
}
public void clear(){
picture.clear();
if(tool != null)
tool.reset();
draw(gbufimg);
repaint();
}
public boolean save(String name){
if(!name.endsWith(".jpg") && !name.endsWith(".JPG"))
name += ".jpg";
boolean r = false;
File f = new File(name);
if(f == null)
return false;
try{
r = ImageIO.write(bufimg, "JPEG", f);
} catch(Exception e) {}
return r;
}
public boolean open(String name){
BufferedImage img = null;
try{
img = ImageIO.read(new File(name));
} catch(Exception e) {}
if(img == null)
return false;
orgimg = img;
initBufimg();
clear();
return true;
}
public void setColor(Color c){
if(tool != null && c!=null)
tool.setColor(c);
}
public void setTool(Tool t){
if(t != null)
tool = t;
}
public void mousePressed(MouseEvent e){
int x = e.getX(), y = e.getY();
if(tool!=null && x<=width && y<=height && tool.mouseDown(x, y))
repaint();
}
public void mouseReleased(MouseEvent e){
int x = e.getX(), y = e.getY();
if(tool!=null && x<=width && y<=height){
tool.mouseUp(x, y);
picture.add(tool.getProduct());
}
if(picture.getLast() != null)
picture.getLast().draw(gbufimg);
}
public void mouseDragged(MouseEvent e){
int x = e.getX(), y = e.getY();
if(tool!=null && x<=width && y<=height && tool.mouseDrage(x, y))
repaint();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}
class ColorButton extends JButton
{
private Color c;
public ColorButton(Color c) {
super("123");
this.c = c;
}
public Color getColor() {return c;}
public void setColor(Color c) {
this.c = c;
repaint();
}
public void paint(Graphics g) {
g.setColor(c);
g.fill3DRect(0, 0, getWidth(), getHeight(), true);
}
}
class ColorPanel extends JToolBar implements ActionListener
{
private WidgeMediator mediator;
private JButton chooseColor = new JButton("Custom");
private ColorButton curColor = new ColorButton(Color.black);
private ColorButton customColor = new ColorButton(Color.white);
private ColorButton colors[]={ new ColorButton(Color.black),
new ColorButton(Color.blue),
new ColorButton(Color.cyan),
new ColorButton(Color.darkGray),
new ColorButton(Color.gray),
new ColorButton(Color.green),
new ColorButton(Color.lightGray),
new ColorButton(Color.magenta),
new ColorButton(Color.orange),
new ColorButton(Color.pink),
new ColorButton(Color.red),
new ColorButton(Color.white),
new ColorButton(Color.yellow)
};
public ColorPanel(WidgeMediator m) {
mediator = m;
setLayout(new FlowLayout());
add(curColor);
add(new JLabel("Select"));
for(int i=0; i<colors.length; ++i){
add(colors[i]);
colors[i].addActionListener(this);
}
add(chooseColor);
chooseColor.addActionListener(this);
add(customColor);
customColor.addActionListener(this);
}
public Color getColor() {return curColor.getColor();}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src == chooseColor) {
Color c = JColorChooser.showDialog(null, "Choose Color", customColor.getColor());
if(c != null)
customColor.setColor(c);
} else {
curColor.setColor( ((ColorButton)src).getColor() );
mediator.widgeChanged(this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -