You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将已实现完整功能的JFrame嵌入另一个JFrame作为JInternalFrame?

将现有JFrame功能迁移至JInternalFrame

我已了解JInternalFrame的工作原理,现在需要把一个已实现完整功能的JFrame迁移到另一个JFrame中预先放置的空内部框架里。该目标JFrame的功能包括:读取图片并转为纯黑白效果,通过两次点击屏幕将两点间所有像素坐标存入二维数组。选择迁移而非直接集成是因为内部框架功能较多,直接集成会让容器JFrame过于臃肿。

以下是该目标JFrame的代码:

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);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Arpan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:10:32