2015-11-13 46 views
-1

我需要添加一個背景圖像到我的主框架中添加了面板。然後在我面板類的paintComponent方法中,我將繪製圖形和線條。 paintComponent將形狀繪製到面板而不是框架。 我可以使用標籤將背景圖像添加到框架,但問題是在上面沒有任何東西可見。 任何幫助表示讚賞。添加背景到主框架並從面板上的paintcomponent繪製橢圓

+0

大。你做了什麼,你卡在哪裏? –

+0

鏈接到創建背景圖像:[1](http://stackoverflow.com/questions/1064977/setting-background-images-in-jframe),[2](http://stackoverflow.com/questions/18777893/jframe-background-image),[3](http://stackoverflow.com/questions/1466240/how-to-set-an-image-as-a-background-for-frame-in-swing-gui-的-java的)。在JPanel中繪製橢圓的鏈接:[1](http://stackoverflow.com/questions/17753492/adding-oval-shape-to-jpanel),[2](http://stackoverflow.com/questions/14056610 /罐不能繪製橢圓-ON-A-的JPanel)。 –

+0

這只是我正在處理的代碼中的一小部分。所以我嘗試在paintComponent中使用ImageIcon,但它在圓圈上繪製。 – Unknown94

回答

3

您需要:

  • 首先,不使用的ImageIcon和JLabel。
  • 使用單個JPanel繪製背景圖像和形狀,並在其paintComponent方法中繪製。
  • 取而代之,使用BufferedImage,並使用g.drawImage(...)g是您的圖形對象變量)在您的JPanel的paintComponent方法中繪製該圖像。
  • 在那個相同的paintComponent方法中,繪製你的形狀,但是之後繪製圖像。
  • 創建背景圖片的鏈接:12,3
  • 鏈接到JPanel中的繪圖橢圓:12

例如,這一形象與它繪製一個橢圓形:

enter image description here

與此代碼創建:

import java.awt.BasicStroke; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Shape; 
import java.awt.Stroke; 
import java.awt.geom.Ellipse2D; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class DrawOnImage extends JPanel { 
    private static final String PATH = "https://upload.wikimedia.org/wikipedia/" 
      + "commons/thumb/c/c4/Gay_nov_20_1992_2115Z.jpg/" 
      + "581px-Gay_nov_20_1992_2115Z.jpg"; 
    private static final Color SHAPE_COLOR = new Color(150, 150, 255); 
    private static final Stroke STROKE = new BasicStroke(10f); 
    private static final double OVAL_X = 195; 
    private static final double OVAL_Y = 200; 
    private static final double OVAL_W = 200; 
    private BufferedImage img; 
    private List<Shape> shapes = new ArrayList<>(); 

    public DrawOnImage(BufferedImage img) { 
     this.img = img; 
     shapes.add(new Ellipse2D.Double(OVAL_X, OVAL_Y, OVAL_W, OVAL_W)); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (img != null) { 
      g.drawImage(img, 0, 0, this); 
     } 
     Graphics2D g2 = (Graphics2D) g; 
     g2.setStroke(STROKE); 
     g2.setColor(SHAPE_COLOR); 
     for (Shape shape : shapes) { 
      g2.draw(shape); 
     } 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet() || img == null) { 
      return super.getPreferredSize(); 
     } 
     int w = img.getWidth(); 
     int h = img.getHeight(); 
     return new Dimension(w, h); 
    } 

    private static void createAndShowGui() { 
     BufferedImage img = null; 

     try { 
      img = ImageIO.read(new URL(PATH)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

     JFrame frame = new JFrame("Draw On Image"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new DrawOnImage(img)); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 
+0

@AKANKSHABANSAL:請參閱更新以回答包括代碼。也請不要在評論中張貼代碼。相反,如果您需要顯示代碼,請編輯您的問題並在其中顯示您的代碼。再次,**不要使用ImageIcon和JLabel **。我無法強調這一點。 –