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

📄 graphicsborderedcanvas.java

📁 使用java application 的共享白板系统
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;

import java.io.*;

public class GraphicsBorderedCanvas extends BorderedCanvas 
		implements GraphicOps,MouseListener, MouseMotionListener {

         private static final long serialVersionUID = 6747854543777651818L;
         public GraphicsBorderedCanvas() {
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}

	// GraphicOps interface
	public void setColor(Color color) {
		// Set drawing color
		drawingColor = color;
	}

	public void setShape(int shape) {
		// Set drawing shape
		drawingShape = shape;
	}

	public void clearCanvas() {
		// Remove everything from the drawing area
		int count = drawingOps.size();
		drawingOps.removeAllElements();
		
		// Inform listeners
		firePropertyChange(SHAPE_PROPERTY, new Integer(count),
							new Integer(0));
		
		repaint();
	}

	public void removeLast() {
		// Remove last drawn item
		if (drawingOps.isEmpty() == false && currentShape == null) {
			int count = drawingOps.size();

			DrawnShape d = (DrawnShape)drawingOps.lastElement();
			drawingOps.removeElementAt(count - 1);
			Rectangle r = d.getBoundingBox();
			repaint(r.x, r.y, r.width, r.height);
			
			// Inform listeners
			firePropertyChange(SHAPE_PROPERTY, new Integer(count),
								new Integer(count - 1));
		}
	}

	// Print the canvas contents
	public void printCanvas() {
		if (drawingOps.isEmpty() == false) {
			// Locate the frame that we are 
			Container c = this.getTopLevelAncestor();
			if (c != null && c instanceof Frame) {
				c.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				Properties props = new Properties();
				PrintJob pj = Toolkit.getDefaultToolkit().getPrintJob((Frame)c,
								"Print Drawn Graphics",
								props);
				if (pj != null) {
					Graphics g = pj.getGraphics();
					Dimension pd = pj.getPageDimension();
					Dimension od = this.getSize();

					g.translate((pd.width - od.width)/2, (pd.height - od.height)/2);
					paintComponent(g);

					g.dispose();		// Printing happens here
					pj.end();
				}
				c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}															
		}
	}

	// Get the number of shapes on the canvas
	public int getShapeCount() {
		return drawingOps.size();
	}

	// Implementation
	public void paintComponent(Graphics g) {
		super.paintComponent(g);		// Fill the background
	
		int len = drawingOps.size();
		for (int i = 0; i < len; i++) {
			DrawnShape d = (DrawnShape)drawingOps.elementAt(i);
			if (d.isPermanent()) {
				g.setPaintMode();
			} else {
				g.setXORMode(getBackground());
			}
			d.draw(g);
		}
		g.setPaintMode();				// Restore paint mode
	}

	// Shape manipulation
	protected DrawnShape getNewShape(int x, int y) {
		switch (drawingShape) {
		case GraphicOps.DRAWN_LINE:
			return new DrawnLine(drawingColor, x, y);
		case GraphicOps.DRAWN_RECT:
			return new DrawnRectangle(drawingColor, x, y);
		case GraphicOps.DRAWN_ROUND_RECT:
			return new DrawnRoundedRectangle(drawingColor, x, y);
		case GraphicOps.FILLED_RECT:
			return new FilledRectangle(drawingColor, x, y);
		case GraphicOps.FILLED_ROUND_RECT:
			return new FilledRoundedRectangle(drawingColor, x, y);
		case GraphicOps.DRAWN_OVAL:
			return new DrawnOval(drawingColor, x, y);
		case GraphicOps.FILLED_OVAL:
			return new FilledOval(drawingColor, x, y);
		default:
			throw new IllegalArgumentException("Illegal shape - " + drawingShape);
		}
	}

	public void drawShape() {
		if (currentShape != null) {
			Graphics g = getGraphics();
			if (currentShape.isPermanent()) {
				g.setPaintMode();
			} else {
				g.setXORMode(getBackground());
			}
			currentShape.draw(g);
			g.dispose();
		}
	}

	// MouseListener interface
	public void mouseEntered(MouseEvent evt) {};
	public void mouseExited(MouseEvent evt) {};

	public void mousePressed(MouseEvent evt) {
		if ((evt.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
			currentShape = getNewShape(evt.getX(), evt.getY());
			setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			drawingOps.addElement(currentShape);
			drawShape();
		}
	}

	public void mouseReleased(MouseEvent evt) {
		if ((evt.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
			if (currentShape != null) {
				drawShape();		// Remove shape
				currentShape.changeEnd(evt.getX(), evt.getY());
				currentShape.makePermanent();
				drawShape();		// Redraw it
				
				// Inform listeners
				int count = drawingOps.size();
				firePropertyChange(SHAPE_PROPERTY, new Integer(count - 1),
									new Integer(count));
				
				//currentShape = null;
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
		
	
                    
            try{
            ByteArrayOutputStream buffers = new ByteArrayOutputStream();
            
            ObjectOutputStream out = new ObjectOutputStream(buffers);
            
           // System.out.println("currentShape is "  + currentShape);
            out.writeObject(currentShape);
            out.close();
            
            byte mes1[]= buffers.toByteArray();//这里返回的就是你对像的字节数组了

            
            InetAddress inetaddress=InetAddress.getByName("235.0.0.125");
            
            DatagramPacket datagrampacket1=
				new DatagramPacket(mes1,mes1.length,inetaddress,8000);
            System.out.println(mes1.length);
			MulticastSocket multicastsocket1=new MulticastSocket(8000);
			multicastsocket1.send(datagrampacket1);
			
			currentShape = null;
            repaint();
            }
			catch(Exception ee){
	            System.out.println(ee);
	            
	            }
	}

        }
                
 }                
	public void mouseClicked(MouseEvent evt) {};

	// MouseMotionListener interface
	public void mouseMoved(MouseEvent evt) {};

	public void mouseDragged(MouseEvent evt) {
		if (currentShape != null) {
			drawShape();		// Remove shape
			currentShape.changeEnd(evt.getX(), evt.getY());
			drawShape();		// Redraw it
                    }
        
        }
	


	// Private state
	public Color drawingColor;	// Drawing color
	public int drawingShape;		// Current shape
	public Vector drawingOps = new Vector();
	public DrawnShape currentShape;
         					// Current object being drawn
}

⌨️ 快捷键说明

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