-1
我需要添加一個背景圖像到我的主框架中添加了面板。然後在我面板類的paintComponent方法中,我將繪製圖形和線條。 paintComponent將形狀繪製到面板而不是框架。 我可以使用標籤將背景圖像添加到框架,但問題是在上面沒有任何東西可見。 任何幫助表示讚賞。添加背景到主框架並從面板上的paintcomponent繪製橢圓
我需要添加一個背景圖像到我的主框架中添加了面板。然後在我面板類的paintComponent方法中,我將繪製圖形和線條。 paintComponent將形狀繪製到面板而不是框架。 我可以使用標籤將背景圖像添加到框架,但問題是在上面沒有任何東西可見。 任何幫助表示讚賞。添加背景到主框架並從面板上的paintcomponent繪製橢圓
您需要:
paintComponent
方法中繪製。g.drawImage(...)
(g
是您的圖形對象變量)在您的JPanel的paintComponent方法中繪製該圖像。paintComponent
方法中,繪製你的形狀,但是之後繪製圖像。例如,這一形象與它繪製一個橢圓形:
與此代碼創建:
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();
}
});
}
}
@AKANKSHABANSAL:請參閱更新以回答包括代碼。也請不要在評論中張貼代碼。相反,如果您需要顯示代碼,請編輯您的問題並在其中顯示您的代碼。再次,**不要使用ImageIcon和JLabel **。我無法強調這一點。 –
大。你做了什麼,你卡在哪裏? –
鏈接到創建背景圖像:[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)。 –
這只是我正在處理的代碼中的一小部分。所以我嘗試在paintComponent中使用ImageIcon,但它在圓圈上繪製。 – Unknown94