?? graphicsdemo.java
字號:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.io.*;
public class GraphicsDemo extends JFrame implements Runnable{
private static final long serialVersionUID = 1652129620007407549L;
GraphicsBorderedCanvas c;
DrawnShape newshape;
public GraphicsDemo(String title) {
super(title);
Container cp = this.getContentPane();
JPanel p = new JPanel(true);
p.setLayout(new BorderLayout());
cp.add(p);
// Create the canvas in which the
// drawing will be done.
c = new GraphicsBorderedCanvas();
c.setPreferredSize(new Dimension(450, 500));
// Create the border and associate it with the canvas
Border bd = new EdgedBorder(c.getBackground());
c.setBackground(Color.white);
c.setBorder(bd);
p.add(c, BorderLayout.CENTER);
// Create the right-hand panel which
// holds the color buttons
GraphicsDemoPane gp = new GraphicsDemoPane();
p.add(gp, BorderLayout.EAST);
// Connect panel to canvas so that
// it can enable/disable buttons
c.addPropertyChangeListener(gp);
// Connect the canvas to the panel
// so that it can action button presses
gp.setDrawingArea(c);
// Select the initial color and shape
// from the button panel.
gp.selectTools();
}
public void run()
{
try
{
///////先用UDP通訊,傳遞組播號
InetAddress inetaddress=InetAddress.getByName("235.0.0.125");
// 加入多播組,接收并處理msg
MulticastSocket multicastsocket2;
multicastsocket2=new MulticastSocket(8000);
multicastsocket2.joinGroup(inetaddress);
while(true)
{
byte[] mes=new byte[20000];
DatagramPacket datagrampacket=new DatagramPacket(mes,mes.length);
multicastsocket2.receive(datagrampacket);
ByteArrayInputStream buffers = new ByteArrayInputStream(mes);
ObjectInputStream in = new ObjectInputStream(buffers);
newshape=(DrawnShape)in.readObject();
in.close();
c.drawingOps.add(newshape);
c.repaint();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void main(String[] args) {
GraphicsDemo f = new GraphicsDemo("電子白板");
f.pack();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
Thread thread=new Thread(f);
thread.start();
f.setVisible(true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -