添加背景到主框架并从面板

时间:2015-11-13 16:46:11

标签: java swing graphics

我需要在我的主框架中添加一个背景图像,其中添加了面板。然后在我的面板类的paintComponent方法中,我将绘制椭圆和线条。 paintComponent将形状绘制到面板而不是框架。 我可以使用标签将背景图像添加到框架,但问题是顶部没有任何东西可见。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

你需要:

  • 首先,不要使用ImageIcon和JLabel。
  • 使用单个JPanel绘制背景图像和形状,并在其paintComponent方法中绘制。
  • 而是使用BufferedImage,并使用g.drawImage(...)g作为Graphics对象变量)在JPanel的paintComponent方法中绘制该图像。
  • 使用相同的paintComponent方法绘制形状,但 绘制图像后。
  • 创建背景图片的链接:123
  • 在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();
            }
        });
    }
}