简体   繁体   English

Image over Image Java

[英]Image Over Image Java

Basically in this code the problem is that when I run it, the character named Aaron does not show up. 基本上,在这段代码中,问题是当我运行它时,名为Aaron的字符不会显示。 Only his user name does. 只有他的用户名。

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();
    }
}

Options: 选项:

  • You can use JLayeredPanes and overlap the text over the image 您可以使用JLayeredPanes并将文本覆盖在图像上
  • For PNG images transparency might be the answer 对于PNG图片,透明度可能是答案

Try these, try to get back to me if it does not work. 试试这些,如果不起作用,请尝试与我联系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM