1

I have problem with repaint function, i have array with numbers describing what image shoud be draw. When my character moving the number in array is change and I want to repaint whole map with the changed element, but nothing is happend.

    player = new Player(2,7);

            this.addKeyListener(new KeyListener() {

                @Override
                public void keyTyped(KeyEvent e) {          
                }
                @Override
                public void keyReleased(KeyEvent e) {
                }
                @Override
                public void keyPressed(KeyEvent e) {
                    Point playerposition = player.returnPosition();
                    switch(e.getKeyCode()){

                    case KeyEvent.VK_LEFT:
                        player.moveHorizontal(-1);  
                        if(playerposition.x<=0)
                            player.moveHorizontal(1);

                        break;
                    case KeyEvent.VK_RIGHT:
                        player.moveHorizontal(1);
                        arr[15][15]=-1;
                        System.out.println(arr[15][15]);
                        if(playerposition.x>=960)
                            player.moveHorizontal(-1);
                        break;
                    case KeyEvent.VK_UP:
                        player.moveVertical(-1);
                        if(playerposition.y<=30)
                        player.moveVertical(1);
                        break;
                    case KeyEvent.VK_DOWN:
                        player.moveVertical(1);
                        if(playerposition.y>=690)
                            player.moveVertical(-1);
                        break;

                    }   
                    repaint();
                }
});
            Thread t = new Thread()
            {
            @Override
            public void run()
            {
            while(true)
            {

            try {
            Thread.sleep(1000 / 25);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }}}
            };
            t.start();
}
void paintBackgorund(Graphics gr)
{ImageIcon background = new ImageIcon ("lib/jar/tlo.PNG");
Image backgroundpic;
backgroundpic = background.getImage();
gr.drawImage(backgroundpic, 0, 0, 990, 720, null);
}
@Override
public void paint(Graphics g)
{       
    Point playerposition = player.returnPosition();// testy
    System.out.println("x:"+playerposition.x);
    System.out.println("y:"+playerposition.y);
    for (int i=0 ; i <33 ; i++)
    {
        for(int j=1 ; j<24 ; j++)
        {
            if(arr[i][j] == 1)
                g.drawImage(((Grass)(objects.get(0))).objpic, (i*30), (j*30), 30, 30, null);

            else if(arr[i][j] == 2)
                g.drawImage(((Stone)(objects.get(1))).objpic, (i*30), (j*30), 30, 30, null);

            else if(arr[i][j] == 3)
                g.drawImage(((Diamond)(objects.get(2))).objpic, (i*30), (j*30),30,30, null);                
        }}
    g.drawImage(((Playerobj)(objects.get(3))).objpic,playerposition.x,playerposition.y,30,30,null);
}

void checkObject(int hor, int ver)
{
    int objPositionX,objPositionY;
    Point playerposition = player.returnPosition();
    objPositionX= (playerposition.x+hor)/30;
    objPositionY= (playerposition.y+ver)/30;
    if(arr[objPositionX][objPositionY]==1)
    {
        arr[objPositionX][objPositionY]=-1;
    }
    else if(arr[objPositionX][objPositionY]==2)
    {
        //function for stone
    }
    else if( arr[objPositionX][objPositionY]==3)
    {
        arr[objPositionX][objPositionY]=-1;
        //function for diamond
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Expiredmind
  • 788
  • 1
  • 8
  • 29
  • sounds similar to this: http://stackoverflow.com/questions/8853397/repaint-in-java – Ueli Hofstetter Apr 11 '15 at 03:59
  • `Thread.sleep ( ... )` is the culprit here. Since you blocking the `Event Dispatcher Thread` while calling this method, hence all painting will be blocked, instead use [javax.swing.Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) – nIcE cOw Apr 11 '15 at 04:03
  • Hopefully this [example](http://stackoverflow.com/a/10837751/1057230) might can give some idea :-) – nIcE cOw Apr 11 '15 at 04:10

0 Answers0