Search code examples
javajframejinternalframe

How do I set a well functioning JFrame to be inside another JFrame as a internal JFrame?


Now I know how JInternalFrame works but what I am trying to do is give an already well-functioning JFrame with a space in a place where I have placed an empty Internal Frame.

I want it to grab what I have in the already existing JFrame in the package and place it in that Internal JFrame. Here is the JFrame I want to be placed into another JFrame as an internal frame. Why I wanna do it this way because the inner frame has a lot of functionality and the container Jframe would be too big to do it all in itself.

What it does is not much of an interest to the thing I wanna do but here it is: It takes images and makes them pure b/w and takes 2 clicks on the screen and stores every pixel between them in a 2d array with their coordinates.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class imageFilt {
    public static void main(String[] args)  {new imageFilt();}
    //--basic initialization
    int[] x= new int[3], y= new int[3];
    static int[] black= new int[3]; //---------------- if black is 0, then the pixel is black
    int clr;int flag=0;
    //-----------------------------initialize the screen as runnable. dont disturb the fit
    public imageFilt() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}  catch (Exception ex) {}
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
    public class TestPane extends JPanel {
        private BufferedImage master;
        private BufferedImage blackWhite;        
        public TestPane() {
            //----------------------try/catch for (pure black || pure white)
            try {
                master = ImageIO.read(new File("D:\\colz\\java\\1Aakansh thapa\\1_1_2.jpg"));
                blackWhite = new BufferedImage(master.getWidth(), master.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
                Graphics2D g2d = blackWhite.createGraphics();
                g2d.drawImage(master, 0, 0, this);
                g2d.dispose();
            }catch (IOException ex) {ex.printStackTrace();}
            //--------------------------1st and 2nd click point data and color
                this.addMouseListener(new MouseListener() {
                    int[] isFristEmpty;
                    @Override
                    public void mouseClicked(MouseEvent e1) {
                            int[] temp =new int[3];     //external container so i can get 1st and 2nd separately
                            temp[0] = (int) e1.getX();
                            temp[1] = (int) e1.getY();
                            clr = blackWhite.getRGB(temp[0], temp[1]);
                            temp[2] = (clr & 0x00ff0000) >> 16;//--------------------bit map to find if red is there or not.
                            //-------------------------------------------------------since its pure b/w, if red 0, its white.
                            if(isFristEmpty==null) {
                                isFristEmpty=temp;
                                x[0] = temp[0]; y[0] = temp[1]; black[0]=temp[2];//------1st click
                            }else {
                                x[1] = temp[0]; y[1] = temp[1]; black[1]=temp[2];//-----2nd click
                                isFristEmpty=null;  //so the 3rd click is considered 1st click again
                                flag=1;
                            }
                            if (flag==1) {
                                System.out.print("X1: "+x[0]+" & "+"Y1: "+y[0]+" "+"(225 if white): "+black[0]+"\t");
                                System.out.println("X2: "+x[1]+" & "+"Y2: "+y[1]+" "+"(225 if white): "+black[1]);
                                counter(x,y);
                            }
                    }
                    @Override   public void mousePressed(MouseEvent e) {}
                    @Override   public void mouseReleased(MouseEvent e) {}
                    @Override   public void mouseEntered(MouseEvent e) {}
                    @Override   public void mouseExited(MouseEvent e) {}
                });
        }
        //--------------------------------------------DDA block
        private void counter(int x[],int y[]) {
            if(flag!=1) return;//-------------------to only go to counter method after it takes that 2nd click
            int dx = (x[1] - x[0]);
            int dy = (y[1] - y[0]);//--------------makes it applicable for both inclinations (we do not have math.abs implies-> -ve goes as -ve)
            int step = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
            System.out.println("Steps: "+step);
            float Xinc = dx / (float) step;
            float Yinc = dy / (float) step;
            
            int[][] tog= new int[step][3];
            tog[0][0]=x[0]; tog[0][1]=y[0];
            //---------------------------------------------------------------send value of x1 and y1 to listOfCoordinates
            float xt=x[0],yt=y[0];  int i=0, j=1;   int a=0 ,b=0;
            
            while (a!=x[1] && b!=y[1]){
                xt += Xinc;
                yt += Yinc;
                a=(int) xt; b=(int) yt;
                tog[j][i] = a;
                tog[j][i+1] = b;
                //System.out.println(tog[j][i]+" "+tog[j][i+1]);           //*------------to print all that is saved
                if(i==1) i=0;
            }
        }
        //------------image size and such stuff. don't touch it
        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            if (master != null) {
                size = new Dimension(master.getWidth(), master.getHeight());
            }
            return size;
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (master != null) {
                int x = (getWidth() - (master.getWidth())) / 2;
                int y = (getHeight() - master.getHeight()) / 2;
                g.drawImage(blackWhite, x, y, this);
            }
        }
    }
}

Hope what I wanna do makes sense.


Solution

  • Just imported the frame that I want into the JFrame I want it to be in and did this.

    JInternalFrame printImg = new JInternalFrame(inTitle);
    JPanel inLabel= new JPanel();
            inLabel.add(new TestPane());
            printImg.add(inLabel);
            printImg.setVisible(true);
    

    just for people who want to know when they stumble upon the post. Wouldnt want them to stay unanswered. might also work if u copy-paste the Testpane (as the example) part and do the same thing for the rest instead of importing.