有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的精灵不显示背景

我有一个问题,我的精灵不是显示背景图像,而是显示帧的背景颜色

这是我的密码:

MAIN(经过编辑,因此我只显示加载精灵的部分)

private JFrame principal;
private JButton bt_inicio_iniciar;
private JButton bt_menu_1;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable(){
        public void run()
        {
            try
            {
                // UIManager.setLookAndFeel(new SyntheticaBlackEyeLookAndFeel());
                new Thread (new ClientMain()).start();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    });
}


public ClientMain() throws IOException {
    img = ImageIO.read(new File("Resources\\images\\background\\background.png"));
    loadGUI();

}
private void loadGUI()
{
    int principal_width = 1200;
    int principal_heigth = 500;
    principal = new JFrame();

//        principal.setBackground(new Color(0,0,0,0));
    JLabel contentPane = new JLabel();
    contentPane.setIcon( new ImageIcon("Resources\\images\\background\\background.png") );
    contentPane.setLayout( new BorderLayout() );
    principal.pack();
    principal.setContentPane( contentPane );
    principal.setResizable(false);
    principal.setLocationRelativeTo(null);
    principal.setTitle("Cliente ");
    principal.setSize(principal_width, principal_heigth);
    //principal.setContentPane(new JLabel(new ImageIcon("Resources\\images\\background\\background.png")));
    principal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    principal.getContentPane().setLayout(null);

    //inicio
    bt_inicio_iniciar = new JButton();
    bt_inicio_iniciar.setBounds(500,200,200,50);
    bt_inicio_iniciar.setText("Iniciar");
    bt_inicio_iniciar.setEnabled(false);

    tx_inicio_estado = new JLabel();
    tx_inicio_estado.setText("Esperando al servidor");
    tx_inicio_estado.setForeground(Color.WHITE);
    tx_inicio_estado.setSize(200,20);
    tx_inicio_estado.setLocation(500,175);

    //menu
    bt_menu_1 = new JButton();
    bt_menu_1.setBounds(10,350,150,50);
    bt_menu_1.setText("Attack");
    bt_menu_1.setBackground(Color.LIGHT_GRAY);
    bt_menu_1.setEnabled(true);

    bt_menu_2 = new JButton();
    bt_menu_2.setBounds(170,350,150,50);
    bt_menu_2.setText("Spells");
    bt_menu_2.setBackground(Color.LIGHT_GRAY);
    bt_menu_2.setEnabled(true);

    bt_menu_3 = new JButton();
    bt_menu_3.setBounds(10,410,150,50);
    bt_menu_3.setText("Items");
    bt_menu_3.setBackground(Color.LIGHT_GRAY);
    bt_menu_3.setEnabled(true);

    bt_menu_4 = new JButton();
    bt_menu_4.setBounds(170,410,150,50);
    bt_menu_4.setText("Defend");
    bt_menu_4.setBackground(Color.LIGHT_GRAY);
    bt_menu_4.setEnabled(true);

    //battleLog
    tx_log = new JLabel();
    tx_log.setText("Battle Log");
    tx_log.setBounds(330,350,150,15);

    log = new TextArea();
    log.setBounds(330,370,400,89);
    log.setBackground(Color.lightGray);
    log.setForeground(Color.white);

    //Hp

    tx_hp_player1 = new JLabel();
    tx_hp_player1.setBounds(740,350,150,15);
    tx_hp_player1.setText("Player1");

    pb_hp_player1 = new JProgressBar();
    pb_hp_player1.setBounds(740,365,445,15);
    pb_hp_player1.setMaximum(100);
    pb_hp_player1.setMinimum(0);
    pb_hp_player1.setValue(100);
    pb_hp_player1.setIndeterminate(false);
    pb_hp_player1.setStringPainted(true);
    pb_hp_player1.setForeground(Color.red);
    pb_hp_player1.setString(pb_hp_player1.getValue()+"/"+pb_hp_player1.getMaximum());

    tx_hp_player2 = new JLabel();
    tx_hp_player2.setBounds(740,385,150,15);
    tx_hp_player2.setText("Player2");

    pb_hp_player2 = new JProgressBar();
    pb_hp_player2.setBounds(740,400,445,15);
    pb_hp_player2.setMaximum(100);
    pb_hp_player2.setMinimum(0);
    pb_hp_player2.setValue(100);
    pb_hp_player2.setIndeterminate(false);
    pb_hp_player2.setStringPainted(true);
    pb_hp_player2.setForeground(Color.red);
       pb_hp_player2.setString(pb_hp_player2.getValue()+"/"+pb_hp_player2.getMaximum());

    tx_hp_boss = new JLabel();
    tx_hp_boss.setBounds(740,420,150,15);
    tx_hp_boss.setText("Boss");

    pb_hp_boss = new JProgressBar();
    pb_hp_boss.setBounds(740,435,445,15);
    pb_hp_boss.setMaximum(200);
    pb_hp_boss.setMinimum(0);
    pb_hp_boss.setValue(200);
    pb_hp_boss.setIndeterminate(false);
    pb_hp_boss.setStringPainted(true);
    pb_hp_boss.setForeground(Color.red);
    pb_hp_boss.setString(pb_hp_boss.getValue()+"/"+pb_hp_boss.getMaximum());


    p = new Player();
    //p.setBounds(100,100,30,35);
    //p.setOpaque(true);

    principal.add(bt_inicio_iniciar);
    principal.add(bt_menu_1);
    principal.add(bt_menu_2);
    principal.add(bt_menu_3);
    principal.add(bt_menu_4);
    principal.add(tx_log);
    principal.add(log);
    principal.add(tx_hp_player1);
    principal.add(pb_hp_player1);
    principal.add(tx_hp_player2);
    principal.add(pb_hp_player2);
    principal.add(tx_hp_boss);
    principal.add(pb_hp_boss);
    principal.add(tx_inicio_estado);
    principal.add(p);

    principal.setVisible(true);



}

}

玩家

public class Player extends JPanel {

// Screen Dimensions
int 
   GWIDTH = 30,
   GHEIGHT = 30;

Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);

// Double Buffer Varabiles

Image dbImage;
Graphics dbg;

// buffered spritesheet
Image firstBall;
SpriteLoader ball = new   SpriteLoader("Resources\\images\\sprites\\sprite1.png");
// Animator     
Animation ballAnimation;
ArrayList animationArray;


public Player() {
    this.setSize(screenSize);
    init();



}

 private void init(){
    animationArray = new ArrayList<BufferedImage>();

    animationArray.add(0,ball.getImage(0,64,32,32));
    animationArray.add(1,ball.getImage(32,64,32,32));
    animationArray.add(2,ball.getImage(64,64,32,32));


    ballAnimation = new Animation(animationArray);
    ballAnimation.setSpeed(100);
    ballAnimation.start();

 }
@Override
public void paint(Graphics g){
    super.paint(g);
    dbImage = createImage(getWidth(),getHeight());
    dbg = dbImage.getGraphics();
    draw(dbg);
    g.drawImage(dbImage, 0, 0, this);
    g.dispose();
}


public void draw(Graphics g){
    if(ballAnimation != null){
         g.drawImage(ballAnimation.sprites, 0, 0, this);
        ballAnimation.update(System.currentTimeMillis());
       repaint();
    }
    g.dispose();


}


public static void main(String[] args) {
    Player m = new Player();
 }
}

动画

public class Animation {

// initalize values
private ArrayList<BufferedImage> frames;

public BufferedImage sprites;

// Animation Varabales
private volatile boolean running = false;
private long previousTime, speed;
private int frameAtPause, currentFrame;

public Animation(ArrayList<BufferedImage> frames){
        this.frames = frames;
}

public void setSpeed(long speed){
    this.speed = speed;
}

public void update(long time){
    if(running){
        if(time - previousTime >= speed){
            //Update the animation
            currentFrame++;
           try{

               sprites = frames.get(currentFrame);
           } catch(IndexOutOfBoundsException e){
               currentFrame = 0;
               sprites = frames.get(currentFrame);

           }
           previousTime = time;
        }
    }
}// end update

public void start (){
    running = true;
    previousTime = 0;
    frameAtPause = 0;
    currentFrame = 0;
}
public void stop (){
    running = false;
    running = true;
    previousTime = 0;
    frameAtPause = 0;
    currentFrame = 0;
}
public void pause (){
    frameAtPause = currentFrame;
    running = false;
}
public void restart (){
    currentFrame = frameAtPause;
    running = true;
}
}

喷洒器

public class SpriteLoader {

//Initialize values
private BufferedImage bImage = null,bImage2 = null;






public SpriteLoader(String location){

    try{
        bImage = ImageIO.read(new File(location));

    }catch(IOException e){

    }


}


public BufferedImage getImage (int x, int y, int w, int h){
    bImage2 = bImage.getSubimage(x, y , w, h);
    return bImage2;
}

}

这是我用的雪碧

enter image description here

背景呢

enter image description here

到目前为止,我尝试着做我能在stackoverflow上找到的所有事情,但我找不到任何有助于解决问题的东西:S

EDIT1:编辑了主类,使其仅包含GUI部分,但我不知道是否可以清理其他类,因为我不知道错误可能在哪里:S

EDIT2:清除未使用的方法


共 (0) 个答案