首页 > 解决方案 > 在Java中的图像上绘制一个矩形

问题描述

我正在尝试使用 java.awt 类在 Image 上绘制一个矩形。为此,我使用了以下示例代码:

public class DrawRectangle {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestPane());
        frame.pack();
        frame.setVisible(true);
      }
    });
  }
}

class TestPane extends JPanel {
  private BufferedImage myImage;
  private Rectangle myOffice = new Rectangle(150, 50, 30, 20);
  public TestPane() {
    try {
      File image = new File("C:\\Users\\NNaphade\\work\\ImageDetection\\Trial_Pascal_VOC\\test_image\\IMG_20180327_110210.jpg");
      if(image.exists())
          myImage = ImageIO.read(image);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  @Override
  public Dimension getPreferredSize() {
    System.out.println("image exist!!!!!!");
    return myImage == null ? new Dimension(200, 200) : new Dimension(
        myImage.getWidth(), myImage.getHeight());
  }

  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    if (myImage != null) {
      g2d.drawImage(myImage, 0, 0, 1000, 1000, this);
      g2d.setColor(Color.RED);
      g2d.translate(0, 0);
      g2d.draw(myOffice);
    }
    g2d.dispose();
  }
}

这工作正常,输出按预期显示。在这里,我将矩形的参数固定为:

private Rectangle myOffice = new Rectangle(150, 50, 30, 20);

但是,在我的应用程序中,我想从另一个方法传递这些参数。我想将这些 x1、y1、w 和 h 传递给上面给出的 TestPane 类。我尝试通过传递这 4 个参数来更改 TestPane 构造函数,但我无法将它们设置为实例变量。例如,以下代码不起作用。

private void markWithBoundingBox(INDArray testData, int gridWidth, int gridHeight, double w, double h, DetectedObject obj) {

    double[] xy1 = obj.getTopLeftXY();      
    int predictedClass = obj.getPredictedClass();
    int x1 = (int) Math.round(w * xy1[0] / gridWidth);
    int y1 = (int) Math.round(h * xy1[1] / gridHeight);

    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TestPane(x1, y1, w, h));
        frame.pack();
        frame.setVisible(true);
      }
    });
}   

class TestPane extends JPanel {
      private BufferedImage myImage;
      //private Rectangle myOffice = new Rectangle(50, 50, 3, 20);
      public TestPane(int x, int y, double w, double h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        try {
          File file = new File("C:\\Users\\NNaphade\\work\\ImageDetection\\Trial_Pascal_VOC\\test_image\\IMG_20180327_110210.jpg");
          if(file.exists()) {
              myImage = ImageIO.read(file);
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }

    @Override
      public Dimension getPreferredSize() {
        return myImage == null ? new Dimension(100, 100) : new Dimension(
            myImage.getWidth(), myImage.getHeight());
      }

      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        if (myImage != null) {
          g2d.drawImage(myImage, 0, 0, 2000, 2000, this);
          g2d.setColor(Color.RED);
          g2d.translate(0, 0);
          g2d.draw(new Rectangle(this.x, this.y, this.w, this.h));
        }
        g2d.dispose();
      }
    }

在我看来,TestPane这里不是一个类,而是组件。因为Java编译器不允许我在构造函数中声明实例变量以及组件的所有可用方法。我怎样才能摆脱这个问题?

标签: javagraphicsawtdrawingrectangles

解决方案


推荐阅读