JFrame doesn't refresh unless minimized and then maximized

Joined
Apr 18, 2007
Messages
1
Reaction score
0
This is more of a nub question on how to do something than a problem. I've been working on this for the past few days and I can't figure this out. The window doesn't refresh unless done so manually by minimizing and maximizing. I want it to refresh constantly, or at least when something changes.

Here's my code. Thanks in advance to anyone who helps me.

main class:
Code:
import javax.swing.JFrame;

public class graphicstester
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame ("Hey");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        graphicstest graphics = new graphicstest();

        frame.setContentPane(graphics);
        frame.setSize(256,256);
        frame.setVisible(true);
    }
}

method class
Code:
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.JFrame;

public class graphicstest extends javax.swing.JPanel
{
Graphics g;
int[][] d00d;
int x,y;
boolean hey;
public graphicstest()
{
init();

}

public void init()
    {
                 int[][] d00d = { {0,0,1,1,1,1,0,0},
                 {0,0,1,1,1,1,0,0},
                 {0,0,0,1,1,0,0,0},
                 {1,1,1,1,1,1,1,1},
                 {0,0,0,1,1,0,0,0},
                 {0,0,1,1,1,1,0,0},
                 {0,1,1,0,0,1,1,0},
                 {1,1,0,0,0,0,1,1}};

int[][] block = { {1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1}};

int[][] map = { {1,1,1,1,1,1,1,1},
{1,2,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1}};


        

        setFocusable(true);
        addKeyListener( new KeyPress() );
        setBackground( Color.green );
        x = 3;
        y = 3;
    }
    
protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawRect(x,y,4,4);

        
    }
    
public void update(Graphics g)
{
super.update(g);
}

    

public class KeyPress      implements KeyListener
    {
        public void keyPressed(KeyEvent event )
        {

                if (event.getKeyCode() == KeyEvent.VK_W )
                {
                 if(fits(x,y-1)){
                     y--;paintComponent(g);}
                }
                if (event.getKeyCode() == KeyEvent.VK_A )
                {
                 if(fits(x-1,y)){
                     x--;paintComponent(g);}
                }
                if (event.getKeyCode() == KeyEvent.VK_S )
                {
                 if(fits(x,y+1)){
                     y++;paintComponent(g);}
                }
                if (event.getKeyCode() == KeyEvent.VK_D )
                {
                 if(fits(x+1,y)){
                     x++;paintComponent(g);}
                }
}
            
        
        public void keyTyped( KeyEvent event )
                {}
        public void keyReleased( KeyEvent event )
                {}

}

public boolean fits(int x,int y){
boolean fits = true;
if((x>256)||(x<0)||(y>256)||(y<0)){fits=false;}
return fits;
}

public void sprite()
{

    }


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top