14 Replies - 2977 Views - Last Post: 24 July 2012 - 11:40 AM Rate Topic: -----

#1 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 01:13 AM

Just like my topic says, when I run an executable jar file exported from eclipse my JFrame remains empty.
If I compile and run the same project in Eclipse itself it works fine.
My project is a simple brick breaker game with several classes and too much code to paste it all, so, as I don't know which part of the code may be relevant, or if any of it would be relevant at all, I'll just post 1 method, the one I think could have something to do with the problem.
Basically for the drawing I create a Graphics2D buffer, draw what I want to the buffer, then draw it to the screen with
public void drawScreen()
{
    Graphics2D g = (Graphics2D)this.getGraphics();
    g.drawImage(buffer,0,0,this);
    Toolkit.getDefaultToolkit().sync();
    g.dispose();
}


Please help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Buffer not drawn onto Jframe when executable .Jar file is ran

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 04:20 AM

Make sure you're packaging your resources properly

http://technojeeves....va-with-eclipse
Was This Post Helpful? 0
  • +
  • -

#3 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 05:19 AM

Umm, sorry, I'm not sure what that tutorial was supposed to accomplish, tried doing what it showed but to no avail.
My resources are text files the contents of which hold the positions on blocks for levels. I remember once compiling the code with the text file for the first level being in the wrong directory, which gave me that blank JFrame and file not found exception. Maybe its something similar, I just don't see the console....
Yeah, now that i checked another one of my games which works with text files doesn't do a function its supposed to accomplish through file i/o...
I've only worked with java for a short while and have not much of an idea about packages...
If you could tell me how to properly package a text file into a runnable jar file i'd be thankful :)
If the above tutorial was meant to show how that is done, I was unable to understand it.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 05:24 AM

Quote

If the above tutorial was meant to show how that is done, I was unable to understand it.


I don't see how it could be any clearer sorry. That would be all you need to do before exporting as an executable jar in Eclipse.
As for errors, unless you've coded it improperly, you should be seeing exceptions:

http://technojeeves....free/118-errors
Was This Post Helpful? 0
  • +
  • -

#5 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 05:48 AM

It was very clear yes... it told me to put my resources(ie text files) and code files into separate packages. I have done this... aswell as put the needed text files into every directory there is.
Umm where would I get the exceptions? I am not getting anything when launching the runnable...
Thank you for helping
Please let me know if theres any more info that I can post to help resolve my issue

I get no errors or exceptions when compiling/running in eclipse
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 06:50 AM

You need to post your code please
Was This Post Helpful? 0
  • +
  • -

#7 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 07:20 AM

BBmain.java
package codePackage;

import javax.swing.*;

public class BBmain
{
        JFrame window;
        DrawPanel panel;

        public BBmain()
        {
            window = new JFrame("Ball Breaker!");
            panel = new DrawPanel();
            window.setSize(1000, 700);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.getContentPane().add(panel);
            window.setLocationRelativeTo(null);
            window.setVisible(true);
        }
        
        public void go()
        {
            panel.startGame();
        }

        public static void main(String[]args)
        {
            BBmain game = new BBmain();
            game.go();
        }
}



DrawPanel.java
package codePackage;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.*;


public class DrawPanel extends JPanel implements KeyListener
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	BufferedImage buffer;
	Entity paddle;
	Entity ball;
	int level, destroyed;
	Bricks bricks;
	ArrayList<Brick> brickArray;
	Brick brick;
	int totalBricks, currentBricks, levelUp;
	
    public DrawPanel()
    {
        setIgnoreRepaint(true);
        addKeyListener(this);
        setFocusable(true);
        level = 1;
    }
    
    public void initialize()
    {
    	buffer = new BufferedImage(1000, 700,BufferedImage.TYPE_INT_RGB);
    	paddle = new Entity(450, 650, 0);
    	ball = new Entity(495, 640, 1);
        bricks = new Bricks(0);
    	loadLvl(1);
    }
    
    public void loadLvl(int lvl)
    {
    	levelUp = 0;
    	totalBricks = 0;
        bricks.loadLevel(lvl);
        bricks.fillGrid();
        brickArray = new ArrayList<Brick>();
        
        int brickX = 10, brickY = 55;
        
        for(int i=0; i<400; i++)
        {
        	if(bricks.brickGrid[i] == 2)
        	{
        		brickX += 2;
        		brick = new Brick(brickX, brickY, 0);
        		brickArray.add(brick);
        		brickX += 30;
        		totalBricks++;
        	}
        	else if(bricks.brickGrid[i] == 1)
        	{
        		brickX += 2;
        		brickX += 30;
        	}
        	else if(bricks.brickGrid[i] == 3)
        	{
        		brickY += 17;
        		brickX = 10;
        	}
        }
        currentBricks = totalBricks;
    }

    public void update()
    {
    	if(ball.x < -50 || ball.x > 1050 || ball.y < -50 || ball.y > 750)
    		System.exit(0);
    	paddle.movePaddle();
    	if(!paddle.launch)
    	{
    		ball.moveBallBeforeLaunch();
    	}
    	else if(paddle.launch)
    	{
    		if(ball.getBounds().intersects(new Rectangle(paddle.getX(), paddle.getY()-3, 20, 10)))
    		{
        		ball.moveBall(14);
    		}
    		else if(ball.getBounds().intersects(new Rectangle(paddle.getX()+80, paddle.getY()-3, 20, 10)))
    		{
        		ball.moveBall(2);
    		}
    		else if(ball.getBounds().intersects(new Rectangle(paddle.getX()+40, paddle.getY()-3, 20, 10)))
    		{
    			ball.moveBall(0);
    		}
    		else if(ball.getBounds().intersects(new Rectangle(paddle.getX()+20, paddle.getY()-3, 20, 10)))
    		{
    			ball.moveBall(15);
    		}
    		else if(ball.getBounds().intersects(new Rectangle(paddle.getX()+60, paddle.getY()-3, 20, 10)))
    		{
    			ball.moveBall(1);
    		}
    		else
    			ball.moveBall(16);
    	}
    	
    }

    public void checkCollisions()
    {
    	if (ball.getBounds().intersects(new Rectangle(-1000, 670, 3000, 1000)))
    	{
    		paddle.lives--;
    		paddle.launch = false;
    		ball.launch = false;
    		ball.x = paddle.getX() + 45;
    		ball.y = 640;
			if(paddle.lives == 0)
			{
				JOptionPane.showMessageDialog(null, "         Y O U  L O S E !");
				int dialogButton = JOptionPane.YES_NO_OPTION;
				int dialogResult = JOptionPane.showConfirmDialog (null, "Play Again?","",dialogButton);
				if(dialogResult == JOptionPane.YES_OPTION)
				{
	                BBmain game = new BBmain();
	                game.go();
				}
				else
					System.exit(0);
			}
    	}
    	for(int i=0; i<totalBricks; i++)
    	{			
			if(!brickArray.get(i).destroyed && ball.getBounds().intersects(new Rectangle(brickArray.get(i).getX()+29, brickArray.get(i).getY()+1, 1, 13)))//left
        	{
    			brickArray.get(i).destroyed = true;
    			currentBricks--;
    			if(ball.dir == 12)
    			{
    				ball.x += ball.ballSpeed;
    				ball.dir = 4;
    			}
    			else if(ball.dir == 10)
    			{
    				ball.y += ball.ballSpeed45;
    				ball.x += ball.ballSpeed45;
    				ball.dir = 6;
    			}
    			else if(ball.dir == 14)
    			{
    				ball.x += ball.ballSpeed45;
    				ball.y -= ball.ballSpeed45;
    				ball.dir = 2;
    			}
    			else if(ball.dir == 15)
    			{
    				ball.y -= ball.ballSpeed1;
    				ball.x += ball.ballSpeed2;
    				ball.dir = 1;
    			}
    			else if(ball.dir == 13)
    			{
    				ball.y -= ball.ballSpeed2;
    				ball.x += ball.ballSpeed1;
    				ball.dir = 3;
    			}
    			else if(ball.dir == 11)
    			{
    				ball.x += ball.ballSpeed1;
    				ball.y += ball.ballSpeed2;
    				ball.dir = 5;
    			}
    			else if(ball.dir == 9)
    			{
    				ball.y += ball.ballSpeed1;
    				ball.x += ball.ballSpeed2;
    				ball.dir = 7;
    			}
        	}
        	else if(!brickArray.get(i).destroyed && ball.getBounds().intersects(new Rectangle(brickArray.get(i).getX()+1, brickArray.get(i).getY(), 28, 1)))//bottom
        	{
    			brickArray.get(i).destroyed = true;
    			currentBricks--;
        		if(ball.dir == 8)
        		{
        			ball.y -= ball.ballSpeed;
        			ball.dir = 0;
        		}
        		else if(ball.dir == 10)
        		{
        			ball.y -= ball.ballSpeed45;
        			ball.x -= ball.ballSpeed45;
        			ball.dir = 14;
        		}
        		else if(ball.dir == 6)
        		{
        			ball.x += ball.ballSpeed45;
        			ball.y -= ball.ballSpeed45;
        			ball.dir = 2;
        		}
        		else if(ball.dir == 11)
        		{
        			ball.y -= ball.ballSpeed2;
        			ball.x -= ball.ballSpeed1;
        			ball.dir = 13;
        		}
        		else if(ball.dir == 9)
        		{
        			ball.x -= ball.ballSpeed2;
        			ball.y -= ball.ballSpeed1;
        			ball.dir = 15;
        		}
        		else if (ball.dir == 7)
        		{
        			ball.y -= ball.ballSpeed1;
        			ball.x += ball.ballSpeed2;
        			ball.dir = 1;
        		}
        		else if(ball.dir == 5)
        		{
        			ball.x += ball.ballSpeed1;
        			ball.y -= ball.ballSpeed2;
        			ball.dir = 3;
        		}
        	}
        	else if(!brickArray.get(i).destroyed && ball.getBounds().intersects(new Rectangle(brickArray.get(i).getX()+1, brickArray.get(i).getY()+14, 28, 1)))//top
        	{
    			brickArray.get(i).destroyed = true;
    			currentBricks--;
    			if(ball.dir == 0)
        		{
    				ball.y += ball.ballSpeed;
        			ball.dir = 8;
        		}
        		else if(ball.dir == 2)
        		{
        			ball.x += ball.ballSpeed45;
        			ball.y += ball.ballSpeed45;
        			ball.dir = 6;
        		}
        		else if(ball.dir == 14)
        		{
        			ball.x -= ball.ballSpeed45;
        			ball.y += ball.ballSpeed45;
        			ball.dir = 10;
        		}
        		else if(ball.dir == 13)
        		{
        			ball.x -= ball.ballSpeed1;
        			ball.y += ball.ballSpeed2;
        			ball.dir = 11;
        		}
        		else if(ball.dir == 15)
        		{
        			ball.x -= ball.ballSpeed2;
        			ball.y += ball.ballSpeed1;
        			ball.dir = 9;
        		}
        		else if(ball.dir == 1)
        		{
        			ball.x += ball.ballSpeed2;
        			ball.y += ball.ballSpeed1;
        			ball.dir = 7;
        		}
        		else if(ball.dir == 3)
        		{
        			ball.x += ball.ballSpeed1;
        			ball.y -= ball.ballSpeed2;
        			ball.dir = 5;
        		}
        	}
        	else if(!brickArray.get(i).destroyed && ball.getBounds().intersects(new Rectangle(brickArray.get(i).getX(), brickArray.get(i).getY()+1, 1, 13)))//right
        	{
    			brickArray.get(i).destroyed = true;
    			currentBricks--;
    			if(ball.dir == 4)
        		{
    				ball.x -= ball.ballSpeed;
        			ball.dir = 12;
        		}
        		else if(ball.dir == 6)
        		{
        			ball.y += ball.ballSpeed45;
        			ball.x -= ball.ballSpeed45;
        			ball.dir = 10;
        		}
        		else if(ball.dir == 2)
        		{
        			ball.y -= ball.ballSpeed45;
        			ball.x -= ball.ballSpeed45;
        			ball.dir = 14;
        		}
        		else if(ball.dir == 1)
        		{
        			ball.x -= ball.ballSpeed2;
        			ball.y -= ball.ballSpeed1;
        			ball.dir = 15;	            			
        		}
        		else if(ball.dir == 3)
        		{
        			ball.x -= ball.ballSpeed1;
        			ball.y -= ball.ballSpeed2;
        			ball.dir = 13;
        		}
        		else if(ball.dir == 5)
        		{
        			ball.x -= ball.ballSpeed1;
        			ball.y += ball.ballSpeed2;
        			ball.dir = 11;
        		}
        		else if(ball.dir == 7)
        		{
        			ball.y += ball.ballSpeed1;
        			ball.x -= ball.ballSpeed2;
        			ball.dir = 9;
        		}
        	}
		}
		if(currentBricks == 0)
		{
			level++;
			JOptionPane.showMessageDialog(null, "         LEVEL CLEAR!");
			loadLvl(level);
    		paddle.launch = false;
    		ball.launch = false;
    		ball.x = paddle.getX() + 45;
    		ball.y = 640;
		}
    }

    public void drawBuffer()
    {
    	 Graphics2D b = buffer.createGraphics();
         b.setColor(Color.black);
         b.fillRect(0,0,1000,700);
         b.setColor(Color.darkGray);
         b.fillRect(0, 0, 1010, 55);
         
         b.setColor(Color.red);
         b.fillRect(paddle.getX(),paddle.getY(),paddle.getWidth(),paddle.getHeight());
         

    	 int spacing = 15;
         b.setColor(Color.white);
         for(int i=0; i<paddle.lives; i++)
         {
             b.fillOval(5 + (spacing * i), 10,ball.getWidth(),ball.getHeight());
        	 
         }

         b.drawString("Bricks left: "+ currentBricks, 5, 40);

         b.setColor(Color.blue);
         for(int i=0; i<totalBricks; i++)
         {
        	 if(!brickArray.get(i).destroyed)
        		 b.fillRect(brickArray.get(i).getX(), brickArray.get(i).getY(), brickArray.get(i).width, brickArray.get(i).height);
         }

         b.setColor(Color.red);
         b.fillOval(ball.getX(),ball.getY(),ball.getWidth(),ball.getHeight());
         b.dispose();
         /* b.setColor(Color.white);
         b.drawString("C O L L I S I O N !",350,300);
         b.dispose();*/
    }

    public void drawScreen()
    {
    	Graphics2D g = (Graphics2D)this.getGraphics();
    	g.drawImage(buffer,0,0,this);
    	Toolkit.getDefaultToolkit().sync();
    	g.dispose();
    }

    public void startGame()
    {
    	initialize();
    	while(true)
    	{
	        try
	        {
                update();
                checkCollisions();
                drawBuffer();
                drawScreen();
                Thread.sleep(1);
	        }
	        catch(Exception e)
	        {
                e.printStackTrace();
	        }       
    	}
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyPressed(KeyEvent e)
    {
    	int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT)
        {
        	if(paddle.launch)
        		paddle.left = true;
        	else
        	{
        		paddle.left = true;
        		ball.left = true;
        	}
        }
        if (key == KeyEvent.VK_RIGHT)
        {
                if(paddle.launch)
            		paddle.right = true;
            	else
            	{
            		paddle.right = true;
            		ball.right = true;
            	}
        }
        if (key == KeyEvent.VK_SPACE)
        {
        	if(!paddle.launch)
        	{
        		paddle.launch = true;
        		ball.launch = true;
        	}
        }
        if (key == KeyEvent.VK_P)
        {
        	if(!ball.paused)
        	{
	        	ball.paused = true;
	            paddle.paused = true;
        	}
        	else
        	{
        		ball.paused = false;
            	paddle.paused = false;
        	}
        }
        if(ball.paused && key == KeyEvent.VK_N)
        	levelUp++;
        if(ball.paused && key == KeyEvent.VK_E)
        	levelUp++;
        if(ball.paused && key == KeyEvent.VK_X)
        	levelUp++;
        if(ball.paused && key == KeyEvent.VK_T)
        	levelUp++;
        if(levelUp == 4)
        {
        	level++;
        	loadLvl(level);
        }
    }

    public void keyReleased(KeyEvent e)
    {
    	int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT)
        {
                paddle.left = false;
                ball.left = false;
        }
        if (key == KeyEvent.VK_RIGHT)
        {
                paddle.right = false;
                ball.right = false;
        }
    }
}



