Question

I have a JInternalFrame painted with a BufferedImage and contained in the JDesktopPane of a JFrame. I also have a JTextArea where I want to write some java code (function) that takes the current JInternalFrame's painted BufferedImage as an input and after doing some manipulation on this input it returns another manipulated BufferedImage that paints the JInternalFrame with new manipulated Image again.

Manipulation java code of JTextArea:-

public BufferedImage customOperation(BufferedImage CurrentInputImg) 
{    
    Color colOld;
    Color colNew;

    BufferedImage manipulated=new BufferedImage(CurrentInputImg.getWidth(),CurrentInputImg.getHeight(),BufferedImage.TYPE_INT_ARGB);

    // make all Red pixels of current image black 
    for(int i=0;i< CurrentInputImg.getWidth();i++) {
        for(int j=0;j< CurrentInputImg.getHeight(),j++) { 
            colOld=new Color(CurrentInputImg.getRGB(i,j));
            colNew=new Color(0,colOld.getGreen(),colOld.getBlue(),colOld.getAlpha());
            manipulated.setRGB(i,j,colNew.getRGB());
        } 
    }

    return manipulated;
}

How can I run/compile this JTextArea java code at runtime and get a new manipulated image for painting on JInternalFrame?

Here is my Main class:

(This class is not actual one but I have created it for you for basic interfacing containing JTextArea,JInternalFrame,Apply Button)

import java.awt.*;    
import java.awt.event.*;    
import javax.swing.*;    
import javax.swing.event.*;    
import javax.swing.JInternalFrame;    
import javax.swing.JDesktopPane;    
import java.awt.image.*;    
import javax.imageio.*;    
import java.io.*;    
import java.io.File;    
import java.util.*;

class MyCustomOperationSystem extends JFrame    
{

    public JInternalFrame ImageFrame;     
    public BufferedImage CurrenFrameImage;

    public MyCustomOperationSystem() {
        setTitle("My Custom Image Operations");
        setSize((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());

        JDesktopPane desktop=new JDesktopPane();
        desktop.setPreferredSize(new Dimension((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));

        try {
            CurrenFrameImage=ImageIO.read(new File("c:/Lokesh.png"));
        }catch(Exception exp) {
            System.out.println("Error in Loading Image");
        }

        ImageFrame=new JInternalFrame("Image Frame",true,true,false,true);
        ImageFrame.setMinimumSize(new Dimension(CurrenFrameImage.getWidth()+10,CurrenFrameImage.getHeight()+10));
        ImageFrame.getContentPane().add(CreateImagePanel());
        ImageFrame.setLayer(1);
        ImageFrame.setLocation(100,100);
        ImageFrame.setVisible(true);  
        desktop.setOpaque(true);
        desktop.setBackground(Color.darkGray);
        desktop.add(ImageFrame);
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add("Center",desktop);
        this.getContentPane().add("South",ControlPanel());
        pack();
        setVisible(true);
    }

    public JPanel CreateImagePanel() {
        JPanel tempPanel=new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawImage(CurrenFrameImage,0,0,this);
            }
        };

        tempPanel.setPreferredSize(new Dimension(CurrenFrameImage.getWidth(),CurrenFrameImage.getHeight()));
        return tempPanel;
    }

    public JPanel ControlPanel() {
        JPanel controlPan=new JPanel(new FlowLayout(FlowLayout.LEFT));
        JButton customOP=new JButton("Custom Operation");

        customOP.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evnt) {
                JFrame CodeFrame=new JFrame("Write your Code Here");
                JTextArea codeArea=new JTextArea("Your Java Code Here",100,70);
                JScrollPane codeScrollPan=new JScrollPane(codeArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
                CodeFrame.add(codeScrollPan);
                CodeFrame.setVisible(true);
            }
        });

        JButton Apply=new JButton("Apply Code");

        Apply.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){

             // What should I do!!! Here!!!!!!!!!!!!!!!

            }
        });

        controlPan.add(customOP);
        controlPan.add(Apply);
        return controlPan;
    }

    public static void main(String s[]) {
        new MyCustomOperationSystem();
    }
}

Note: in the above class JInternalFrame (ImageFrame) is not visible even though I have declared it visible. So, ImageFrame is not visible while compiling and running the above class. You have to identify this problem before running it.

Was it helpful?

Solution

Take a look at the Java 6 Compiler API

OTHER TIPS

If you want to actually compile code from within java, it's doable but not trivial.

It's much better to use a scripting framework like Groovy--that would work great. Groovy is very java-compatible, it will almost always run java code directly (there are a few strange exceptions)

BeanShell is another scripting framework.

These both do just what you want without the effort of trying to figure out how to compile a class then load it into your existing runtime. (Actually, the problem I've seen isn't the initial compile, it's flushing your class so you can replace it with a new one after an edit).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top