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

📄 penciltool.java

📁 基于JAVA实现的网络电话源码
💻 JAVA
字号:
/*
 * PencilTool.java - part of __PROJECT__
 *
 * Created on 28 aprile 2006, 18.06
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package _main.windowElement.drawTools;

import _main.ClientTCP;
import _main.instancePacket.onlyClient.LinePacket;
import _main.windowElement.DrawPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.sound.sampled.Line;
import javax.swing.Icon;
import streamlib.PacketObject;


/**
 *
 * @author paolomind
 */
public class PencilTool extends AbstractTool{
    
    int oldX;
    int oldY;
    
    BasicStroke stroke = new BasicStroke(3,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND);
    
    protected final PencilPacket packet = new PencilPacket();
    protected final PencilOption dialog = new PencilOption();
    
    public final Icon icon = new javax.swing.ImageIcon(getClass().getResource("/_main/icons/pencil.gif"));
    
    public PencilTool(ClientTCP c, DrawPanel p) {
        super(c,p);
        setIcon(icon);
    }
    
    public PencilTool(DrawPanel p) {
        super(p);
        setIcon(icon);
    }
    
    class PencilPacket extends LinePacket {
        
        public void readMe(InputStream in) throws IOException {
            synchronized(this) {
                super.readMe(in);
                synchronized(panel) {
                    Graphics paint = panel.getBufferedGraphics();
                    paint.setColor(new Color(RGB));
                    if (paint instanceof Graphics2D)
                        ((Graphics2D)paint).setStroke(new BasicStroke(strokeWidth,BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND));
                    paint.drawLine(x,y,x2,y2);
                    panel.repaint(x-strokeWidth-5,y-strokeWidth-5,x2+strokeWidth+5,y2+strokeWidth+5);
                }
            }
        }
        
        public void writeMe(OutputStream out) throws IOException {
            super.writeMe(out);
        }
        
        public void setCoordinates(int x1,int y1,int x2,int y2) {
            from=core.getUsername();
            RGB=parent.getColor().getRGB();
            strokeWidth=(int)stroke.getLineWidth();
            x=x1;
            y=y1;
            this.x2=x2;
            this.y2=y2;
        }
    }
    
    public void mouseClicked(MouseEvent e) {
    }
    
    public void mousePressed(MouseEvent e) {
        oldX=e.getX();
        oldY=e.getY();
        stroke=new BasicStroke(dialog.getValue(),BasicStroke.JOIN_ROUND,BasicStroke.CAP_ROUND);
    }
    
    public void mouseReleased(MouseEvent e) {
    }
    
    public void mouseEntered(MouseEvent e) {
    }
    
    public void mouseExited(MouseEvent e) {
    }
    
    public void mouseDragged(MouseEvent e) {
        int x=e.getX();
        int y=e.getY();
        synchronized(panel) {
            Graphics paint = panel.getBufferedGraphics();
            paint.setColor(parent.getColor());
            if (paint instanceof Graphics2D)
                ((Graphics2D)paint).setStroke(stroke);
            paint.drawLine(oldX,oldY,x,y);
            panel.repaint(oldX-((int)stroke.getLineWidth())-5,
                    oldY-((int)stroke.getLineWidth())-5,
                    x+((int)stroke.getLineWidth())+5,
                    y+((int)stroke.getLineWidth())+5);
        }
        if (core.isChannelActive()) {
            try {
                synchronized (packet) {
                    packet.setCoordinates(oldX,oldY,x,y);
                    core.sendPacket(packet);
                }
            } catch (IOException ex) {}
        }
        oldX=x;
        oldY=y;
    }
    
    public void mouseMoved(MouseEvent e) {
    }
    
    public PacketObject getToolPacket() {
        return packet;
    }
    
    public void myAction() {
        dialog.setVisible(true);
        //.show();
    }
}

class PencilOption extends javax.swing.JDialog {
    
    public PencilOption() {
        super();
        initComponents();
    }
    
    public PencilOption(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jSlider1 = new javax.swing.JSlider();
        jTextField1 = new javax.swing.JTextField();
        
        getContentPane().setLayout(new java.awt.FlowLayout());
        setTitle("Stroke Width select");
        setResizable(false);
        jLabel1.setText("stroke width :");
        getContentPane().add(jLabel1);
        
        jSlider1.setMaximum(30);
        jSlider1.setPaintTicks(true);
        jSlider1.setValue(3);
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });
        
        getContentPane().add(jSlider1);
        
        jTextField1.setColumns(5);
        jTextField1.setText("3");
        jTextField1.setEditable(false);
        getContentPane().add(jTextField1);
        
        pack();
    }// </editor-fold>
    
    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
        jTextField1.setText(String.valueOf(jSlider1.getValue()));
        
    }
    
    public int getValue() {
        return jSlider1.getValue();
    }
    
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
    
}

⌨️ 快捷键说明

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