将图像放入JavaGUI中

将图像放入JavaGUI中,java,swing,user-interface,imagej,Java,Swing,User Interface,Imagej,我正在为一个学校项目创建一个JavaUI。我有4个按钮和JFrame中的一个图像,我希望能够使用每个按钮在图像上执行任务。每次我按下按钮,它都会使用按钮功能覆盖图像。我有按钮和功能,但我不知道如何将图像添加到JFrame。我是否使用Jpanel或画布将图像放到框架上。我使用了netbeans GUI构建器。这是我的压缩代码 //import static NewJFrame.plotWidth; import ij.IJ; import ij.ImagePlus; import ij.Windo

我正在为一个学校项目创建一个JavaUI。我有4个按钮和JFrame中的一个图像,我希望能够使用每个按钮在图像上执行任务。每次我按下按钮,它都会使用按钮功能覆盖图像。我有按钮和功能,但我不知道如何将图像添加到JFrame。我是否使用Jpanel或画布将图像放到框架上。我使用了netbeans GUI构建器。这是我的压缩代码

//import static NewJFrame.plotWidth;
import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageConverter;
import ij.process.ImageProcessor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Polygon;


public class my_JframePlugin extends javax.swing.JFrame {

    static int plotWidth =400;
    static double angleInDegrees =  40;
    static double angle = (angleInDegrees/360.0)*2.0*Math.PI;
    static int polygonMultiplier = 100;
    static boolean oneToOne;
    double picsize;

    ImagePlus img = IJ.openImage();
    ImageProcessor imgP = img.getProcessor();
    int[] x,y;
    /**
     * Creates new form NewJFrame
     */
    public my_JframePlugin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        canvas1 = new java.awt.Canvas();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 477, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 310, Short.MAX_VALUE)
        );

        jButton1.setText("FFT");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Inverse FFT");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("3D Plot");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Crop");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Add Image");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(108, 108, 108))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        IJ.log("FFT clicked");
    }                                        


    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //ImagePlus myimage = img;
        img = WindowManager.getCurrentImage();
         if (img==null)
                 {
                     //IJ.noImage();
                     IJ.log("No Image selected");
                     return;
                 }
         if (!showDialog())
                 return;
         if (img.getType()!=ImagePlus.GRAY8) 
         {
                 ImageProcessor ip = img.getProcessor();
                 ip = ip.crop(); // duplicate
                 img = new ImagePlus("temp", ip);
                  new ImageConverter(img).convertToGray8();
         }
         ImageProcessor plot = makeSurfacePlot(img.getProcessor()); 
         new ImagePlus("Surface Plot", plot).show();
         IJ.register(Surface_Plotter.class);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        IJ.log("Inverse FFT clicked");
    }                                        


    boolean showDialog() 
    {
            GenericDialog gd = new GenericDialog("Surface Plotter");
            gd.addNumericField("Width (pixels):", plotWidth, 0);
            gd.addNumericField("Angle (-90-90 degrees):", angleInDegrees, 0);
            gd.addNumericField("Polygon Multiplier (10-200%):", polygonMultiplier, 0);
            gd.addCheckbox("One Polygon Per Line", oneToOne);
            gd.showDialog();
            if (gd.wasCanceled())
                    return false;
            plotWidth = (int) gd.getNextNumber();
            angleInDegrees = gd.getNextNumber();
            polygonMultiplier = (int)gd.getNextNumber();
            oneToOne = gd.getNextBoolean();
            if (polygonMultiplier>400) polygonMultiplier = 400;
            if (polygonMultiplier<10) polygonMultiplier = 10;
            return true;
    }

    public ImageProcessor makeSurfacePlot(ImageProcessor ip) 
    {
            double angle = (angleInDegrees/360.0)*2.0*Math.PI;
            int polygons = (int)(plotWidth*(polygonMultiplier/100.0)/4);
            if (oneToOne)
                    polygons = ip.getHeight();
            double xinc = 0.8*plotWidth*Math.sin(angle)/polygons;
            double yinc = 0.8*plotWidth*Math.cos(angle)/polygons;
            boolean smooth = true;
            IJ.showProgress(0.01);
            ip.setInterpolate(true);
            ip = ip.resize(plotWidth, polygons);
            int width = ip.getWidth();
            int height = ip.getHeight();
            double min = ip.getMin();
            double max = ip.getMax();

            if (smooth)
                    ip.smooth();
            //new ImagePlus("Image", ip).show();

            int windowWidth =(int)(plotWidth+polygons*Math.abs(xinc) + 20.0);
            int windowHeight = (int)(255+polygons*yinc + 10.0);
            Image plot =IJ.getInstance().createImage(windowWidth, windowHeight);
            Graphics g = plot.getGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, windowWidth, windowHeight);

            x = new int[width+2];
            y = new int[width+2];
            double xstart = 10.0;
            if (xinc<0.0)
                    xstart += Math.abs(xinc)*polygons;
            double ystart = 0.0;
            for (int row=0; row<height; row++) {
                    double[] profile = ip.getLine(0, row, width-1, row);
                    Polygon p = makePolygon(profile, xstart, ystart);
                    g.setColor(Color.white);
                    g.fillPolygon(p);
                    g.setColor(Color.black);
                    g.drawPolygon(p);
                    xstart += xinc;
                    ystart += yinc;
                    if ((row%5)==0) IJ.showProgress((double)row/height);
            }
            IJ.showProgress(1.0);

            ip = new ColorProcessor(plot);
            byte[] bytes = new byte[windowWidth*windowHeight];
            int[] ints =(int[]) ip.getPixels();
            for (int i=0; i<windowWidth*windowHeight; i++)
                    bytes[i] = (byte)ints[i];
            ip = new ByteProcessor(windowWidth,windowHeight, bytes, null);
            return ip;
    }

    Polygon makePolygon(double[] profile, double xstart, double ystart) {
            int width = profile.length;
            for (int i=0; i<width; i++) 
                    x[i] =(int)( xstart+i);
            for (int i=0; i<width; i++) 
                    y[i] =(int)(ystart+255.0-profile[i]);
            x[width] =(int)xstart+width-1;
            y[width] = (int)ystart+255;
            x[width+1] =(int)xstart;
            y[width+1] = (int)ystart+255;

            //for (int i=0; i<width; i++)
            //  IJ.write("dbg: "+i+"  "+profile[i]+"  "+x[i]+"  "+y[i]);

            return new Polygon(x, y, width+2);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(my_JframePlugin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(my_JframePlugin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(my_JframePlugin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(my_JframePlugin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private java.awt.Canvas canvas1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}
//导入静态NewJFrame.plotWidth;
输入ij.ij;
导入ij.ImagePlus;
导入ij.WindowManager;
导入ij.gui.GenericDialog;
导入ij.process.ByteProcessor;
导入ij.process.ColorProcessor;
导入ij.process.ImageConverter;
导入ij.process.ImageProcessor;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.Polygon;
公共类my_JframePlugin扩展了javax.swing.JFrame{
静态int plotWidth=400;
静态双角度指数=40;
静态双角度=(angleInDegrees/360.0)*2.0*Math.PI;
静态整数多乘子=100;
静态布尔oneToOne;
双倍皮卡尺寸;
ImagePlus img=IJ.openImage();
ImageProcessor imgP=img.getProcessor();
int[]x,y;
/**
*创建新表单NewJFrame
*/
公共my_JframePlugin(){
初始化组件();
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
jFileChooser1=newjavax.swing.JFileChooser();
canvas1=newjava.awt.Canvas();
jPanel1=newjavax.swing.JPanel();
jButton1=newjavax.swing.JButton();
jButton2=newjavax.swing.JButton();
jButton3=newjavax.swing.JButton();
jButton4=newjavax.swing.JButton();
jButton5=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout=新的javax.swing.GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,477,短。最大值)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,310,短。最大值)
);
jButton1.setText(“FFT”);
jButton1.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton1ActionPerformed(evt);
}
});
jButton2.setText(“逆FFT”);
jButton2.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton2ActionPerformed(evt);
}
});
jButton3.setText(“3D绘图”);
jButton3.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
JButton3执行的操作(evt);
}
});
jButton4.setText(“裁剪”);
jButton4.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton4ActionPerformed(evt);
}
});
jButton5.setText(“添加图像”);
jButton5.addActionListener(新java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
jButton5ActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addGap(36,36,36)
.addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,28,Short.MAX_值)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addComponent(jButton5,javax.swing.GroupLayout.DEFAULT_SIZE,113,Short.MAX_值)
.addComponent(jButton1,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
.addComponent(jButton2,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
.addComponent(jButton3,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE)
.addComponent(jButton4,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
.addGap(108、108、108))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45,45,45)
.addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE,55,javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31,31,31)
.addComponent(jButton2,javax.swing.GroupLayout.PREFERRED_SIZE,55,javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(33,33,33)
.addComponent(jButton3,javax.swing.GroupLayout.PREFERRED_SIZE,55,javax.swing.GroupLayout.PREFERRED