Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用Processing时实现main()方法?_Java_Swing_Processing - Fatal编程技术网

如何在Java中使用Processing时实现main()方法?

如何在Java中使用Processing时实现main()方法?,java,swing,processing,Java,Swing,Processing,我试图在netbeans的JFrame中实现以下处理代码。 代码取自 但是,在运行MyFrame.java之后,它只在帧中显示一个静态图像。 这不是连续读数 MyProcessingSketch.java `package testprocessing; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choos

我试图在netbeans的JFrame中实现以下处理代码。 代码取自

但是,在运行MyFrame.java之后,它只在帧中显示一个静态图像。 这不是连续读数

MyProcessingSketch.java
`package testprocessing;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Arrays;
import processing.core.*;
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;



 public class MyProcessingSketch extends PApplet {


ToxiclibsSupport gfx;

Serial port;                         // The serial port
char[] teapotPacket = new char[14];  // InvenSense Teapot packet
int serialCount = 0;                 // current packet byte position
int synced = 0;
int interval = 0;

float[] q = new float[4];
Quaternion quat = new Quaternion(1, 0, 0, 0);

float[] gravity = new float[3];
float[] euler = new float[3];
float[] ypr = new float[3];

@Override
public void setup() {
    // 300px square viewport using OpenGL rendering
    size(300, 300, OPENGL);
    gfx = new ToxiclibsSupport(this);

    // setup lights and antialiasing
    lights();
    smooth();

    // display serial port list for debugging/clarity
    System.out.println(Arrays.toString(Serial.list()));

    // get the first available port (use EITHER this OR the specific port code below)
    //String portName = Serial.list()[0];

    // get a specific serial port (use EITHER this OR the first-available code above)
    String portName = "COM10";

    // open the serial port
    port = new Serial(this, portName, 9600);

    // send single character to trigger DMP init/start
    // (expected by MPU6050_DMP6 example Arduino sketch)
    port.write('r');
}

@Override
public void draw() {
    if (millis() - interval > 1000) {
        // resend single character to trigger DMP init/start
        // in case the MPU is halted/reset while applet is running
        port.write('r');
        interval = millis();
    }

    // black background
    background(0);

    // translate everything to the middle of the viewport
    pushMatrix();
    translate(width / 2, height / 2);

    // 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
    // ...and other weirdness I haven't figured out yet
    //rotateY(-ypr[0]);
    //rotateZ(-ypr[1]);
    //rotateX(-ypr[2]);

    // toxiclibs direct angle/axis rotation from quaternion (NO gimbal lock!)
    // (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
    // different coordinate system orientation assumptions between Processing
    // and InvenSense DMP)
    float[] axis = quat.toAxisAngle();
    rotate(axis[0], -axis[1], axis[3], axis[2]);

    // draw main body in red
    fill(255, 0, 0, 200);
    box(10, 10, 200);

    // draw front-facing tip in blue
    fill(0, 0, 255, 200);
    pushMatrix();
    translate(0, 0, -120);
    rotateX(PI/2);
    drawCylinder(0, 20, 20, 8);
    popMatrix();

    // draw wings and tail fin in green
    fill(0, 255, 0, 200);
    beginShape(TRIANGLES);
    vertex(-100,  2, 30); vertex(0,  2, -80); vertex(100,  2, 30);  // wing top layer
    vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30);  // wing bottom layer
    vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70);  // tail left layer
    vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70);  // tail right layer
    endShape();
    beginShape(QUADS);
    vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(  0, -2, -80); vertex(  0, 2, -80);
    vertex( 100, 2, 30); vertex( 100, -2, 30); vertex(  0, -2, -80); vertex(  0, 2, -80);
    vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2,  30); vertex(100, 2,  30);
    vertex(-2,   0, 98); vertex(2,   0, 98); vertex(2, -30, 98); vertex(-2, -30, 98);
    vertex(-2,   0, 98); vertex(2,   0, 98); vertex(2,   0, 70); vertex(-2,   0, 70);
    vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2,   0, 70); vertex(-2,   0, 70);
    endShape();

    popMatrix();
}

void serialEvent(Serial port) {
    interval = millis();
    while (port.available() > 0) {
        int ch = port.read();

        if (synced == 0 && ch != '$') return;   // initial synchronization - also used to resync/realign if needed
        synced = 1;
        print ((char)ch);

        if ((serialCount == 1 && ch != 2)
            || (serialCount == 12 && ch != '\r')
            || (serialCount == 13 && ch != '\n'))  {
            serialCount = 0;
            synced = 0;
            return;
        }

        if (serialCount > 0 || ch == '$') {
            teapotPacket[serialCount++] = (char)ch;
            if (serialCount == 14) {
                serialCount = 0; // restart packet byte position

                // get quaternion from data packet
                q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
                q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
                q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
                q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
                for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];

                // set our toxilibs quaternion to new data
                quat.set(q[0], q[1], q[2], q[3]);

                /*
                // below calculations unnecessary for orientation only using toxilibs

                // calculate gravity vector
                gravity[0] = 2 * (q[1]*q[3] - q[0]*q[2]);
                gravity[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
                gravity[2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];

                // calculate Euler angles
                euler[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
                euler[1] = -asin(2*q[1]*q[3] + 2*q[0]*q[2]);
                euler[2] = atan2(2*q[2]*q[3] - 2*q[0]*q[1], 2*q[0]*q[0] + 2*q[3]*q[3] - 1);

                // calculate yaw/pitch/roll angles
                ypr[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
                ypr[1] = atan(gravity[0] / sqrt(gravity[1]*gravity[1] + gravity[2]*gravity[2]));
                ypr[2] = atan(gravity[1] / sqrt(gravity[0]*gravity[0] + gravity[2]*gravity[2]));

                // output various components for debugging
                //println("q:\t" + round(q[0]*100.0f)/100.0f + "\t" + round(q[1]*100.0f)/100.0f + "\t" + round(q[2]*100.0f)/100.0f + "\t" + round(q[3]*100.0f)/100.0f);
                //println("euler:\t" + euler[0]*180.0f/PI + "\t" + euler[1]*180.0f/PI + "\t" + euler[2]*180.0f/PI);
                //println("ypr:\t" + ypr[0]*180.0f/PI + "\t" + ypr[1]*180.0f/PI + "\t" + ypr[2]*180.0f/PI);
                */
            }
        }
    }
}

void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
    float angle = 0;
    float angleIncrement = TWO_PI / sides;
    beginShape(QUAD_STRIP);
    for (int i = 0; i < sides + 1; ++i) {
        vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
        vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
        angle += angleIncrement;
    }
    endShape();

    // If it is not a cone, draw the circular top cap
    if (topRadius != 0) {
        angle = 0;
        beginShape(TRIANGLE_FAN);

        // Center point
        vertex(0, 0, 0);
        for (int i = 0; i < sides + 1; i++) {
            vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
            angle += angleIncrement;
        }
        endShape();
    }

    // If it is not a cone, draw the circular bottom cap
    if (bottomRadius != 0) {
        angle = 0;
        beginShape(TRIANGLE_FAN);

        // Center point
        vertex(0, tall, 0);
        for (int i = 0; i < sides + 1; i++) {
            vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
            angle += angleIncrement;
        }
        endShape();
    }
}
   public static void main(String[] args) {

        PApplet.main(new String[] { "--present", "MyProcessingSketch" });
    }  
 }   

