📄 painter.java
字号:
//Painter.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.*;
import java.util.*;
import java.awt.image.*;
class FaceMenuItem extends JRadioButtonMenuItem
{
private String face;
public FaceMenuItem(String text, String face,boolean selected){
super(text,selected);
this.face = face;
}
public FaceMenuItem(String text, String face){
this(text, face, false);
}
public String getFace(){return face;}
}
interface WidgeMediator
{
void widgeChanged(Component widge);
}
public class Painter extends JFrame implements ActionListener,WidgeMediator
{
private String filename = null;
private Picture picture = new Picture();
private Container c = getContentPane();
private DrawPanel drawPanel = new DrawPanel();
private ColorPanel colorPanel = new ColorPanel(this);
private ToolPanel toolPanel = new ToolPanel(this,drawPanel);
private ButtonGroup faceGrp = new ButtonGroup();
private JMenuBar menuBar1 = new JMenuBar();
private JMenu fileMenu = new JMenu("File"),
editMenu = new JMenu("Edit");
private JMenu menus[] = {fileMenu,editMenu};
private JMenuItem newf = new JMenuItem("New"),
open = new JMenuItem("Open"),
save = new JMenuItem("Save"),
saveas = new JMenuItem("Save As"),
exit = new JMenuItem("Exit");
private JMenuItem fileItems[] = {newf,open,save,saveas,exit};
private JMenuItem clear = new JMenuItem("Clear");
private JMenuItem editItems[] = {clear};
private JMenuItem menuItems[][] = {fileItems,editItems};
/////////////////////////////////
public Painter(){
super("画图 ");
c.setLayout(new BorderLayout());
c.add(toolPanel,BorderLayout.WEST);
c.add(drawPanel,BorderLayout.CENTER);
c.add(colorPanel,BorderLayout.SOUTH);
for(int i=0; i<menus.length; ++i)
menuBar1.add(menus[i]);
for(int i=0; i<fileItems.length; ++i)
fileMenu.add(fileItems[i]);
for(int i=0; i<editItems.length; ++i)
editMenu.add(editItems[i]);
for(int i=0; i<menuItems.length; ++i)
for(int j=0; j<menuItems[i].length; ++j)
menuItems[i][j].addActionListener(this);
setJMenuBar(menuBar1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon icon = new ImageIcon("res/painter.gif");
setIconImage(icon.getImage());
setSize(800, 600);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
Object src = e.getSource();
if(src instanceof FaceMenuItem)
setFace(((FaceMenuItem)src).getFace());
else if(src==newf){
filename = null;
drawPanel.newPicture();
setTitle("Painter--New");
}
else if(src==open){
if(showDlg(JFileChooser.OPEN_DIALOG) != false)
drawPanel.open(filename);
}
else if(src==save){
if (filename!=null || showDlg(JFileChooser.SAVE_DIALOG)!=false)
drawPanel.save(filename);
}
else if(src == saveas){
if (showDlg(JFileChooser.SAVE_DIALOG) != false)
drawPanel.save(filename);
}
else if(src==clear)
drawPanel.clear();
else if(src==exit) {
System.exit(0);
}
}
public void widgeChanged(Component widge){
if(widge == colorPanel)
toolPanel.setColor(colorPanel.getColor());
else if(widge == toolPanel)
drawPanel.setTool(toolPanel.getTool());
}
private boolean showDlg(int dlgType) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(dlgType);
if((chooser.showDialog(null,null) != JFileChooser.APPROVE_OPTION))
return false;
filename = chooser.getSelectedFile().getPath();
setTitle("Painter--" + filename);
return true;
}
private void setFace(String face){
try{
UIManager.setLookAndFeel(face);
}catch(Exception e){return;}
SwingUtilities.updateComponentTreeUI(toolPanel);
SwingUtilities.updateComponentTreeUI(colorPanel);
SwingUtilities.updateComponentTreeUI(drawPanel);
SwingUtilities.updateComponentTreeUI(menuBar1);
}
public static void main(String args[]){
new Painter();
}
}
//Tool.java
abstract class Tool
{
protected AShape sp;
protected boolean flage;
protected Graphics g = null;
public Tool(AShape s) {sp = s;}
public void reset() {flage=false;}
public void setColor(Color c) {sp.setColor(c);}
public void setSize(int size) {}
public void setGraphics(Graphics g) {this.g = g;}
public boolean mouseDown(int x, int y) {
sp.setFirstPoint(x, y);
flage = true;
return false;
}
abstract public boolean mouseDrage(int x, int y);
abstract public boolean mouseUp(int x, int y);
abstract public void draw(Graphics g);
abstract public Graphic getProduct();
}
// the product is a AShape
class NormalTool extends Tool
{
public NormalTool(AShape s) {super(s);}
public boolean mouseDrage(int x, int y) {
sp.setSecondPoint(x, y);
return true;
}
public boolean mouseUp(int x, int y) {
return flage = false;
}
public void draw(Graphics g) {
if(flage == true)
sp.draw(g);
}
public Graphic getProduct() {
Graphic g = sp;
sp = (AShape)sp.clone();
return g;
}
}
// the product is a composite of AShape: Picture
abstract class CompositeTool extends Tool {
protected int size;
protected Picture picture = new Picture();
public CompositeTool(AShape s) {super(s);}
public CompositeTool(AShape s,int sz) {super(s); size=sz;}
public void setSize(int size) {this.size=size;}
public boolean mouseUp(int x, int y) {
return flage = false;
}
protected void addShape() {
if(flage == true) {
picture.add(sp);
sp.draw(g);
sp = (AShape)sp.clone();
flage = false;
}
}
public void draw(Graphics g) {
picture.draw(g);
if(flage == true)
sp.draw(g);
}
public Graphic getProduct() {
Graphic g = picture;
picture = new Picture();
return g;
}
}
class PenTool extends CompositeTool {
public PenTool(Color c) {
super(new Line(c));
}
public boolean mouseDrage(int x, int y) {
sp.setSecondPoint(x, y);
addShape();
sp.setFirstPoint(x, y);
flage = true;
return false;
}
}
class SprayTool extends CompositeTool implements Runnable
{
private int x, y;
Random rand = new Random();
public SprayTool(Color c,int s) {
super(new Line(c), s);
new Thread(this).start();
}
public void run() {
while(true) {
if(flage == true) {
addShape();
}
try {Thread.sleep(10);}
catch(InterruptedException e) {}
}
}
public boolean mouseDown(int x, int y) {
flage = true;
this.x = x;
this.y = y;
addShape();
return false;
}
public boolean mouseDrage(int x, int y) {
mouseDown(x, y);
return false;
}
protected synchronized void addShape() {
double a;
int x1, y1;
for(int i=0; i<size; ++i) {
a = Math.PI*(rand.nextInt(360)-180)/180;
x1 = x + (int)(i*Math.sin(a));
y1 = y + (int)(i*Math.cos(a));
sp.setFirstPoint(x1, y1);
sp.setSecondPoint(x1, y1);
picture.add(sp);
sp.draw(g);
sp = (AShape)sp.clone();
}
}
}
class BrushTool extends CompositeTool
{
private int x1, x2 ,y1, y2;
public BrushTool(Color c,int s){super(new FillOval(c),s);}
public boolean mouseDrage(int x, int y) {
int dx = (x2=x) - x1, dy = (y2=y) - y1, len =size/2;
double dist = Math.sqrt(dx*dx + dy*dy);
if(dist>len) {
double incx=len*((double)dx/dist),incy=len*((double)dy/dist);
for(int i=0,j=(int)(dist+len-1)/len; i<j; ++i)
addShape( x1 + (int)(i * incx), y1 + (int)(i * incy) );
}
addShape(x1=x2, y1=y2);
return false;
}
public boolean mouseDown(int x, int y) {
super.mouseDown(x ,y);
addShape(x1=x, y1=y);
return false;
}
protected void addShape(int x,int y) {
sp.setFirstPoint(x-size, y-size);
sp.setSecondPoint(x+size, y+size);
picture.add(sp);
sp.draw(g);
sp = (AShape)sp.clone();
}
}
class RubberTool extends BrushTool
{
public RubberTool(int s) {
super(null, s);
sp = new FillRect(Color.white);
}
public void setColor(Color c) {sp.setColor(Color.white);}
}
//Shape.java
abstract class Graphic
{
abstract public void draw(Graphics g);
}
class Picture extends Graphic
{
private ArrayList gList = new ArrayList(1);
private int curNumber;
public void add(Graphic g) {
// First remove the objects after index curNumber,which is due to undo operation
if(gList.size() > curNumber)
for(int i=gList.size()-1; i>=curNumber; --i)
gList.remove(i);
gList.add(g);
++curNumber;
}
public Graphic getLast() {
if(curNumber>0)
return (Graphic)gList.get(curNumber-1);
else
return null;
}
// This is just for redo operation
public void add() {
if( gList.size()>curNumber )
++curNumber;
}
// This is just for undo operation
public void remove() {
if( curNumber>0 )
--curNumber;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -