Closed Thread
Results 1 to 9 of 9

Thread: [java]background image

  1. #1
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    [java]background image

    I want to make a background image in my game I do in class. Can anyone tell me how? Here is the code:

    Code:
    package dk.koderko.games.pong; // vilket package det tillhör
    
    import java.io.IOException; // importerar några klasser för att kunna göra miljön i spelet
    import javax.microedition.lcdui.game.GameCanvas;
    import javax.microedition.lcdui.game.Sprite;
    import javax.microedition.lcdui.Graphics;
    import javax.microedition.lcdui.Image;
    import java.util.Random;
    
    public class PongCanvas extends GameCanvas implements Runnable { // gör så att man kan starta spelet
    
        public PongCanvas() {
            super(false);
        }
        
        public void run() {
            
            while(true) {
                
                updateScreen(getGraphics());
                
                try {
                    Thread.sleep(sleepTime);
                } catch (Exception e) {
                    
                }
            }
            
        }
        
        public void start() { // vad man ska göra när spelet startat
    
            try {
                ballImg = Image.createImage("/ball.png"); // bilder
                padImg = Image.createImage("/pad.png");
            } catch (IOException ioex) {
                System.out.println(ioex);
            }
    
            ballSprite = new Sprite(ballImg, 20, 21); // gör spriten
            ballSprite.defineReferencePixel(2, 2);
            ballSprite.setRefPixelPosition(ballX, ballY);
    
            padSprite = new Sprite(padImg, 3, 20);
            padSprite.defineReferencePixel(1, 10);
            padSprite.setRefPixelPosition(padX, padY);        
    
            AISprite = new Sprite(padImg, 3, 20);
            AISprite.defineReferencePixel(3, 10);
            AISprite.setRefPixelPosition(AIX, AIY);
    
            Thread runner = new Thread(this);
            runner.start();
        }
        
        private void createBackground(Graphics g) { // gör bakgrunden
            g.setColor(0x000000); //sätter färgen på bakgrunden
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    
        private void updateScreen(Graphics g) {
            
            createBackground(g);
            createScoreboard(g);
            creatWidth(g); // jag vet, felstavning på båda
            
            moveBall(); 
            movePad();
            moveAI();
            
            ballSprite.setRefPixelPosition(ballX, ballY);
            ballSprite.paint(g);        
    
            padSprite.setRefPixelPosition(padX, padY);
            padSprite.paint(g);
    
            AISprite.setRefPixelPosition(AIX, AIY);
            AISprite.paint(g);
            
            flushGraphics();
            
        }
        
        private void moveBall() { // flyttar bollen
            if (ballDirection == 0) {
                ballX -= ballXVel;
                ballY -= ballYVel;
            } else if (ballDirection == 1) {
                ballX += ballXVel;
                ballY -= ballYVel;
            } else if (ballDirection == 2) {
                ballX += ballXVel;
                ballY += ballYVel;
            } else if (ballDirection == 3) {
                ballX -= ballXVel;
                ballY += ballYVel;
            }
    
            if (ballDirection == 0 && ballX < 0) {
                ballDirection = 1;
                AIScore++;                                        // Added
            } else if (ballDirection == 0 && ballY < 0) {
                ballDirection = 3;
            } else if (ballDirection == 1 && ballY < 0) {
                ballDirection = 2;
            } else if (ballDirection == 1 && ballX > getWidth()) {
                ballDirection = 0;
                padScore++;                                        // Added
                if (sleepTime > 5) sleepTime--;
            } else if (ballDirection == 2 && ballY > getHeight()) {
                ballDirection = 1;
            } else if (ballDirection == 2 && ballX > getWidth()) {
                ballDirection = 3;
                padScore++;                                        // Added
                if (sleepTime > 5) sleepTime--;
            } else if (ballDirection == 3 && ballY > getHeight()) {
                ballDirection = 0;
            } else if (ballDirection == 3 && ballX < 0) {
                ballDirection = 2;
                AIScore++;                                        // Added
            }
    
            if (ballDirection == 0 && ballSprite.collidesWith(padSprite, false)) {
                ballDirection = 1;
            } else if (ballDirection == 3 && ballSprite.collidesWith(padSprite, false)) {
                ballDirection = 2;
            } else if (ballDirection == 1 && ballSprite.collidesWith(AISprite, false)) {
                ballDirection = 0;
            } else if (ballDirection == 2 && ballSprite.collidesWith(AISprite, false)) {
                ballDirection = 3;
            }
            
            sleepTime += AIScore - padScore;                                // Added
            if (sleepTime < 15) sleepTime = 10;                                 // Added
        
        }
    
        private void movePad() { // fixar så att man kanm kontrollera paden
            int keyState = getKeyStates();
            
            if ((keyState & UP_PRESSED) != 0 && padY > padSprite.getHeight() / 2) {
                padY -= padYVel;
            } else if ((keyState & DOWN_PRESSED) != 0 && padY <= getHeight() - padSprite.getHeight() / 2) {
                padY += padYVel;
            } 
        }
        
        private void moveAI() {
            Random random = new Random();
            actX = getWidth() / 3 + Math.abs(random.nextInt() % (getWidth() / 8));
            
            if (ballY < AIY && ballX > actX && AIY > AISprite.getHeight() / 2) AIY -= AIYVel;
            if (ballY > AIY && ballX > actX && AIY < getHeight() - AISprite.getHeight() / 2) AIY += AIYVel;
        }
    
        private void createScoreboard(Graphics g) {                                // Added entirely new method
            g.setColor(0xffffff);
            g.drawString(padScore + "  -  " + AIScore, getWidth() / 2, 20, Graphics.HCENTER | Graphics.TOP);        
        }
        
        private void creatWidth(Graphics g) {                                // Added entirely new method
            g.setColor(0xffffff);
            g.drawString(getWidth() + "  -  " + getHeight(), getWidth() / 2, 30, Graphics.HCENTER | Graphics.TOP);        
        }
        
        private int sleepTime = 10;
        
        private Image ballImg;
        private Sprite ballSprite;
        private int ballX = getWidth() / 2;
        private int ballY = getHeight() / 2;
        private final static int ballXVel = 3;
        private final static int ballYVel = 1;
        private int ballDirection = 1;
    
        
        private Image padImg;
        private Sprite padSprite;
        private int padX = 10;
        private int padY = getHeight() / 2;
        private final static int padYVel = 2;
        
        private Sprite AISprite;
        private int AIX = getWidth() - 10;
        private int AIY = getHeight() / 2;
        private final static int AIYVel = 2;
        private int actX;
        
        
        private int padScore = 0;                                        // Added
        private int AIScore = 0;                                        // Added
        
    }
    Ignore the swedish comments. The background image I made is 240 x 291 and it's name is back.png. If you could tell me how in a easy way I would be thankful.

  2. #2
    worldwise001's Avatar
    worldwise001 is offline x10Hosting Member worldwise001 is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Carbondale, IL
    Posts
    57

    Re: [java]background image

    In your createBackground function:

    Code:
        private void createBackground(Graphics g) { // gör bakgrunden
            //g.setColor(0x000000); //sätter färgen på bakgrunden
           g.drawImage(bgImg, 0, 0, getWidth(), getHeight(), null);
            //g.fillRect(0, 0, getWidth(), getHeight());
        }
    In your global var list at the end:

    Code:
        private int sleepTime = 10;
        
        private Image bgImg;
    
        private Image ballImg;
        private Sprite ballSprite;
    In your start function:

    Code:
        public void start() { // vad man ska göra när spelet startat
    
            try {
                ballImg = Image.createImage("/ball.png"); // bilder
                padImg = Image.createImage("/pad.png");
                bgImg = Image.createImage("/bgimg.png");
            } catch (IOException ioex) {
                System.out.println(ioex);
            }
    I'm not sure if this will create extra overhead in your application, but give it a shot anyway.


  3. #3
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [java]background image

    it returns:

    Uncaught exception java/lang/Error: Unresolved compilation problem:
    The method drawImage(Image, int, int, int) in the type Graphics is not applicable for the arguments (Image, int, int, int, int, null)
    .

  4. #4
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [java]background image

    hey would be really good if this got fixed to my java lesson tomorrow.

  5. #5
    nyanko's Avatar
    nyanko is offline x10Hosting Member nyanko is an unknown quantity at this point
    Join Date
    Jul 2008
    Location
    In front of my pc
    Posts
    57

    Re: [java]background image

    remove the extra arguments on the function call at the line specified and see what happen xD

    Also you could review the java API to know which arguments that function accepts, I don't remember that function D:
    Last edited by nyanko; 09-07-2008 at 05:31 PM.
    Hago banners/logos/diseños por comida y/o links a mi sitio :P.
    Making banners/logos/designs for food/backlink to my site :P.

  6. #6
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [java]background image

    Quote Originally Posted by nyanko View Post
    remove the extra arguments on the function call at the line specified and see what happen xD

    Also you could review the java API to know which arguments that function accepts, I don't remember that function D:
    which arguments do you mean?

  7. #7
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [java]background image

    in:

    Code:
        private void createBackground(Graphics g) { // gör bakgrunden
          //  g.setColor(0x000000); //sätter färgen på bakgrunden
          g.drawImage(bgImg, 0, 0, getWidth(), getHeight(), null);
           // g.fillRect(0, 0, getWidth(), getHeight());
        }
    it want me to change it to

    Code:
       private void createBackground(Graphics g) { // gör bakgrunden
          //  g.setColor(0x000000); //sätter färgen på bakgrunden
          g.drawImage(bgImg);
           // g.fillRect(0, 0, getWidth(), getHeight());
        }
    When I do, it want's me to add them again...

    It says: "The method drawImage(Image, int, int, int) in the type Graphics isn't applicable for the arguments (Image, int, int, int, int)"

  8. #8
    worldwise001's Avatar
    worldwise001 is offline x10Hosting Member worldwise001 is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    Carbondale, IL
    Posts
    57

    Re: [java]background image

    Problem resolved.

    the method was actually g.drawImage(Image img, int x, int y, int anchor);

    anchor is used for relative placement of text. Weird thing how java decided to implement this in the microedition version as opposed to in the full version.

    so it should have been g.drawImage(img, 0, 0, 0);

    Quote Originally Posted by nyanko View Post
    Also you could review the java API to know which arguments that function accepts, I don't remember that function D:
    Well he was using Java ME (microedition) which is apparently drastically different from Java SE (standard edition). APIs for ME are at http://java.sun.com/javame/reference/apis/jsr118/
    Last edited by worldwise001; 09-13-2008 at 01:56 PM.


  9. #9
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: [java]background image

    Thank you :P. *closed*

Closed Thread

Similar Threads

  1. 3000 credits for header image
    By bigjoe4 in forum The Marketplace
    Replies: 44
    Last Post: 05-02-2008, 11:35 AM
  2. Replies: 1
    Last Post: 11-04-2006, 06:49 PM
  3. errors while attaching
    By mattspec in forum Feedback and Suggestions
    Replies: 0
    Last Post: 12-19-2005, 01:50 PM
  4. November Desktop
    By n4tec in forum Off Topic
    Replies: 12
    Last Post: 11-08-2005, 07:18 AM
  5. Add free image hosting to your site
    By nigelwong in forum Off Topic
    Replies: 2
    Last Post: 09-19-2005, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers