⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scribble.java

📁 java 绘图的代码,应用applet, awt, 可嵌入网页进行手写和绘图等
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*    Name: Scribbler version 0.0.1    By Arash Partow   ("`-''-'").___..--''"`-._     6_ 6  )   `-.  (    ).`-..,...'`)     (_Y_.)'  ._   )  .'_ ` ....-..-'   _..`--'_..-_/  /--'_.' ,'  (il),-''  (li),'  ((!.-'  Functionality:    1. Pen Function    2. Line Function    3. Rectangle Function    4. Oval Function    5. Filled Rectangle Function    6. Filled Oval Function    7. Polygon Function (limited number of nodes) (TBC)    8. Spline Function (limited number of nodes) (TBC)    9. User defined color function (RGB setting)   10. Most of the functions contain shadow sampling before object is drawn  Colors:  Scibble contains 10 colors, includeing most primary colors.    1. Black    2. Blue    3. Green    4. Red    5. Purple    6. Orange    7. Pink    8. Gray    9. Yellow   10. User-Defined  API: The following methods have been specially implemented for       Scribbler       (01.) public void actionPerformed(ActionEvent e)       (02.) public void clearPanel(Panel p)       (03.) public void penOperation(MouseEvent e)       (04.) public void lineOperation(MouseEvent e)       (05.) public void rectOperation(MouseEvent e)       (06.) public void ovalOperation(MouseEvent e)       (07.) public void frectOperation(MouseEvent e)       (08.) public void fovalOperation(MouseEvent e)       (09.) public void eraserOperation(MouseEvent e)       (10.) public void polygonOperation(MouseEvent e)       (11.) public void splineOperation(MouseEvent e)       (12.) public boolean mouseHasMoved(MouseEvent e)       (13.) public void setActualBoundry()       (14.) public void setGraphicalDefaults(MouseEvent e)       (15.) public void setMainColor()       (16.) public void releasedPen()       (17.) public void releasedLine()       (18.) public void releasedEraser()       (19.) public void releasedRect()       (20.) public void releasedOval()       (21.) public void releasedFRect()       (22.) public void releasedFOval()       (23.) public void updateMouseCoordinates(MouseEvent e)       (24.) public void updateRGBValues()       (25.) public void mouseClicked(MouseEvent e)       (26.) public void mouseDragged(MouseEvent e)       (27.) public void mouseEntered(MouseEvent e)       (28.) public void mouseExited(MouseEvent e)       (29.) public void mouseMoved(MouseEvent e)       (30.) public void mousePressed(MouseEvent e)       (31.) public void mouseReleased(MouseEvent e)    Known bugs:    (01.) Screen loses contents after refresh or resize    (02.) On NTx and Win2k, oval operation produces outline effect          This problem has not been encountered on Win9x and linux distributions    (03.) When full screen clear occurs if another drag-type opertation being executed          after it, invalid initial coordinates are used. Copyright notice: Free use of Scribble is permitted under the guidelines and in accordance  with the most current version of the Common Public License. http://www.opensource.org/licenses/cpl.php*/import java.awt.*;import java.applet.*;import java.awt.event.*;import java.awt.event.MouseMotionListener;import java.awt.event.MouseEvent;import java.lang.Math.*;import java.text.*;import java.io.*;import java.awt.Scrollbar.*;public class Scribble extends Applet implements ActionListener, AdjustmentListener, MouseListener, MouseMotionListener{ /* Maximum X and Maximum Y coordinate values. */ private final int MAX_X           = 800; private final int MAX_Y           = 600; /* Operation Constants */ private final int  NO_OP          = 0; private final int  PEN_OP         = 1; private final int  LINE_OP        = 2; private final int  ERASER_OP      = 3; private final int  CLEAR_OP       = 4; private final int  RECT_OP        = 5; private final int  OVAL_OP        = 6; private final int  FRECT_OP       = 7; private final int  FOVAL_OP       = 8; private final int  SPLINE_OP      = 9; private final int  POLY_OP        = 10; /* Current mouse coordinates */ private int mousex                = 0; private int mousey                = 0; /* Previous mouse coordinates */ private int prevx                 = 0; private int prevy                 = 0; /* Initial state falgs for operation */ private boolean initialPen        = true; private boolean initialLine       = true; private boolean initialEraser     = true; private boolean initialRect       = true; private boolean initialOval       = true; private boolean initialFRect      = true; private boolean initialFOval      = true; private boolean initialPolygon    = true; private boolean initialSpline     = true; /* Main Mouse X and Y coordiante variables */ private int  Orx                  = 0; private int  Ory                  = 0; private int  OrWidth              = 0; private int  OrHeight             = 0; private int  drawX                = 0; private int  drawY                = 0; private int  eraserLength         = 5; private int  udefRedValue         = 255; private int  udefGreenValue       = 255; private int  udefBlueValue        = 255; /* Primitive status & color variables */ private int    opStatus           = PEN_OP; private int    colorStatus        = 1; private Color  mainColor          = new Color(0,0,0); private Color  xorColor           = new Color(255,255,255); private Color  statusBarColor     = new Color(166,166,255); private Color  userDefinedColor   = new Color(udefRedValue,udefGreenValue,udefBlueValue); /* Operation Button definitions */ private Button penButton          = new Button("Pen"); private Button lineButton         = new Button("Line"); private Button eraserButton       = new Button("Eraser"); private Button clearButton        = new Button("Clear"); private Button rectButton         = new Button("Rectangle"); private Button ovalButton         = new Button("Oval"); private Button fillRectButton     = new Button("Filled Rectangle"); private Button fillOvalButton     = new Button("Filled Oval"); private Button splineButton       = new Button("Spline"); private Button polygonButton      = new Button("Polygon"); /* Color Button definitions */ private Button blackButton        = new Button("Black"); private Button blueButton         = new Button("Blue"); private Button redButton          = new Button("Red"); private Button greenButton        = new Button("Green"); private Button purpleButton       = new Button("Purple"); private Button orangeButton       = new Button("Orange"); private Button pinkButton         = new Button("Pink"); private Button grayButton         = new Button("Gray"); private Button yellowButton       = new Button("Yellow"); private Button userDefButton      = new Button("User-Def"); /* User defined Color variables */ private Scrollbar redSlider       = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255); private Scrollbar blueSlider      = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255); private Scrollbar greenSlider     = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255); /* Assorted status values for different variables */ private TextField colorStatusBar  = new TextField(20); private TextField opStatusBar     = new TextField(20); private TextField mouseStatusBar  = new TextField(10); private TextField redValue        = new TextField(3); private TextField greenValue      = new TextField(3); private TextField blueValue       = new TextField(3); /* Labels for operation and color fields */ private Label operationLabel      = new Label("   Tool mode:"); private Label colorLabel          = new Label("   Color mode:"); private Label cursorLabel         = new Label("   Cursor:"); /* Sub panels of the main applet */ private Panel controlPanel        = new Panel(new GridLayout(11,2,0,0)); private Panel drawPanel           = new Panel(); private Panel statusPanel         = new Panel(); private Panel udefcolPanel        = new Panel(new GridLayout(3,2,0,0)); private Panel udefdemcolPanel     = new Panel(); public void init() {    setLayout(new BorderLayout());    /* setup color buttons */    controlPanel.add(blackButton);    controlPanel.add(blueButton);    controlPanel.add(redButton);    controlPanel.add(greenButton);    controlPanel.add(purpleButton);    controlPanel.add(orangeButton);    controlPanel.add(pinkButton);    controlPanel.add(grayButton);    controlPanel.add(yellowButton);    controlPanel.add(userDefButton);    blueButton.setBackground(Color.blue);    redButton.setBackground(Color.red);    greenButton.setBackground(Color.green);    purpleButton.setBackground(Color.magenta);    orangeButton.setBackground(Color.orange);    pinkButton.setBackground(Color.pink);    grayButton.setBackground(Color.gray);    yellowButton.setBackground(Color.yellow);    userDefButton.setBackground(userDefinedColor);    /* setup operation buttons */    controlPanel.add(penButton);    controlPanel.add(lineButton);    controlPanel.add(eraserButton);    controlPanel.add(clearButton);    controlPanel.add(rectButton);    controlPanel.add(ovalButton);    controlPanel.add(fillRectButton);    controlPanel.add(fillOvalButton);    controlPanel.add(splineButton);    controlPanel.add(polygonButton);    controlPanel.setBounds(0,0,100,300);    controlPanel.add(udefcolPanel);    controlPanel.add(udefdemcolPanel);    /* Add user-defined RGB buttons to panel */    udefcolPanel.add(redValue);    udefcolPanel.add(redSlider);    udefcolPanel.add(greenValue);    udefcolPanel.add(greenSlider);    udefcolPanel.add(blueValue);    udefcolPanel.add(blueSlider);    /* Add label and color text field */    statusPanel.add(colorLabel);    statusPanel.add(colorStatusBar);    /* Add label and operation text field */    statusPanel.add(operationLabel);    statusPanel.add(opStatusBar);    /* Add label and cursor text field */    statusPanel.add(cursorLabel);    statusPanel.add(mouseStatusBar);    /* Set not editable */    colorStatusBar.setEditable(false);    opStatusBar.setEditable(false);    mouseStatusBar.setEditable(false);    statusPanel.setBackground(statusBarColor);    controlPanel.setBackground(Color.white);    drawPanel.setBackground(Color.white);    add(statusPanel, "North");    add(controlPanel, "West");    add(drawPanel, "Center");    /* Setup action listener */    penButton.addActionListener(this);    lineButton.addActionListener(this);    eraserButton.addActionListener(this);    clearButton.addActionListener(this);    rectButton.addActionListener(this);    ovalButton.addActionListener(this);    fillRectButton.addActionListener(this);    fillOvalButton.addActionListener(this);    splineButton.addActionListener(this);    polygonButton.addActionListener(this);    blackButton.addActionListener(this);    blueButton.addActionListener(this);    redButton.addActionListener(this);    greenButton.addActionListener(this);    purpleButton.addActionListener(this);    orangeButton.addActionListener(this);    pinkButton.addActionListener(this);    grayButton.addActionListener(this);    yellowButton.addActionListener(this);    userDefButton.addActionListener(this);    redSlider.addAdjustmentListener(this);    blueSlider.addAdjustmentListener(this);    greenSlider.addAdjustmentListener(this);    /* Adding component listeners to main panel (applet) */    drawPanel.addMouseMotionListener(this);    drawPanel.addMouseListener(this);    this.addMouseListener(this);    this.addMouseMotionListener(this);    updateRGBValues();    opStatusBar.setText("Pen");    colorStatusBar.setText("Black"); } /*    Method is called up when an action event has been preformed.    All button operations and some labels, text field operations    are handled in this method. */ public void actionPerformed(ActionEvent e) {    /* Determine what action has occured */    /* Set the relative values           */    if (e.getActionCommand() == "Pen")       opStatus = PEN_OP;    if (e.getActionCommand() == "Line")       opStatus = LINE_OP;    if (e.getActionCommand() == "Eraser")       opStatus = ERASER_OP;    if (e.getActionCommand() == "Clear")       opStatus = CLEAR_OP;    if (e.getActionCommand() == "Rectangle")       opStatus = RECT_OP;    if (e.getActionCommand() == "Oval")       opStatus = OVAL_OP;    if (e.getActionCommand() == "Filled Rectangle")       opStatus = FRECT_OP;    if (e.getActionCommand() == "Filled Oval")       opStatus = FOVAL_OP;    if (e.getActionCommand() == "Polygon")       opStatus = POLY_OP;    if (e.getActionCommand() == "Spline")       opStatus = SPLINE_OP;    if (e.getActionCommand() == "Black")       colorStatus = 1;    if (e.getActionCommand() == "Blue")       colorStatus = 2;    if (e.getActionCommand() == "Green")       colorStatus = 3;    if (e.getActionCommand() == "Red")       colorStatus = 4;    if (e.getActionCommand() == "Purple")       colorStatus = 5;    if (e.getActionCommand() == "Orange")       colorStatus = 6;    if (e.getActionCommand() == "Pink")       colorStatus = 7;    if (e.getActionCommand() == "Gray")       colorStatus = 8;    if (e.getActionCommand() == "Yellow")       colorStatus = 9;    if (e.getActionCommand() == "User-Def")       colorStatus = 10;    initialPolygon = true;    initialSpline   = true;    /* Update Operations status bar, with current tool */    switch (opStatus)    {       case PEN_OP   : opStatusBar.setText("Pen");                       break;       case LINE_OP  : opStatusBar.setText("Line");                       break;       case ERASER_OP: opStatusBar.setText("Eraser");                       break;       case CLEAR_OP : clearPanel(drawPanel);                       break;       case RECT_OP  : opStatusBar.setText("Rectangle");                       break;       case OVAL_OP  : opStatusBar.setText("Oval");                       break;       case FRECT_OP : opStatusBar.setText("Fill-Rectangle");                       break;       case FOVAL_OP : opStatusBar.setText("Fill-Oval");                       break;       case POLY_OP : opStatusBar.setText("Polygon");                       break;       case SPLINE_OP: opStatusBar.setText("Spline");                       break;    }    /* Update Color status bar, with current color */    switch (colorStatus)    {       case  1: colorStatusBar.setText("Black");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -