gpt4 book ai didi

java - 将使用Processing.org制作的应用程序迁移到Eclipse

转载 作者:行者123 更新时间:2023-12-01 15:13:14 32 4
gpt4 key购买 nike

我在Processing中做了一个简单的程序,效果很好。我现在尝试将我的 Java 初学者技能运用到 Eclipse 中,重新编写相同的程序。该程序采用来自 Com 端口的 Rs232 字符串并通过 Xmpp 发送它。

我遇到的问题是我无法像从任何类/选项卡进行处理一样调用 newChat.sendMessage(message) 。有人可以告诉我应该寻找什么来解决这个问题。我猜想扩展或实现 Xmpp 类是我需要做的。我喜欢简单的处理方式......

任何帮助将不胜感激。

仅供引用:Smack 库位于用于处理的代码文件夹中。

主要应用:

import processing.net.*;




void setup() {
size(400, 200);
noStroke();
background(0);
LoadSerialPort();
OpenChatConnection();
setupFilterThread ();
}

void draw() {
}



String timestampTime() {
Calendar now = Calendar.getInstance();
return String.format("%1$tH:%1$tM:%1$tS", now);
}


String timestampDate() {
Calendar now = Calendar.getInstance();
return String.format("%1$tm/%1$td/%1$tY", now);
}

过滤器类别/选项卡:

FilterThread thread1;

List FilteredArchAddressList = new ArrayList();
ArrayList CallList = new ArrayList();

void setupFilterThread () {

thread1 = new FilterThread(100, "a");
thread1.start();
}

void checkForNewCalls() {
if (CallList.size() >=1) {
println("New Call In List, Size: "+CallList.size());
FilterComPortString((String) CallList.get(0));
CallList.remove(0);
}
}

void FilterComPortString(String s) {

Message message = new Message("icu1@broadcast.server", Message.Type.normal);
//message.setTo("icu1@broadcast.x-dev");
message.setSubject("MSG_TYPE_NORMAL");
message.setBody(s);
message.setProperty("systemID", "JS1");
message.setProperty("serverTime", trim(timestampTime()));
message.setProperty("serverDate", trim(timestampDate()));

try {
newChat.sendMessage(message);
}
catch (Exception e) {
println(e);
}
}
}

class FilterThread extends Thread {

boolean running; // Is the thread running? Yes or no?
int wait; // How many milliseconds should we wait in between executions?
String id; // Thread name
int count; // counter

// Constructor, create the thread
// It is not running by default
FilterThread (int w, String s) {
wait = w;
running = false ;
id = s;
count = 0;
}

int getCount() {
return count;
}

// Overriding "start()"
void start () {
// Set running equal to true
running = true ;
// Print messages
println ("Starting thread (will execute every " + wait + " milliseconds.)");
// Do whatever start does in Thread, don't forget this!
super .start();
}


// We must implement run, this gets triggered by start()
void run () {
while (running) {
checkForNewCalls();
// Ok, let's wait for however long we should wait
try {
sleep((long )(wait));
}
catch (Exception e) {
}
}
System.out.println (id + " thread is done!"); // The thread is done when we get to the end of run()
}


// Our method that quits the thread
void quit() {
System.out.println ("Quitting.");
running = false ; // Setting running to false ends the loop in run()
// IUn case the thread is waiting. . .
interrupt();
}
}

Rs232 类别/选项卡:

import processing.serial.*;

Serial myPort; // Rs232, Serial Port
String inString; // Input string from serial port:
int lf = 10; // ASCII linefeed


String SelectedCom;

void LoadSerialPort() {


println(Serial.list());
println("________________________________________________________");

try {
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(lf);
SelectedCom = Serial.list()[0];
println("Connected to Serial Port:");
println("________________________________________________________");
}
catch (ArrayIndexOutOfBoundsException e) {
String exception = e.toString();
if (exception.contains("ArrayIndexOutOfBoundsException: 0")) {
println("NO AVAILABLE COM PORT FOUND");
}
else {
println(e);
}
}
}

void serialEvent(Serial p) {
inString = p.readString();
CallList.add(new String(inString));
}

Xmpp 类/选项卡:

String FromXmpp = "";
Chat newChat;
ChatManager chatmanager;


XMPPConnection connection;

public void OpenChatConnection() {

// This is the connection to google talk. If you use jabber, put other stuff in here.
ConnectionConfiguration config = new ConnectionConfiguration("192.168.0.103", 5222, "Jabber/XMPP");
config.setSASLAuthenticationEnabled(false);

configure(ProviderManager.getInstance());

connection = new XMPPConnection(config);

println("Connecting");
try {
//connection.DEBUG_ENABLED = true;
connection.connect();
println("Connected to: "+connection.getServiceName() );
}

catch (XMPPException e1) {
println("NOT Connected");
}

if (connection.isConnected()) {

try {

// This is the username and password of the chat client that is to run within Processing.
println("Connecting");
connection.login("System1", "test");
//connection.login("inside_processing_username@gmail.com", "yourpassword");
}
catch (XMPPException e1) {
// would probably be a good idea to put some user friendly action here.
e1.printStackTrace();
}
println("Logged in as: "+connection.getUser() );
}
chatmanager = connection.getChatManager();
// Eventhandler, to catch incoming chat events
newChat = chatmanager.createChat("icu@broadcast.server", new MessageListener() { //icu1@broadcast.x-dev //admin@x-dev
public void processMessage(Chat chat, Message message) {
// Here you do what you do with the message
FromXmpp = message.getBody();
// Process commands
//println(FromXmpp);
}
}
);


Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
for (RosterEntry entry : entries) {
System.out.println(entry);
}
}


public void configure(ProviderManager pm) {

// Private Data Storage
pm.addIQProvider("query", "jabber:iq:private", new PrivateDataManager.PrivateDataIQProvider());

// Time
try {
pm.addIQProvider("query", "jabber:iq:time", Class.forName("org.jivesoftware.smackx.packet.Time"));
}
catch (ClassNotFoundException e) {
println(("TestClient "+" Can't load class for org.jivesoftware.smackx.packet.Time"));
}

// Roster Exchange
pm.addExtensionProvider("x", "jabber:x:roster", new RosterExchangeProvider());

// Message Events
pm.addExtensionProvider("x", "jabber:x:event", new MessageEventProvider());

// Chat State
pm.addExtensionProvider("active", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("composing", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("paused", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("inactive", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());
pm.addExtensionProvider("gone", "http://jabber.org/protocol/chatstates", new ChatStateExtension.Provider());

// XHTML
pm.addExtensionProvider("html", "http://jabber.org/protocol/xhtml-im", new XHTMLExtensionProvider());

// Group Chat Invitations
pm.addExtensionProvider("x", "jabber:x:conference", new GroupChatInvitation.Provider());

// Service Discovery # Items
pm.addIQProvider("query", "http://jabber.org/protocol/disco#items", new DiscoverItemsProvider());

// Service Discovery # Info
pm.addIQProvider("query", "http://jabber.org/protocol/disco#info", new DiscoverInfoProvider());

// Data Forms
pm.addExtensionProvider("x", "jabber:x:data", new DataFormProvider());

// MUC User
pm.addExtensionProvider("x", "http://jabber.org/protocol/muc#user", new MUCUserProvider());

// MUC Admin
pm.addIQProvider("query", "http://jabber.org/protocol/muc#admin", new MUCAdminProvider());

// MUC Owner
pm.addIQProvider("query", "http://jabber.org/protocol/muc#owner", new MUCOwnerProvider());

// Delayed Delivery
pm.addExtensionProvider("x", "jabber:x:delay", new DelayInformationProvider());

// Version
try {
pm.addIQProvider("query", "jabber:iq:version", Class.forName("org.jivesoftware.smackx.packet.Version"));
}
catch (ClassNotFoundException e) {
// Not sure what's happening here.
}

// VCard
pm.addIQProvider("vCard", "vcard-temp", new VCardProvider());

// Offline Message Requests
pm.addIQProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageRequest.Provider());

// Offline Message Indicator
pm.addExtensionProvider("offline", "http://jabber.org/protocol/offline", new OfflineMessageInfo.Provider());

// Last Activity
pm.addIQProvider("query", "jabber:iq:last", new LastActivity.Provider());

// User Search
pm.addIQProvider("query", "jabber:iq:search", new UserSearch.Provider());

// SharedGroupsInfo
pm.addIQProvider("sharedgroup", "http://www.jivesoftware.org/protocol/sharedgroup", new SharedGroupsInfo.Provider());

// JEP-33: Extended Stanza Addressing
pm.addExtensionProvider("addresses", "http://jabber.org/protocol/address", new MultipleAddressesProvider());

// FileTransfer
pm.addIQProvider("si", "http://jabber.org/protocol/si", new StreamInitiationProvider());

pm.addIQProvider("query", "http://jabber.org/protocol/bytestreams", new BytestreamsProvider());

// Privacy
pm.addIQProvider("query", "jabber:iq:privacy", new PrivacyProvider());
pm.addIQProvider("command", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider());
pm.addExtensionProvider("malformed-action", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.MalformedActionError());
pm.addExtensionProvider("bad-locale", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadLocaleError());
pm.addExtensionProvider("bad-payload", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadPayloadError());
pm.addExtensionProvider("bad-sessionid", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.BadSessionIDError());
pm.addExtensionProvider("session-expired", "http://jabber.org/protocol/commands", new AdHocCommandDataProvider.SessionExpiredError());
}

最佳答案

如果您了解 Eclipse,首先您需要创建一个新的 Java 项目并将Processing 的 core.jar 添加到构建路径,然后首先创建 PApplet 的子类。如果没有,有一个非常容易使用的 eclipse 插件,名为 Proclipsing和视频指南以开始使用。

proclipsing

将代码从Processing引入到Eclipse:

您需要了解处理草图中的所有代码(包括选项卡)都会合并到单个 .java 文件中,并且草图名称是类名称。您在草图中的选项卡中定义的任何类都会成为主单个类中的嵌套类。最简单的查看方法是导出一个小程序并在生成的文件夹中查看 SketchName.java 文件。请记住,选项卡不是一个类(尽管它可以包含一个类)。

所以你有几个选择。以下是两种基本方法:

  1. 创建一个新的 PApplet 子类并开始将整个代码粘贴到其中(包括选项卡的内容)。您需要明确访问器(例如 public void setup() 而不仅仅是 void setup()),并小心使用 double/float 值(例如,如果 eclipse 提示大约 1.0,明确一点:1.0f)

  2. 另一个选项是创建多个类,正如我想象的那样,如果您使用 eclipse,您打算这样做。问题是,在处理中,选项卡中定义的变量实际上是“全局”变量。如果您在顶部的主选项卡中声明这些代码,您的代码将以相同的方式工作。另外,使用 PApplet 功能需要重构。可以使用 Java 的类(而不是 PApplet 的)调用某些函数来打破依赖性:例如System.out.println() 而不是 println(),但您可能希望您的类能够访问 PApplet 实例:

例如

public class  FilterThread extends  Thread {

boolean running; // Is the thread running? Yes or no?
int wait; // How many milliseconds should we wait in between executions?
String id; // Thread name
int count; // counter
YourSketchClass parent;

// Constructor, create the thread
// It is not running by default
FilterThread (int w, String s,YourSketchClass p) {
wait = w;
running = false ;
id = s;
count = 0;
parent = p;
}

int getCount() {
return count;
}

// Overriding "start()"
public void start () {
// Set running equal to true
running = true ;
// Print messages
System.out.println ("Starting thread (will execute every " + wait + " milliseconds.)");
// Do whatever start does in Thread, don't forget this!
super .start();
}


// We must implement run, this gets triggered by start()
public void run () {
while (running) {
parent.checkForNewCalls();
// Ok, let's wait for however long we should wait
try {
sleep((long )(wait));
}
catch (Exception e) {
}
}
System.out.println (id + " thread is done!"); // The thread is done when we get to the end of run()
}


// Our method that quits the thread
void quit() {
System.out.println ("Quitting.");
running = false ; // Setting running to false ends the loop in run()
// IUn case the thread is waiting. . .
interrupt();
}
}

关于java - 将使用Processing.org制作的应用程序迁移到Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12019317/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com