Entity.java
package codePackage;
import java.awt.Rectangle;

public class Entity
{
         int x,y,speed,width,height, paddleSpeed, ballSpeed, ammo, dir, type, lives, ballSpeed45, ballSpeed1, ballSpeed2, previousX, previousY;
         boolean up, down, left, right,stop, collision, paused, launch, fired, wallCollision;
   
        public Entity(int x, int y, int type)
        {
            this.x = x;
            this.y = y;
            this.type = type;
            paddleSpeed = 7;
            ballSpeed = 5;
            ballSpeed45 = 4;
            ballSpeed1 = 4;
            ballSpeed2 = 3;
            
            if(type == 0)
            {
                width = 100;
                height = 10;
            }
            else if(type == 1)
            {
            	width = 10;
            	height = 10;
            }
            
            up = false;
            down = false;
            left = false;
            right = false;
            collision = false;
            launch = false;
            ammo = 0; 
            fired = false;
            dir = 0;
            lives = 5;
        }
        
        public int getX()
        {
                return x;
        }
        
        public int getY()
        {
                return y;
        }
        
        public int getWidth()
        {
                return width;
        }
        
        public int getHeight()
        {
                return height;
        }
        
        public Rectangle getBounds()
        {
                return new Rectangle(getX(),getY(),getWidth(),getHeight());
        }

        public void movePaddle()
        {
        	if(!paused)
        	{
	            //left line
	            if (left && !(this.getBounds().intersects(new Rectangle(-1000, -1000, 1005, 3000))))
	                    x -= paddleSpeed;
	            //right line
	            if (right && !(this.getBounds().intersects(new Rectangle(980, -1000, 1000, 3000))))
	                    x += paddleSpeed;
        	}
        }
        
        public void moveBallBeforeLaunch()
        {
        	if(!paused)
        	{
	            //left line
	            if (left && !(this.getBounds().intersects(new Rectangle(-1000, -1000, 1050, 3000))))
	                    x -= paddleSpeed;
	            //right line
	            if (right && !(this.getBounds().intersects(new Rectangle(935, -1000, 1000, 3000))))
	                    x += paddleSpeed;
        	}
        }
        
		public void moveBall(int direction)
		{
			wallCollision = false;
			if(direction < 16)
				this.dir = direction;
			
			if(!paused && launch)
	    	{
				do
				{
		        	if(this.getBounds().intersects(new Rectangle(-1000, -1000, 3000, 1056)) || this.getBounds().intersects(new Rectangle(-1000, -1000, 1005, 3000)) || this.getBounds().intersects(new Rectangle(980, -1000, 1000, 3000)))
		        	{
		        		wallCollision = true;
		            	if(this.getBounds().intersects(new Rectangle(-1000, -1000, 1005, 3000)))//left
		            	{
		        			if(dir == 12)
		        			{
		        				x += ballSpeed;
		        				dir = 4;
		        			}
		        			else if(dir == 10)
		        			{
		        				y += ballSpeed45;
		        				x += ballSpeed45;
		        				dir = 6;
		        			}
		        			else if(dir == 14)
		        			{
		        				x += ballSpeed45;
		        				y -= ballSpeed45;
		        				dir = 2;
		        			}
		        			else if(dir == 15)
		        			{
		        				y -= ballSpeed1;
		        				x += ballSpeed2;
		        				dir = 1;
		        			}
		        			else if(dir == 13)
		        			{
		        				y -= ballSpeed2;
		        				x += ballSpeed1;
		        				dir = 3;
		        			}
		        			else if(dir == 11)
		        			{
		        				x += ballSpeed1;
		        				y += ballSpeed2;
		        				dir = 5;
		        			}
		        			else if(dir == 9)
		        			{
		        				y += ballSpeed1;
		        				x += ballSpeed2;
		        				dir = 7;
		        			}
		            	}
		            	/*else if(this.getBounds().intersects(new Rectangle(-1000, 670, 3000, 1000)))//bottom
		            	{
		            		y -= random;
		            		if(dir == 4)
		            			dir = 0;
		            		if(dir == 5)
		            			dir = 7;
		            		if(dir == 3)
		            			dir = 1;
		            	}*/
		            	else if(this.getBounds().intersects(new Rectangle(-1000, -1000, 3000, 1056)))//top
		            	{
		            		if(dir == 0)
		            		{
		            			y += ballSpeed;
		            			dir = 8;
		            		}
		            		else if(dir == 2)
		            		{
		            			x += ballSpeed;
		            			y += ballSpeed;
		            			dir = 6;
		            		}
		            		else if(dir == 14)
		            		{
		            			x -= ballSpeed;
		            			y += ballSpeed;
		            			dir = 10;
		            		}
		            		else if(dir == 13)
		            		{
		            			x -= ballSpeed1;
		            			y += ballSpeed2;
		            			dir = 11;
		            		}
		            		else if(dir == 15)
		            		{
		            			x -= ballSpeed2;
		            			y += ballSpeed1;
		            			dir = 9;
		            		}
		            		else if(dir == 1)
		            		{
		            			x += ballSpeed2;
		            			y += ballSpeed1;
		            			dir = 7;
		            		}
		            		else if(dir == 3)
		            		{
		            			x += ballSpeed1;
		            			y -= ballSpeed2;
		            			dir = 5;
		            		}
		            	}
		            	else if(this.getBounds().intersects(new Rectangle(980, -1000, 1000, 3000)))
		            	{
		            		if(dir == 4)
		            		{
		            			x -= ballSpeed;
		            			dir = 12;
		            		}
		            		else if(dir == 6)
		            		{
		            			y += ballSpeed45;
		            			x -= ballSpeed45;
		            			dir = 10;
		            		}
		            		else if(dir == 2)
		            		{
		            			y -= ballSpeed45;
		            			x -= ballSpeed45;
		            			dir = 14;
		            		}
		            		else if(dir == 1)
		            		{
		            			x -= ballSpeed2;
		            			y -= ballSpeed1;
		            			dir = 15;	            			
		            		}
		            		else if(dir == 3)
		            		{
		            			x -= ballSpeed1;
		            			y -= ballSpeed2;
		            			dir = 13;
		            		}
		            		else if(dir == 5)
		            		{
		            			x -= ballSpeed1;
		            			y += ballSpeed2;
		            			dir = 11;
		            		}
		            		else if(dir == 7)
		            		{
		            			y += ballSpeed1;
		            			x -= ballSpeed2;
		            			dir = 9;
		            		}
		            	}
		            	
		            	
		        	}
			        else
	            		wallCollision = false;
				}while(wallCollision);
		        	
	        	//if(!wallCollision)
	        	{
		        	switch (dir)
		        	{
		        	case 0:
		        		y -= ballSpeed;
		        		break;
		        	case 1:
		        		x += ballSpeed1;
		        		y -= ballSpeed2;
		        		break;
		        	case 2:
		        		y -= ballSpeed45;
		        		x += ballSpeed45;
		        		break;
		        	case 3:
		        		y -= ballSpeed2;
		        		x += ballSpeed1;
		        		break;
		        	case 4:
		        		x += ballSpeed;
		        		break;
		        	case 5:
		        		y += ballSpeed2;
		        		x += ballSpeed1;
		        		break;
		        	case 6:
		        		y += ballSpeed45;
		        		x += ballSpeed45;
		        		break;
		        	case 7:
		        		y += ballSpeed1;
		        		x += ballSpeed2;
		        		break;       
		        	case 8:
		        		y += ballSpeed;
		        		break;
		        	case 9:
		        		y += ballSpeed1;
		        		x -= ballSpeed2;
		        		break;
		        	case 10:
		        		y += ballSpeed45;
		        		x -= ballSpeed45;
		        		break;
		        	case 11:
		        		x -= ballSpeed1;
		        		y += ballSpeed2;
		        		break;
		        	case 12:
		        		x -= ballSpeed;
		        		break;
		        	case 13:
		        		y -= ballSpeed2;
		        		x -= ballSpeed1;
		        		break;
		        	case 14:
		        		y -= ballSpeed45;
		        		x -= ballSpeed45;
		        		break;
		        	case 15:
		        		y -= ballSpeed1;
		        		x -= ballSpeed2;
		        		break; 
        			default:
			        		y -= ballSpeed;
			        		break;
		        	}
	        	}
	    	}
			previousX = x;
			previousY = y;
		}
		
