简体   繁体   English

我正在尝试使用图像的坐标在imageIcon上绘制一个填充矩形,并且矩形显示为关闭

[英]I'm trying to draw a filled rectangle over an imageIcon using the coordinates of the image and the rectangle appears off

Eventually after I work out this small detail it will receive a building and room number to outline said building and room number so it is easy to locate but I can't get the rectangle to draw even close to acurately over a single room. 最后,在我计算出这个小细节之后,它将收到一个建筑物和房间号码,以概述所说的建筑物和房间号码,因此很容易找到,但我不能让矩形在一个房间内画得非常接近。

package programSTLApp;
/*
   Program to request the classroom no. in STLCC and Display the location of 
   that classroom.
 */

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

public class STLApp extends JFrame
{
    private JLabel imageLabel;
    private JButton button;
    private JPanel imagePanel;
    private JPanel buttonPanel;

public STLApp()
{
    super("My STLCC Class Locator");   

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
    buildImagePanel();
    buildButtonPanel();

    add(imagePanel, BorderLayout.CENTER);
    add(buttonPanel,BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

private void buildImagePanel()
{
    imagePanel = new JPanel();
    imageLabel = new JLabel("Click the button to see the drawing indicating "
            + "the location of your class");
    imagePanel.add(imageLabel);
}

private void buildButtonPanel()
{
    buttonPanel = new JPanel();

    button = new JButton("Get Image");

    button.addActionListener(new ButtonListener());
    buttonPanel.add(button);
}

private class ButtonListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
        ImageIcon SiteLayoutFV = new ImageIcon("D:\\B120.jpg");
        imageLabel.setIcon(SiteLayoutFV);
        imageLabel.setText(null);
        pack();
    }

}
public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.RED);
        g.fillRect(55,740,164,815);
    }


    public static void main(String[] args) 
    {
        new STLApp();


    }
}

As has already being pointed out, top level containers ain't a studiable class for performing custom painting, there is just to much going with these containers to make it easy to paint to. 正如已经指出的那样,顶级容器不是用于进行定制涂装的可分类的容器,这些容器也很容易使其易于涂漆。

Instead, create yourself a custom component, extending from something like JPanel , and override it's paintComponent method. 相反,创建一个自定义组件,从JPanel扩展,并覆盖它的paintComponent方法。

Once you have the floor pane rendered, you can render you custom elements over the top of it. 渲染了地板窗格后,可以在其顶部渲染自定义元素。

How you store this information is up to you, but basically, you need some kind of mapping that would allow you to take the floor/room and get the Shape that should be rendered. 如何存储这些信息取决于您,但基本上,您需要某种映射,以便您可以占用楼层/房间并获得应呈现的Shape

Because the floor map might float (it may not always be rendered at 0x0 for example), you need to be able to translate the coordinates so that the Shape will always match. 因为楼层地图可能会浮动(例如,它可能并不总是以0x0渲染),所以您需要能够translate坐标以使Shape始终匹配。

Take a look at... 看一眼...

For more details 更多细节

在此输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FloorPlan {

    public static void main(String[] args) {
        new FloorPlan();
    }

    public FloorPlan() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage floorPlan;

        private Rectangle myOffice = new Rectangle(150, 50, 32, 27);

        public TestPane() {
            try {
                floorPlan = ImageIO.read(new File("floorPlan.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (floorPlan != null) {

                int x = (getWidth() - floorPlan.getWidth()) / 2;
                int y = (getHeight() - floorPlan.getHeight()) / 2;
                g2d.drawImage(floorPlan, x, y, this);

                g2d.setColor(Color.RED);
                g2d.translate(x, y);
                g2d.draw(myOffice);

            }

            g2d.dispose();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM