简体   繁体   English

将swing GUI更改为Processing GUI

[英]Change swing GUI to Processing GUI

I am trying to run https://github.com/anfractuosity/LSD in Processing. 我正在尝试在处理中运行https://github.com/anfractuosity/LSD
The GUI.java is incompatible because it uses swing's GUI. GUI.java不兼容,因为它使用swing的GUI。
How can I change it so that it calls Processing's GUI instead? 我如何更改它,以使其改为调用Processing的GUI?
I've tried a few combos from 我尝试了一些连击
https://forum.processing.org/two/discussion/12774/embedding-papplet-in-java-jframe https://forum.processing.org/two/discussion/12774/embedding-papplet-in-java-jframe
But I cant get it right. 但是我做对了。

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;



public class GUI extends JFrame {


    GUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        try {
            BufferedImage myPicture = ImageIO.read(new File("piet.jpg"));
             Graphics2D g2d = myPicture.createGraphics();
            int x = myPicture.getWidth();
            int y = myPicture.getHeight();

            HashSet<Line> lines = new HashSet<Line>();


            double [] arr = myPicture.getData().getPixels(0,0,x,y,new double[x*y*3]);

            double [] arr2 = new double[x*y];

            System.out.println(arr.length);
            int c=0;
            for(int i = 0; i < arr.length-3; i+=3) {
                double B = arr[i];
                double G = arr[i+1];
                double R = arr[i+2];
                double level = R * 0.2126 + G * 0.7152 + B * 0.0722;
                arr2[c++] = level;
            }

            LSD lsd = new LSD();

            double [] out = lsd.lsd(arr2,x,y);

            for(int i = 0; i < lsd.n_out; i++) {
                for (int j = 0; j < 7; j++)

                lines.add(new Line(out[7 * i + 0], out[7 * i + 1],
                        out[7 * i + 2], out[7 * i + 3]));

            }

            for ( Line l : lines) {
                g2d.drawLine((int)l.x1,(int)l.y1,(int)l.x2,(int)l.y2);
            }

            JLabel picLabel = new JLabel(new ImageIcon(myPicture));
            add(picLabel);


        } catch (IOException e) {

        }

        setSize(800,800);
            setVisible(true);
    }


    public static void main(String [] args){
        new GUI();

    }


}

You can't just change a couple things and have this code work in Processing. 您不能只更改几件事并使此代码在Processing中工作。 Swing and Processing are completely different. 摇摆和处理完全不同。

You're going to have to take a step back and understand what your code does. 您将不得不退后一步,了解代码的功能。 Describe what it does, in English. 用英语描述它的作用。 Then take that English and implement code that accomplishes the same thing in Processing. 然后使用该英语,并实现在Processing中完成相同任务的代码。

To get the ball rolling, here are a few differences: 为了使球滚动起来,这里有一些区别:

  • Processing uses a PApplet instead of JFrame . 处理使用PApplet而不是JFrame
  • Processing uses a PImage instead of BufferedImage . 处理使用PImage而不是BufferedImage
  • Processing uses a PVector class instead of Point . 处理使用PVector类代替Point
  • Processing does not have components like JLabel . 处理没有像JLabel这样的组件。

But like I said, it's not as simple as just substituting a few keywords and having it work. 但是就像我说的那样,这不只是替换几个关键字并使之起作用那么简单。 You have to take what the program does and then do that in Processing. 您必须先执行程序的操作 ,然后在“处理”中执行该操作。

More info can be found in the reference . 参考资料中可以找到更多信息。 Please try something, and post a MCVE if you get stuck. 请尝试尝试,如果遇到问题,请发布MCVE Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM