java 貼圖問題

java swing 要如何貼圖不蓋掉背景?

像我這張人物行走圖

圖片參考:http://imgcld.yimg.com/8/n/AB06902877/o/1512022200...

要如何做到去背 + 不蓋掉背景

希望有範例可以參考

1 個解答

評分
  • 9 年前
    最佳解答

    import java.awt.Container;

    import javax.swing.JFrame;

    import java.awt.*;

    import java.awt.event.*;

    import javax.swing.*;

    public class Ex extends JFrame implements ActionListener{

    MP mp;//main panel

    public Ex(){

    super("MAP Test");

    //Mypanel mp

    mp = new MP();

    Container cp = getContentPane();

    cp.add(mp);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);

    setAlwaysOnTop(true);

    pack();

    }

    public void actionPerformed(ActionEvent e){

    }

    public static void main(String[] args){

    new Ex();

    }

    }

    import java.io.*;

    import java.awt.*;

    import javax.swing.*;

    import java.awt.event.*;

    import java.awt.Graphics;

    import java.awt.Image;

    import javax.swing.ImageIcon;

    public class MP extends JPanel {

    //視窗的寬和高

    private static final int WIDTH = 640;

    private static final int HEIGHT =452;

    //背景方格行和列

    private static final int ROW = 15;

    private static final int COL = 15;

    //圖

    private Image mapImage;

    private Image roleImage;

    public MP() {

    //設定面版大小和背景色

    setPreferredSize(new Dimension(WIDTH, HEIGHT));

    //載入圖

    loadImage();

    }

    public void paintComponent(Graphics g) {

    super.paintComponent(g);

    drawMap(g);

    }

    public void loadImage(){

    ImageIcon icon = new ImageIcon(getClass().getResource("map.jpg"));

    mapImage=icon.getImage();

    icon = new ImageIcon(getClass().getResource("1.png"));

    roleImage = icon.getImage();

    }

    public void drawMap(Graphics g){

    g.drawImage(mapImage,0,0,this);

    g.drawImage(roleImage,0,0,32,48,0,0,32,48,this);

    }

    }

    2012-02-22 14:12:45 補充:

    人物圖建議用PNG檔來弄,這樣也沒有透明的問題存在.

    參考資料: 自己
還有問題?馬上發問,尋求解答。