`
MyFrame.java
  package testprocessing;

import javax.swing.JFrame;

public class MyFrame extends JFrame{
    private MyProcessingSketch mysketch;
    public MyFrame() {
    setTitle("IMU");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    mysketch = new MyProcessingSketch();
    mysketch.init();
    add(mysketch);
  }

  public static void main(String[] args) {
    MyFrame frame = new MyFrame();
    frame.pack();
    frame.setVisible(true);
  }

}
MyProcessingSketch.java
`包装测试处理;
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
导入java.util.array;
进口加工。核心。*;
输入处理。串行。*;
导入处理;
进口toxi.geom.*;
进口毒物处理。*;
公共类MyProcessingSketch扩展了PApplet{
毒物支持gfx;
串口;//串口
char[]teapotPacket=新字符[14];//InvenSense茶壶包
int serialCount=0;//当前数据包字节位置
int synced=0;
整数区间=0;
float[]q=新的float[4];
四元数quat=新的四元数(1,0,0,0);
浮动[]重力=新浮动[3];
浮动[]欧拉=新浮动[3];
浮动[]ypr=新浮动[3];
@凌驾
公共作废设置(){
//使用OpenGL渲染的300px方形视口
大小(300300,OPENGL);
gfx=新毒物支持(本);
//设置灯光和抗锯齿
灯光();
光滑的();
//显示串行端口列表以便调试/清晰
System.out.println(Arrays.toString(Serial.list());
//获取第一个可用端口(使用此端口或下面的特定端口代码)
//字符串portName=Serial.list()[0];
//获取特定的串行端口(使用此代码或上面的第一个可用代码)
字符串portName=“COM10”;
//打开串行端口
端口=新的串行端口(此端口名为9600);
//发送单个字符以触发DMP初始化/启动
//(MPU6050\U DMP6示例Arduino草图预期)
port.write('r');
}
@凌驾
公众抽签(){
如果(毫秒()-间隔>1000){
//重新发送单个字符以触发DMP init/start
//如果小程序运行时MPU停止/重置
port.write('r');
间隔=毫秒();
}
//黑色背景
背景(0);
//将所有内容转换到视口的中间
pushMatrix();
平移(宽度/2,高度/2);
//从偏航/俯仰/滚转角度进行三步旋转(万向节锁!)
//…还有其他我还没弄明白的怪事
//rotateY(-ypr[0]);
//rotateZ(-ypr[1]);
//rotateX(-ypr[2]);
//toxiclibs从四元数开始的直接角度/轴旋转(无万向节锁!)
//(轴顺序[1,3,2]和反转[-1,+1,+1]是
//不同坐标系之间的方位假设处理
//和英文森(DMP)
float[]轴=四轴到轴角度();
旋转(轴[0]、-轴[1]、轴[3]、轴[2]);
//用红色画主体
填充(255,0,0,200);
盒子(10,10200);
//用蓝色绘制前向尖端
填充(0,0,255,200);
pushMatrix();
翻译(0,0,-120);
rotateX(PI/2);
抽油缸(0,20,20,8);
popMatrix();
//画出绿色的翅膀和尾鳍
填充(0,255,0,200);
beginShape(三角形);
顶点(-100,2,30);顶点(0,2,-80);顶点(100,2,30);//机翼顶层
顶点(-100,-2,30);顶点(0,-2,-80);顶点(100,-2,30);//机翼底层
顶点(-2,0,98);顶点(-2,-30,98);顶点(-2,0,70);//左尾层
顶点(2,0,98);顶点(2,-30,98);顶点(2,0,70);//右尾层
endShape();
beginShape(四边形);
顶点(-100,2,30);顶点(-100,-2,30);顶点(0,-2,-80);顶点(0,2,-80);
顶点(100,2,30);顶点(100,-2,30);顶点(0,-2,-80);顶点(0,2,-80);
顶点(-100,2,30);顶点(-100,-2,30);顶点(100,-2,30);顶点(100,2,30);
顶点(-2,0,98);顶点(2,0,98);顶点(2,-30,98);顶点(-2,-30,98);
顶点(-2,0,98);顶点(2,0,98);顶点(2,0,70);顶点(-2,0,70);
顶点(-2,-30,98);顶点(2,-30,98);顶点(2,0,70);顶点(-2,0,70);
endShape();
popMatrix();
}
无效串行事件(串行端口){
间隔=毫秒();
while(port.available()>0){
int ch=port.read();
if(synced==0&&ch!='$')return;//初始同步-如果需要,还用于重新同步/重新对齐
同步=1;
打印((字符)ch);
if((serialCount==1&&ch!=2)
||(serialCount==12&&ch!='\r')
||(serialCount==13&&ch!='\n')){
serialCount=0;
同步=0;
返回;
}
如果(serialCount>0 | | ch='$')){
teapotPacket[serialCount++]=(char)ch;
如果(serialCount==14){
serialCount=0;//重新启动数据包字节位置
//从数据包中获取四元数

q[0]=((teapotPacket[2]您正在执行哪个主方法?我认为问题来自serialEvent()方法。您能告诉我如何在PApplet中使用serialEvent方法吗?