Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java生成图像_Java_Image_Jframe - Fatal编程技术网

用java生成图像

用java生成图像,java,image,jframe,Java,Image,Jframe,作为我问题的背景,我正在使用COMSOL,一个有限元软件,您可以在不打开软件的情况下使用java运行该软件。因此,我尝试将它与另一个用java编写并在Eclipse中运行的软件结合起来。我的问题是让COMSOL生成一个图像文件,比如说在我的2D模拟桌面上。导出一个*.txt文件的代码几乎是相同的,并且可以工作,但是它对图像不起作用,我想看看是否有人能帮助我思考在哪里可以找到如何实现这一点的方法 我附上了###段代码,我认为这些代码足以解释我正在尝试做什么,而不会让你们所有人陷入困境 import

作为我问题的背景,我正在使用COMSOL,一个有限元软件,您可以在不打开软件的情况下使用java运行该软件。因此,我尝试将它与另一个用java编写并在Eclipse中运行的软件结合起来。我的问题是让COMSOL生成一个图像文件,比如说在我的2D模拟桌面上。导出一个*.txt文件的代码几乎是相同的,并且可以工作,但是它对图像不起作用,我想看看是否有人能帮助我思考在哪里可以找到如何实现这一点的方法

我附上了###段代码,我认为这些代码足以解释我正在尝试做什么,而不会让你们所有人陷入困境

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on Jul 24 2012, 13:33 by COMSOL 4.2.1.110. */
public class COMSOL_Model {

  public static Model run() {
    Model model = ModelUtil.create("Model");

    //code to set up and solve the model
    // and then at the end, you tell COMSOL what to export...


        //this code works...
        model.result("pg1").feature("glob1").set("data", "dset"+i);
    model.result().export("plot1").set("filename",  C:\\Users\\Alex\\Desktop\\temp_data_"+i+".txt");
    model.result().export("plot1").run();


       //this code does NOT work
    model.result("pg2").feature("surf1").set("data", "dset"+i);
    model.result("pg2").set("window", "window"+i);
    model.result("pg2").run();
    model.result().export().create("img"+i, "Image2D");
    model.result("pg2").set("window", "graphics");
    model.result("pg2").set("windowtitle", "");
    model.result().export("img"+i)
         .set("pngfilename", "C:\\Users\\Alex\\Desktop\\surface.png");
    model.result().export("img1").run();
所以我的第一个问题是,有人能从理论上或用代码解释为什么这不起作用吗?我猜是因为我需要一个特殊的方法,比如“ImageIO.write”。如有任何提示或想法,将不胜感激

我试着通过COMSOL来解决这个问题,它提供代码来创建一个GUI,显示模拟的进度,然后在GUI上打印一个2D/3D图像。我在一个大的“for”循环中使用了它,java为每个迭代创建了一个GUI和一个新的绘图。问题是,我可以让java复制框架和进度条以及GUI的所有内容,除了打印在上面的图片,我想看看是否有人有任何建议。我的一个想法是让java截图,然后裁剪图像。有什么想法吗

以下是我认为有必要的注释的大部分代码:

import com.comsol.model.*;
import com.comsol.model.util.*;
import com.comsol.modelguiswing.SwingGraphicsPanel;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.*;


public class reuse_solver {

//paste this into configurations
//C:\Program Files\COMSOL\COMSOL42a\lib\win64

private Model model;

public static void main(String[] args) {
    reuse_solver recycler= new reuse_solver();
    recycler.init();
    recycler.looper();
}

public void looper() {
    model = original.run();
    for (int iSimul = 1; iSimul<5; iSimul++){
            start(iSimul);
        }
    }

   //this is the code to have eclipse run comsol    
private void init() {
    ModelUtil.initStandalone(true);     
}

private void start(int iSimul) {

    if (iSimul==1){     
        Progress(iSimul);
        synchronized(model){
            try {
                model.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
    }else{
        Studier(iSimul);
        Progress(iSimul);
        synchronized(model){
            try {
                model.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            }
        }
}

/this creates the GUI and then runs COMSOL, showing the progress
private void Progress(int i){
    final String sol = "sol"+i;
    final int k = i;
    lookandfeel();
    final JFrame frame = new JFrame("This is an example GUI for Frying");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(1000, 730);
    final JPanel mainPanel = new JPanel();
    frame.getContentPane().add(mainPanel);
    mainPanel.setLayout(new BorderLayout());
    final SwingGraphicsPanel graphicsPanel = new SwingGraphicsPanel("window"+i, "Window"+i);
    mainPanel.add(graphicsPanel, BorderLayout.CENTER);
    SwingProgressPanel progressPanel =new SwingProgressPanel();
    mainPanel.add(progressPanel, BorderLayout.PAGE_END);
    progressPanel.updateProgressLog("Starting\n"+i+"\n");
    ProgressWorker.setContext(new SwingDemoProgressContext(progressPanel)); 
    final int pWidth = frame.getWidth();
    final int pHeight = frame.getHeight();
    frame.setVisible(true);

    ProgressWorker.run(new Runnable() {
        public void run() {
            model.sol(sol).feature().create("t1","PlotWhileSolving");
            model.sol(sol).runAll();

            exporter(k, pHeight, pWidth, frame);    

        }

    });



}

//code to help COMSOL run in a loop
private void Studier(int i) {
    //COMSOL code goes here     
}

private void exporter(int i, int pHeight, int pWidth, JFrame frame) {

            //this code will tell java to put the image in the GUI window once the simulation is done

            model.result("pg2").feature("surf1").set("data", "dset"+i);
    model.result("pg2").set("window", "window"+i);
    model.result("pg2").run();

    // this will generate an image on my desktop, but it will not put the image that was put in the GUI

    BufferedImage bi = new BufferedImage(pWidth,pHeight, BufferedImage.TYPE_INT_RGB); 
    Graphics g = bi.createGraphics();
    frame.paint(g);  //this == JComponent
    g.dispose();
    try{ImageIO.write(bi,"png",new File("C:\\Users\\Alex\\Desktop\\surf_"+i+".png"));}catch (Exception e) {}

    synchronized(model){
        model.notify();
    }
}   

private void lookandfeel() {
    try {
    UIManager.setLookAndFeel(
    UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
    }
    }
}   
导入com.comsol.model.*;
导入com.comsol.model.util.*;
导入com.comsol.modelguiswing.SwingGraphicsPanel;
导入java.awt.BorderLayout;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GraphicsConfiguration;
导入java.awt.GraphicsDevice;
导入java.awt.GraphicsEnvironment;
导入java.awt.Transparency;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.*;
公共类重用求解器{
//将其粘贴到配置中
//C:\ProgramFiles\COMSOL\COMSOL42a\lib\win64
私有模型;
公共静态void main(字符串[]args){
重用解算器回收器=新的重用解算器();
recycler.init();
回收器。活套();
}
公共无效循环器(){
model=original.run();
对于(int-iSimul=1;iSimul请参阅捕获Swing组件的映像。该代码显然与AWT组件有问题


在这种情况下,使用捕获图像,但仅捕获组件的区域,而不是整个屏幕。

COMSOL使用图形窗口创建图像以供导出。当COMSOL在独立模式下启动时,图形引擎的加载方式不允许导出。但是,在通过使用客户机-服务器模型(而不是使用独立版本)从Java运行COMSOL,并在服务器实例上启用图形

下面是我用来从COMSOL桌面或直接调用java运行的一些代码

public static void main(String[] args) throws IOException {
    String tag1 = System.getProperty("cs.currentmodel");
    if (tag1 == null) {
        // ModelUtil.initStandalone(true);
        ModelUtil.connect();
        ModelUtil.showProgress(false);
        model = ModelUtil.load("ModelTag", args[0]);
        run();
        ModelUtil.clear();
        ModelUtil.disconnect();
        System.exit(0);
    }
    else {
        model = ModelUtil.model(tag1);
        run();
    }
}
然后,您可以使用图形选项启动COMSOL服务器:

comsolserver -graphics

这将允许您的导出工作。我确信有一种方法可以用java启动COMSOL server实例,但我的项目不必这样做。

这就是我要寻找的,但问题是:我让java打印顶部图像,但GUI看起来像底部。GUI看起来像顶部,然后是大ci出现rcle,然后执行for循环中的下一步并重复该过程。我需要做什么才能获得底部图像?我希望这更清晰。