Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Repaint Not Working

    I have the following code for a SpaceInvaders game, and repaint doesn't do anything.

    Using System.out.println() for debugging, I have noticed everything works smoothly, but repaint does nothing.

    Paint throws an error from the super.graphicsComponent(); if called directly with paint(getGraphics());

    Code:
    public class Launcher extends JFrame{
    	
    	private JButton nGame;
    	private JButton lGame;
    	private String dir = "";
    	
    	public Launcher(){
    		super("Space Invaders");
    		setLayout(new FlowLayout(FlowLayout.CENTER));
    		
    		if(isWindows()){
    			System.out.println("Windows");
    			dir = System.getenv("APPDATA") + "\\.SInvaders\\";
    		}
    		if(isUnix()){
    			System.out.println("Linux/Unix");
    			dir = "~/SInvaders/";
    		}
    		if(isMac()){
    			System.out.println("Mac");
    			dir = System.getProperty("user.home") + "/Library/Application Support/SInvaders/";
    		}
    		
    		nGame = new JButton("New Game");
    		lGame = new JButton("Load Save");
    		File sav = new File(dir+"save.sav");
    		
    		add(nGame);
    		add(lGame);
    		
    		nGame.addActionListener(new HC(false,dir));
    		lGame.addActionListener(new HC(true,dir));
    		
    		if(!sav.exists()){
    			lGame.setEnabled(false);
    		}
    		
    	}
    	//OS Detection functions, isWindows(), isUnix(), isMac()
    	
    	private class HC implements ActionListener {
    		
    		boolean load;
    		String dir;
    		public GameInit g;
    		public GameInit g2;
    		
    		public HC(boolean load, String dir){
    			this.load = load;
    			this.dir = dir;
    		}
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			remove(nGame);
    			remove(lGame);
    			repaint();
    			
    			try {
                                    //Launch the GameInit
    				g = new GameInit(load,dir);
    				
                                    //make it visible
                                    add(g);
    				g.setVisible(true);
    				g.setFocusable(true);
    
                                    //give it focus
    				g.requestFocusInWindow();
    				g.requestFocus();
    
                                    //enlarge it to populate the entire JFrame
    				g.setSize(400, 600);
    				g.repaint();
    
                                    //add key listener to JPanel, passing it the character's X coordinate
    				g.addKeyListener(new KL(g.charX));
    			} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    			}
    		}
    		private class KL implements KeyListener{
    			private int x;
    			public KL(int x){
    				this.x = x;
    			}
    			@Override
    			public void keyPressed(KeyEvent e) {
    				int code = e.getKeyCode();
    				if(code == KeyEvent.VK_LEFT){
    					if(this.x > 0){
    						this.x -= 10;
    						System.out.println(this.x);
    						new GameInit(this.x);
    						System.out.println("Left");
    					}
    				}else if(code == KeyEvent.VK_RIGHT){
    					if(this.x < 336){
    						this.x += 10;
    						
    						System.out.println("Right");
    					}
    				}else if(code == KeyEvent.VK_SPACE){
    					System.out.println("Fire");
    					fire(g.charX);
    				}
    			}
    
    			@Override
    			public void keyReleased(KeyEvent e) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void keyTyped(KeyEvent e) {
    				// TODO Auto-generated method stub
    				
    			}
    			public void fire(int x){
    				
    			}
    			
    		}
    	}
    }
    Where GameInit is the following:

    Code:
    public class GameInit extends JPanel{
    	
    	public int points;
    	public int lives;
    	public Image heart;
    	public Image player;
    	public boolean imLoad = false;
    	public String dir;
    	public int charX = 125;
    	
    	public GameInit(){
    	}
    	public GameInit(int x){
    		this.charX = x;
    		repaint();
    	}
    	public GameInit(boolean load, String dir) throws IOException{
    		if(load){
    			Scanner p = new Scanner(new FileInputStream(dir+"save.sav"));
    			points = Integer.parseInt(p.nextLine());
    			lives = Integer.parseInt(p.nextLine());
    		}else{
    			points = 0;
    			lives = 3;
    		}
    		this.dir = dir;
    		loadImages();
    	}
    	public void loadImages(){
    		if((new File(dir+"life.png").exists())){
    			heart = new ImageIcon(dir+"life.png").getImage();
    		}else{
    			System.out.println("No File \"" + dir + "life.png\"");
    		}
    		if((new File(dir+"char.png").exists())){
    			player = new ImageIcon(dir+"char.png").getImage();
    		}else{
    			System.out.println("No File \"" + dir + "char.png\"");
    		}
    		
    		if(player != null && heart != null)
    			imLoad = true;
    	}
    	public void paint(Graphics g) {
    		super.paintComponent(g);
    		super.setBackground(Color.BLACK);
    		if(g instanceof Graphics2D){
    			Graphics2D g2 = (Graphics2D)g;
    			
    			Font def = new Font("Courier New", Font.BOLD, 16);
    			g.setFont(def);
    			g.setColor(Color.WHITE);
    			g.drawString(Integer.toString(points), 10, 568);
    			//g.drawString(Integer.toString(lives), 384, 568);
    			if(imLoad){
    				for(int x = 374; ((400 - x) / 16) <= lives; x = x - 16){
    					g.drawImage(heart, x, 558, null);
    				}
    				g.drawImage(player, this.charX, 490, null);
    			}else{
    				System.out.println("Could not acquire 1 or more image(s)");
    			}
    		}
    	}
    }
    Last edited by Scriptr; 06-15-2012 at 07:14 AM. Reason: More info


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •