【问题标题】:How to use command arguments in paintComponent method?如何在paintComponent 方法中使用命令参数?
【发布时间】:2014-11-17 01:39:37
【问题描述】:

我目前正在尝试创建一个 Java GUI 程序,该程序根据终端中给出的参数生成图像。例如,如果我在命令行java Draw Image1 中获得参数,我想为其他人绘制我的图像 1 等。如何获取命令参数并在paintComponent 中使用它?这是我在下面尝试做的一个示例:

import javax.swing.*;
import java.awt.*;

public class Draw extends JPanel
{

    public Draw()
    {
        this.setSize(800,800);
        JPanel drawing = new JPanel();
        this.add(drawing);
        this.setVisible(true);
    }

    protected void paintComponent(Graphics g)
    {
        if (args[0].equals("Image1")) // won't work
        {
            super.paintComponent(g);
            Image myImage = Toolkit.getDefaultToolkit().getImage("image/myimage.jpg");
            g.drawImage(myImage, 0, 0, this);
        }
        else
        {
            // draw image 2
        }
    }

    public static void main(String[] args)
    {       
        // create new Jframe
        JFrame frame = new JFrame("Draw");
        frame.add(new Draw());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);
        frame.setVisible(true);
    }
}

【问题讨论】:

  • Image myImage = Toolkit.getDefaultToolkit().getImage("image/myimage.jpg"); 永远不要尝试在绘制方法中加载资源。该图像可以在构造类时加载,然后存储为类属性(可在绘画中访问)。

标签: java swing user-interface awt command-line-arguments


【解决方案1】:

改变这个:

frame.add(new Draw());

到这里:

frame.add(new Draw(args));

然后让你的构造函数接受一个字符串数组参数并用它来设置一个类字段。

public class Draw extends JPanel
{
    private String[] params

    public Draw(String params)
    {
        this.params = params;
        this.setSize(800,800);  // this generally should be avoided
        JPanel drawing = new JPanel();
        this.add(drawing);
        this.setVisible(true);
    }

    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);  // this should be out of the if block
        if (params != null && params.length > 0 && params[0].equals("Image1")) {
          // ..... etc

编辑:安德鲁是对的,我没有仔细阅读您的代码。在构造函数中读取你的图像,并在paintCompnent方法的图像中使用它


例如,

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@SuppressWarnings("serial")
public class Draw extends JPanel {

   private static final int PREF_W = 800;
   private static final int PREF_H = PREF_W;
   private BufferedImage image;

   public Draw(String[] params) throws IOException {
      if (params != null && params.length > 0) {
         image = ImageIO.read(new File(params[0]));
      }
      // this.setSize(800,800);
      JPanel drawing = new JPanel();
      drawing.setOpaque(false); // may need this
      this.add(drawing);
      this.setVisible(true);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      } else {

      }

   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }

      // not sure if you want to size it to the image or not
      if (image != null) {
         int w = image.getWidth();
         int h = image.getHeight();
         return new Dimension(w, h);
      } else {
         return new Dimension(PREF_W, PREF_H);
      }
   }

   public static void main(String[] args) {
      // create new Jframe
      JFrame frame = new JFrame("Draw");
      try {
         frame.add(new Draw(args));
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         // frame.setSize(500,500);
         frame.pack();
         frame.setVisible(true);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

【讨论】:

  • 现在我已经仔细阅读了,我认为Draw 面板应该只接受Image[],因为如果它需要图像(或两个) 给它图像而不是代表图像路径的字符串。但是 +1 让它走到这一步。
  • @AndrewThompson:是的,你可能是对的,但这只是如何将信息从静态世界传递到实例世界这一更大问题的一部分。
  • 是的。我忘了补充那部分只是我个人的“设计不满”,甚至在 J2SE 中都没有遵守。 :P
猜你喜欢
  • 2020-08-17
  • 1970-01-01
  • 2020-08-20
  • 2014-05-23
  • 2021-06-21
  • 2018-10-08
  • 2021-08-21
  • 2012-11-30
  • 2013-03-04
相关资源
最近更新 更多