		public void moveProjectile()
		{
			if(fired && ammo > 0)
			{
				ammo--;
			}
		}
 
         
}



Brick.java
package codePackage;
import java.awt.Rectangle;

public class Brick 
{
	int x, y, width, height, type;
	boolean destroyed;
	
	Brick(int x, int y, int type)
	{
		this.x = x;
		this.y = y;
		width = 30;
		height = 15;
		destroyed = false;
	}
	
	public int getX()
    {
        return x;
	}
	
	public int getY()
	{
        return y;
	}
	
	public int getWidth()
	{
        return width;
	}
	
	public int getHeight()
	{
        return height;
	}
	
	public Rectangle getBounds()
	{
        return new Rectangle(getX(),getY(),getWidth(),getHeight());
	}
}



Bricks.java
package codePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Bricks 
{
	int[] brickGrid;
	int height, width, spacing, level;
	String fileName, fileContents;
	
	Bricks(int level)
	{
		this.level = level;
		brickGrid = new int[1000];
		height = 10;
		width = 30;
		spacing = 3;
	}
	
	public void loadLevel(int level)
	{
		fileName = "level"+level+".txt";		
		File file  = new File(fileName);
		
		try 
		{
			Scanner in = new Scanner(file);
			in.useDelimiter("\n");
			fileContents = in.next();
		} 
		catch (FileNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void fillGrid()
	{
		for(int i=0; i<fileContents.length(); i++)
		{
			if(fileContents.charAt(i) == ' ')
				brickGrid[i] = 1;
			else if(fileContents.charAt(i) == '-')
				brickGrid[i] = 2;
			else if(fileContents.charAt(i) == '|')
				brickGrid[i] = 3;
		}
		
		for(int i=fileContents.length(); i < 1000; i++)
		{
			brickGrid[i] = 0;
		}
		
		/*for(int i=0; i<1000; i++)
		{
			if(brickGrid[i] == 1)
				System.out.print(" ");
			else if(brickGrid[i] == 2)
				System.out.print("-");
			else if(brickGrid[i] == 3)
				System.out.print("\n");			
		}*/
	}
}



level1.txt
- - - - - - - - - - - - - - -|- - - - - - - - - - - - - - - | - - - - - - - - - - - - - - -|- - - - - - - - - - - - - - - | - - - - - - - - - - - - - - -

sorry about the editing... it put in alot of newlines in the long ifs
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 08:12 AM

Even better if you attach all .java as a zip archive
Was This Post Helpful? 0
  • +
  • -

#9 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 08:22 AM

duuh ... Good idea :)
here's the project folder
Attached File  BrickBreaker.zip (18.67K)
Number of downloads: 37
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 09:18 AM

OK, so what exactly is not appearing?
Was This Post Helpful? 0
  • +
  • -

#11 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 09:28 AM

If I export the project as a runnable jar, and open the jar all I get is a blank JFrame

Also, another simple game I made just closes when a call is made to read from a text file.
This game, brick breaker, calls the read file almost right away, to build the brick layout of the level, and it just never gets to the place where it draws the buffer to the frame is what im thinking, so it seems like maybe the text files are not included in the runnable jar...

or they have a different path...
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 09:39 AM

You're trying to load the files as File. You can't do that when you have a jar unless you use absolute paths in the file system. Do it like this:


package codePackage;

import java.util.Scanner;


public class Bricks {
    int[] brickGrid;
    int height;
    int width;
    int spacing;
    int level;
    String fileName;
    String fileContents;

    Bricks(int level) {
        this.level = level;
        brickGrid = new int[1000];
        height = 10;
        width = 30;
        spacing = 3;
    }

    public void loadLevel(int level) {
        // Make this a static class variable, not a local one
        final String RESOURCE_PATH = "/resourcePackage/level%d.txt";
        String resource = String.format(RESOURCE_PATH, level);

        try {
            Scanner in = new Scanner(getClass().getResourceAsStream(resource));
            in.useDelimiter("\n");
            fileContents = in.next();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void fillGrid() {
        for (int i = 0; i < fileContents.length(); i++) {
            if (fileContents.charAt(i) == ' ') {
                brickGrid[i] = 1;
            } else if (fileContents.charAt(i) == '-') {
                brickGrid[i] = 2;
            } else if (fileContents.charAt(i) == '|') {
                brickGrid[i] = 3;
            }
        }

        for (int i = fileContents.length(); i < 1000; i++) {
            brickGrid[i] = 0;
        }

        /*for(int i=0; i<1000; i++)
        {
            if(brickGrid[i] == 1)
                System.out.print(" ");
            else if(brickGrid[i] == 2)
                System.out.print("-");
            else if(brickGrid[i] == 3)
                System.out.print("\n");
        }*/
    }
}



btw 'resourcePackage' is overkill ;) 'resources' is normal and adequate

This post has been edited by g00se: 24 July 2012 - 09:43 AM

Was This Post Helpful? 1
  • +
  • -

#13 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 10:05 AM

Ill try it when I get home thanks alot :)

Ill do it I mean :P
Was This Post Helpful? 0
  • +
  • -

#14 seijurojushi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 24-July 12

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 11:28 AM

Thank you so much, that worked wonders :)
can you please explain to me the level%d.txt part, I've never seen anything like it :)
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Buffer not drawn onto Jframe when executable .Jar file is ran

Posted 24 July 2012 - 11:40 AM

It's just using String.format to ease the construction of the url. Follow (all the) links to give yourself the best understanding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1