【问题标题】:generating an image in java在java中生成图像
【发布时间】:2023-03-07 22:19:01
【问题描述】:

对于我的问题的背景,我正在使用 COMSOL,这是一种有限元软件,您无需打开软件即可使用 java 运行该软件。因此,我试图将它与另一个用 java 编写的软件结合起来并在 Eclipse 中运行。我的问题是让 COMSOL 生成一个图像文件,比如在我的 2D 模拟桌面上。用于导出 *.txt 文件的代码几乎相同并且有效,但它不适用于图像,我想看看是否有人可以帮助我思考在哪里寻找以找出如何完成此操作。

我附上了 ## sn-ps 的代码,我认为这些代码足以解释我正在尝试做的事情,而不会让大家陷入困境。

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截取屏幕截图,然后裁剪图像。有什么想法吗?

以下是我认为有必要使用 cmets 的大部分代码:

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

【问题讨论】:

  • 这就是我正在寻找的东西,但问题是:imgur.com/vjWwT 我让 java 打印顶部图像,但 GUI 看起来像底部。 GUI 看起来像顶部,然后出现大圆圈,然后执行 for 循环中的下一步并重复该过程。我需要做什么才能获得底部图像?我希望这更清楚。

标签: java image jframe


【解决方案1】:

请参阅ComponentImageCapture.java 以捕获 Swing 组件的图像。该代码显然存在与 AWT 组件有关的问题。

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

【讨论】:

    【解决方案2】:

    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 服务器实例,但我不必为我的项目这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2016-08-16
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      相关资源
      最近更新 更多