当我告诉它时,为什么我的图像不会改变?

时间:2012-02-27 11:02:49

标签: java image replace grid paint

我对编程很陌生,请理解很多这些代码可能不是“最佳”,但我正在学习。

我的程序是这样的:

15x15的网格是基于字符串创建的32pxx32px正方形。

b是黑色块,1是白色块,r是红色块。

当我点击时,我应该能够用鼠标将鼠标悬停在红色上。这不会发生。

我已尝试过所有内容,即使删除当前块,创建一个默认为红色的全新块,它仍然无效!

提前非常感谢你。我已经在这个问题上敲了几个小时。

必要文件的SSCCE:

import javax.swing.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.awt.Rectangle;

import javax.swing.JFrame;

public class main extends JFrame {
    game game;
    public main(){
        add(new game());
        setTitle("Grid");
        setSize(487,509);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    }

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


import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

public abstract class entity {
    private int xpos,ypos,gridxpos,gridypos;
    private Image img;
    private String Img="sprites/floorw.gif";

    public void setXPos(int x){
        this.xpos=x;
    }

    public void setYPos(int y){
        this.ypos=y;
    }

    public int getXPos(){
        return xpos;
    }

    public int getYPos(){
        return ypos;
    }

    public void setGrid(int x, int y){
        this.gridxpos=x;
        this.gridypos=y;
    }

    public int getGridX(){
        return gridxpos;
    }

    public int getGridY(){
        return gridypos;
    }

    public void setSprite(Image img){
        this.img=img;
    }

     public Rectangle getBounds() {
         return new Rectangle(this.xpos, this.ypos, this.img.getHeight(null), this.img.getWidth(null));
     }

    public void setImg(char a){
        if (a=='b'){
            Img="sprites/floorb.gif";
        }else if(a=='1'){
            Img="sprites/floorw.gif";
        }else if(a=='r'){
            Img="sprites/floorr.gif";
        }
    }
    public String getImg(){
        return Img;
    }

    public void draw(Graphics g){   
        g.drawImage(this.img,this.xpos,this.ypos,null);
    }
}


import javax.swing.ImageIcon;

public class floor extends entity {
    ImageIcon image;
    public String floorImg;

    public floor(char a,boolean w,int x,int y){

        if (a=='b'){
            setImg('b');
        }else if(a=='1'){
            setImg('1');
        }else if(a=='r'){
            setImg('r');
        }

        setGrid(x,y);

        this.setXPos(x*32);
        this.setYPos(y*32);
        floorImg=getImg();
        image = new ImageIcon(this.getClass().getResource(floorImg));

        setSprite(image.getImage());
    }
}


public class game extends JPanel{

    private String map;
    private static ArrayList<entity> tiles = new ArrayList<entity>();
    private static ArrayList<entity> kill = new ArrayList<entity>();
    private floor floor;
    private player player;

    public game(){

        setFocusable(true);
        addMouseListener(new Listener());
        init();
    }

    public void paint(Graphics g){
        super.paint(g);

        Graphics2D g2d = (Graphics2D) g;

        tiles.removeAll(kill);
        kill.clear();

        for(int i = 0; i<tiles.size();i++){
            entity tile = (entity) tiles.get(i);

            tile.draw(g);
        }
    }

    public void addObj(ArrayList a, entity b){
        a.add(b);
    }
    public void killObj(entity b){
        kill.add(b);
    }

    public void init(){
        map = "bbbbbb11bbbbbbb\n"+
              "bbbb111111bbbbb\n"+
              "bbbb11bb11bbbbb\n"+
              "bbbb11bb11bbbbb\n"+
              "bbbb11bb11bbbbb\n"+
              "bbbb11bbbbbbbbb\n"+
              "111111bb1111111\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb11bb11bb1bb\n"+
              "bbbb111111111bb\n"+
              "bbbbbb11bbbbbbb\n";

        int col,row;
        col=0;
        row=0;
        char space;     
        for(int i = 0; i<map.length();i++){
            space = map.charAt(i);

            if(space=='\n'){
                row++;
                col=0;  
            }else if(space=='b'){
                floor = new floor('b',false,col,row);
                tiles.add(floor);
                col++;
            }else if(space=='1'){
                floor = new floor('w',true,col,row);
                tiles.add(floor);
                col++;
            }else if(space=='r'){
                floor = new floor('r',true,col,row);
                tiles.add(floor);
                col++;
            }   
        }
        repaint();
    }

    public boolean collidesWith(Rectangle r, Rectangle r2){ 
     return r.intersects(r2);
    }

    public class Listener implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            Rectangle mouse = new Rectangle(e.getX(), e.getY(), 1, 1);
            for(int i=0;i<tiles.size();i++){
                if(collidesWith(mouse.getBounds(), tiles.get(i).getBounds())){
                    tiles.get(i).setImg('r');
                }       
            }
            repaint();
        }

        public void mouseEntered(MouseEvent e){
        }   
        public void mouseExited(MouseEvent e) {
        }
        public void mousePressed(MouseEvent e) {    
        }
        public void mouseReleased(MouseEvent e) {
        }

        }

    }

0 个答案:

没有答案