【问题标题】:How to set an image as the background of a JPanel [duplicate]如何将图像设置为JPanel的背景[重复]
【发布时间】:2016-08-10 16:48:24
【问题描述】:

我正在开发一个作为棋盘游戏的 Java 程序。我需要帮助将图像分配给 JPanel 的背景。我现在只有一个可以点击和拖动的移动棋子(棋盘棋子)。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShapeMover {

public ShapeMover() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Shape Mover");

    initComponents(frame);

    frame.pack();
    frame.setVisible(true);
}

public static void main(String s[]) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new ShapeMover();
        }
    });
}

private void initComponents(JFrame frame) {
    frame.getContentPane().add(new DragPanel());
}
}



class DragPanel extends JPanel {

Rectangle rect = new Rectangle(0, 0, 20, 20);
int preX, preY;
boolean isFirstTime = true;
Rectangle area;
boolean pressOut = false;
private Dimension dim = new Dimension(800, 666);

public DragPanel() {
    addMouseMotionListener(new MyMouseAdapter());
    addMouseListener(new MyMouseAdapter());
}

@Override
public Dimension getPreferredSize() {
    return dim;
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    if (isFirstTime) {
        area = new Rectangle(dim);
        rect.setLocation(50, 50);
        isFirstTime = false;
    }

    g2d.setColor(Color.black);
    g2d.fill(rect);
}

boolean checkRect() {
    if (area == null) {
        return false;
    }

    if (area.contains(rect.x, rect.y, rect.getWidth(), rect.getHeight())) {
        return true;
    }

    int new_x = rect.x;
    int new_y = rect.y;

    if ((rect.x + rect.getWidth()) > area.getWidth()) {
        new_x = (int) area.getWidth() - (int) (rect.getWidth() - 1);
    }
    if (rect.x < 0) {
        new_x = -1;
    }
    if ((rect.y + rect.getHeight()) > area.getHeight()) {
        new_y = (int) area.getHeight() - (int) (rect.getHeight() - 1);
    }
    if (rect.y < 0) {
        new_y = -1;
    }
    rect.setLocation(new_x, new_y);
    return false;
}

private class MyMouseAdapter extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
        preX = rect.x - e.getX();
        preY = rect.y - e.getY();

        if (rect.contains(e.getX(), e.getY())) {
            updateLocation(e);
        } else {
            pressOut = true;
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (!pressOut) {
            updateLocation(e);
        } else {
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (rect.contains(e.getX(), e.getY())) {
            updateLocation(e);
        } else {
            pressOut = false;
        }
    }

    public void updateLocation(MouseEvent e) {
        rect.setLocation(preX + e.getX(), preY + e.getY());
        checkRect();

        repaint();
    }
}
}

【问题讨论】:

标签: java swing


【解决方案1】:

创建一个新的 JLabel 将其添加到您希望背景图像出现的框架中。 像这样的:

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Faisal\\Desktop\\New folder\\picture-1.thumbnail.png"));
    lblNewLabel.setBounds(0, 0, 515, 367);
    frame1.add(lblNewLabel);

这是我项目中的代码。

【讨论】:

【解决方案2】:

在 JPanel 上设置背景的正确方法是创建 JPanel 子类并覆盖paintComponent

public ImagePanel extends JPanel {
  Image im;
  public ImagePanel(Image im) {
    this.im = im;
  }

  void paintComponent(Graphics g) {
    g.drawImage(im,0,0, getWidth(), getHeight(), this);
    super.paintComponent(g);
  }
}

【讨论】:

  • 来吧,你现在应该知道了,paintComponent 不应该是public,你应该调用super.paintComponent,你应该通过this 作为ImageObserver (特别是对于Image),这将根据其比例“缩放”图像(恕我直言),这将使它看起来......不愉快
猜你喜欢
  • 2014-01-19
  • 1970-01-01
  • 2014-05-07
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-14
  • 2012-04-09
相关资源
最近更新 更多