2013-11-03 70 views
2

最後,在我制定出這個小細節後,它將收到一個建築和房間號,以概述所述建築物和房間號它很容易找到,但我無法讓矩形在單個房間內甚至接近完全地繪製。我試圖使用圖像的座標在圖像座標上繪製一個填充的矩形,並且矩形出現關閉

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


    } 
} 
+2

第一最重要的是,你幾乎永遠都不想直接在JFrame中或在繪製方法中繪製。請查看教程[使用Swing執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html)。 –

回答

1

正如已經被指出的那樣,頂層容器不能用於執行風俗畫一個studiable類中,只有與這些容器多的事情,可以很容易使塗料。

取而代之,創建一個自定義組件,從JPanel之類的東西延伸,並覆蓋它的paintComponent方法。

一旦您渲染了底板窗格,您就可以將自定義元素渲染到頂層。

您如何存儲這些信息取決於您,但基本上,您需要某種映射來讓您佔據地板/房間並獲取應呈現的Shape

因爲樓層地圖可能會浮動(例如,它可能不總是呈現在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(); 
     } 
    } 

} 
+0

非常感謝您的回答,我也遇到了同樣的問題,當我試圖繪製一張圖片時,我不得不將其設置爲JLabel背景,並將圖片放在圖片後面。使用這種解決方案,我不再需要JLabel了,我將在'paintComponent'中繪製圖像和顏料,非常感謝。 – TiMr