用Java在图像上绘制一个矩形

时间:2018-10-26 14:34:14

标签: java graphics awt drawing rectangles

我正在尝试使用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编译器不允许我在构造函数中声明实例变量,并且所有可用的方法都包含组件。我该如何摆脱这个问题?

0 个答案:

没有答案