user8273673
user8273673

Reputation:

Change swing GUI to Processing GUI

I am trying to run https://github.com/anfractuosity/LSD in Processing.
The GUI.java is incompatible because it uses swing's GUI.
How can I change it so that it calls Processing's GUI instead?
I've tried a few combos from
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();

    }


}

Upvotes: 0

Views: 275

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

You can't just change a couple things and have this code work in 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.

To get the ball rolling, here are a few differences:

  • Processing uses a PApplet instead of JFrame.
  • Processing uses a PImage instead of BufferedImage.
  • Processing uses a PVector class instead of Point.
  • Processing does not have components like 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. Good luck.

Upvotes: 1

Related Questions