how to run/compile java code from JTextArea at Runtime? ----urgent!!! college project

Posted by Lokesh Kumar on Stack Overflow See other posts from Stack Overflow or by Lokesh Kumar
Published on 2010-03-24T21:26:16Z Indexed on 2010/03/24 21:33 UTC
Read the original article Hit count: 330

Filed under:
|
|
|
|

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;

}

so,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 u 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 above class JInternalFrame (ImageFrame) is not visible even i have declared it visible. so, ImageFrame is not visible while compiling and running above class. U have to identify this problem before running it.

© Stack Overflow or respective owner

Related posts about bufferedimage

Related posts about java