gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:19 25 4
gpt4 key购买 nike

最终,在我解决了这个小细节之后,它将收到一个建筑物和房间号来勾勒出所述建筑物和房间号,因此很容易找到,但我无法在一个房间上精确地绘制矩形.

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


}
}

最佳答案

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

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

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

如何存储此信息由您决定,但基本上,您需要某种映射,让您可以发言/发言并获得 Shape应该被渲染。

因为楼层 map 可能会 float (例如,它可能并不总是在 0x0 处呈现),您需要能够 translate坐标使得 Shape将始终匹配。

看看...

更多详情

enter image description here

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

}

关于java - 我正在尝试使用图像的坐标在 imageIcon 上绘制一个填充的矩形,并且该矩形显示为关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com