Java Datagram Programming test program

Source: Internet
Author: User
Tags add object interface return string wrapper
Programming | programs | data

This is written in the test program that is used in the back datagram program, which includes a set of custom events, a Java bean component that can scroll through the display image.

In object-oriented program design, event messages are the basic means of communication between objects. In a graphical user interface program, GUI component objects generate various types of event messages based on user interaction, which are captured by the application's event-handling code, and are then driven by the response of the message response object after the corresponding processing.

For this program, the requirement is that when the data receiver receives the data, the event is generated, which activates the event handler. To customize the related object classes, define the user event objects in Java, including defining event listeners and defining event classes, using the same as standard event methods.

The classes to be defined include: Dataswapevent,dataswaplistener, while also implementing a wrapper class Dataswaplisteneradapter class.

Dataswapevent is an event class, class list:

Package recmail.multiservice;

Import java.awt.*;
Import Java.net.DatagramPacket;

public class Dataswapevent
Extends Java.util.EventObject {
Object source;
String ID;
Java.awt.Image Image;
Private Datagrampacket DP;

Public Dataswapevent () {
Super (NULL);
}

Public dataswapevent (Object ob) {
Super (OB);
This.source = OB;
}

Public String getId () {
return ID;
}

public void SetId (String id) {
This.id = ID;
}

public void SetImage (Image img) {
This.image = img;
}

Public Image GetImage () {
return image;
}

Public Datagrampacket Getdatagram () {
return DP;
}

public void Setdatagram (Datagrampacket dp1) {
DP = DP1;
}
}

Dataswaplistener class, which is the listener interface class.

Package recmail.multiservice;

Public interface Dataswaplistener extends java.util.eventlistener{
public void ondatasendfinished (Object s,dataswapevent e);
public void ondatarecvfinished (Object s,dataswapevent e);
}

Dataswaplisteneradapter wrapper class, this is not a simple wrapper class, the function of registering listeners is also placed here.

Package recmail.multiservice;

Import java.util.*;

public class Dataswaplisteneradapter implements dataswaplistener{
protected ArrayList Listener;
Public Dataswaplisteneradapter () {
Listener = new ArrayList ();
}

protected void Adddataswaplistener (Dataswaplistener DSL) {
Listener.add (DSL);
}

protected void Removedataswaplistener (Dataswaplistener DSL) {
Listener.remove (DSL);
}

protected void Processrecvfinishedevent (Dataswapevent e) {
for (int i = 0; i < this.listener.size (); i++) {
((Dataswaplistener) this.listener.get (i)). Ondatarecvfinished (this, e);
}
}

protected void processsendfinished (Dataswapevent e) {
for (int i = 0; i < this.listener.size (); i++) {
((Dataswaplistener) this.listener.get (i)). Ondatasendfinished (this, e);
}
}

public void ondatasendfinished (Object s, dataswapevent e) {
Iterator it = Listener.iterator ();
while (It.hasnext ())
((Dataswaplistener) It.next ()). Ondatarecvfinished (S, e);
}

public void ondatarecvfinished (Object s, dataswapevent e) {
Iterator it = Listener.iterator ();
while (It.hasnext ())
((Dataswaplistener) It.next ()). Ondatarecvfinished (S, e);
}
}

This class connects the event source and the event handler for the purpose of the update.

Finally, this class Jimagepanel, used to display the image, the list is:

Package recmail.multiservice;

Import java.awt.*;
Import javax.swing.*;

public class Jimagepanel extends jscrollpane{

Jimagepane imagepane = null;

Public Jimagepanel () {
Imagepane = new Jimagepane ();
This.getviewport (). Add (Imagepane);
}

Public synchronized void setimage (image image) {
Imagepane.setimage (image);
}

Class Jimagepane
Extends JPanel {
Image img;
Insets Insets;
Dimension D;

Public Jimagepane () {
insets = This.getinsets ();
}

Public synchronized void setimage (image image) {
if (img!= null) {
Img.flush ();
img = NULL;
}
this.img = image;
if (img!= null)
SetSize (Image.getwidth (this), Image.getheight (this));
Else
SetSize (0, 0);
This.repaint ();
}


public void Paintcomponent (Graphics g) {
Super.paintcomponent (g);
if (img!= null)
G.drawimage (IMG, Insets.left, insets.top, this);
}

Public Dimension getPreferredSize () {
if (img!= null) {
Return to New Dimension (Img.getwidth (this), Img.getheight (this));
}
else {
return new Dimension (320, 160);
}
}

Public Dimension getminimumsize () {
if (img!= null) {
Return to New Dimension (Img.getwidth (this), Img.getheight (this));
}
else {
return new Dimension (320, 160);
}
}

Public Dimension getmaximumsize () {
if (img!= null) {
Return to New Dimension (Img.getwidth (this), Img.getheight (this));
}
else {
return new Dimension (320, 160);
}
}

Public Dimension GetSize () {
if (img!= null) {
Return to New Dimension (Img.getwidth (this), Img.getheight (this));
}
else {
return new Dimension (320, 160);
}
}

private void Setsiz () {
This.setpreferredsize (d);
This.setminimumsize (d);
This.setmaximumsize (d);
This.setsize (d);
}
}
}

Used for these classes will then be used.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.