3

最终,在我计算出这个小细节之后,它会收到一个建筑物和房间号,以概述所述建筑物和房间号,因此很容易找到,但我无法让矩形准确地绘制在一个房间上。

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();


    }
}
4

1 回答 1

2

正如已经指出的那样,顶级容器不是用于执行自定义绘画的学习类,这些容器有很多功能可以使其易于绘画。

相反,为自己创建一个自定义组件,从类似的东西扩展JPanel,并覆盖它的paintComponent方法。

渲染地板窗格后,您可以在其顶部渲染自定义元素。

您如何存储这些信息取决于您,但基本上,您需要某种映射来允许您占据地板/房间并获得Shape应该呈现的内容。

因为平面图可能是浮动的(例如,它可能并不总是在 0x0 处呈现),所以您需要能够translate获得坐标以便Shape始终匹配。

看一眼...

更多细节

在此处输入图像描述

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();
        }
    }

}
于 2013-11-03T23:01:52.113 回答