imagepanel.java

来自「windows自带的扫雷游戏」· Java 代码 · 共 58 行

JAVA
58
字号
package mine.view;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class ImagePanel
    extends JPanel {
    private Point2D.Double position;
    private ImageIcon imageIcon;
    public ImagePanel() {
        init();
    }

    public ImagePanel(ImageIcon imageIcon) {
        init();
        initImage(imageIcon);
    }

    private void initImage(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
        Image image = imageIcon.getImage();
        setSize(
            image.getWidth(this), image.getHeight(this));
        repaint();
    }

    private void init() {
        this.setLayout(null);
        this.setDoubleBuffered(true);
        setOpaque(false);
        //               setOpaque(true);
        position = new Point2D.Double(0, 0);
        setLocation(0, 0);

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (imageIcon != null) {
            imageIcon.paintIcon(this, g, 0, 0);
        }
    }

    public void setIcon(ImageIcon imageIcon) {
        initImage(imageIcon);
    }

    public void setPosition(double x, double y) {
        position.setLocation(x, y);
        setLocation( (int) x, (int) y);
    }

    public Point2D.Double getPosition() {
        return position;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?