Image Over Image

时间:2013-07-22 20:48:20

标签: java image swing

基本上在这段代码中,问题是当我运行它时,名为Aaron的角色不会出现。只有他的用户名呢。

package Java;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class javagame extends JFrame {

    private Image dbImage;
    private Graphics dbg;
    Image face;
    Image backround;

    //Briggs
    int xbn, ybn;
    int xo, yo;

    //Aaron
    int x, y;
    int xan, yan;

    Font font = new Font ("Arial", Font.BOLD, 20);

    public class AL extends KeyAdapter {

        public void keyPressed (KeyEvent e) {

            int keyCode = e.getKeyCode();
            if(keyCode == e.VK_LEFT) {

                    x+= -10;
                    xan+= -10;
            }
            if(keyCode == e.VK_A) {
                    xo+= -10;
                    xbn+= -10;
            }
            if(keyCode == e.VK_RIGHT) {
                    x += +10;
                    xan+= +10;
            }
            if(keyCode == e.VK_D) {
                    xo += +10;
                    xbn += +10;
            }
            if(keyCode == e.VK_UP) {
                    y += -10;
                    yan += -10;
            }
            if(keyCode == e.VK_W) {
                    yo += -10;
                    ybn += -10;
            }
            if(keyCode == e.VK_DOWN) {
                    y += +10;
                    yan += +10;
            }
            if(keyCode == e.VK_S) {
                    yo += +10;
                    ybn += +10;
            }
        }
        public void keyReleased (KeyEvent e) {
        }
    }

    public javagame() {
        //load Images
        ImageIcon ia = new ImageIcon("C:/Users/Douger/Desktop/eclipse/Java Game/src/Java/Square buddy.png");
        face = ia.getImage();

        ImageIcon iback = new ImageIcon("C:/Users/Douger/Desktop/eclipse/Java Game/src/Java/a javagame backround 1.png");
        backround = iback.getImage();

        //Game properties
        addKeyListener(new AL());
        setTitle("A Ball's Adventure");
        setSize(750, 750);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.green);
        setLocationRelativeTo(null);

        //Aaron
        x = 250;
        y = 250;

        xan = 230;
        yan = 250;

        //Briggs

        xo = 300;
        yo = 300;

        xbn = 280;
        ybn = 300;
    }

    public void paint(Graphics g) {
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        paintComponent (dbg);
        g.drawImage(dbImage, 0, 0, this);

    }

    public void paintComponent (Graphics g){
        g.drawImage(backround, 0, 0, this.getWidth(), this.getHeight(), this);
        g.setColor(Color.white);
        g.setFont(font);

        g.drawString("Copy Right All rights reserved to Aaron Collins 2013-2013", 100, 100);

        g.drawLine(100, 105, 640, 105);

        g.setColor(Color.blue);
        g.fillRect(xo, yo, 10, 10);

        g.setColor(Color.black);
        g.drawString("Aaron", xan, yan);

        g.setColor(Color.black);
        g.drawString("Briggs", xbn, ybn);

        //g.drawImage(face, x, y, 50, 50, this);
        g.setColor(Color.blue);
        g.fillRect(x, y, 10, 10);

        repaint();
    }

    public static void main(String[] args) {
        new javagame();
    }
}

1 个答案:

答案 0 :(得分:0)

选项:

  • 您可以使用JLayeredPanes并在图片上重叠文字
  • 对于PNG图像,透明度可能就是答案

尝试这些,如果不起作用,请尽量回复我。