Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with background and objects

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with background and objects

    I wonder how i can add my own background and change the ball to a image...

    // Klassen Tennisbana
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Tennisbana extends JPanel implements ActionListener{
    	private Timer klocka = new Timer(10, this);
    	private JLabel visaPoäng; // För att visa poäng
    	private int poäng;        // Aktuell poäng
    	private int xMax;         // högsta tillåtna x-koordinat
    	private int yMax;         // högsta tillåtna y-koordinat
    	private int radie;        // bollens radie
    	private int x;			  // x-koordinat, bollens mittpunkt
    	private int y;            // y-koordinat, bollens mittpunkt
    	private int vx;			  // bollens fart i x-led
    	private int vy;			  // bollens fart i y-led
    	private int xSteg;		  // bollens steglängd i x-led
    	private int ySteg;		  // bollens steglängs i y-led
    	private int racket;		  // rackets vänstra kant
    	private int längdR;		  // rackets längd
    	private int stegR;		  // rackets steglängd
     
    	public Tennisbana(){
    		setPreferredSize(new Dimension(300,600));
    		setBackground(Color.black);
    	}
     
    	public void start(JLabel p){
    		visaPoäng = p;
    		xMax = getSize().width;
    		yMax = getSize().height;
    		radie = 15;
    		längdR = 80;
    		stegR = 15;
    		nollställ();
     
    		// Lyssnar på tangentbordet
    		addKeyListener (new KeyAdapter(){
    			public void keyPressed(KeyEvent e){
    				if (e.getKeyCode() == KeyEvent.VK_A){
    					racket = Math.max(0, racket-stegR);
    				}
    				else if (e.getKeyCode() == KeyEvent.VK_D){
    					racket = Math.min(xMax-längdR, racket+stegR);
    				}
    			}
    		});
     
    		// Lyssnar på storleksändringar
    		addComponentListener(new ComponentAdapter(){
    			public void componentResized(ComponentEvent e){
    				xMax = e.getComponent().getSize().width;
    				yMax = e.getComponent().getSize().height;
    				e.getComponent().requestFocus();
    				repaint();
    			}
    		});
    	}
    	public void nollställ(){
    		poäng = 0;
    		visaPoäng.setText(" 0");
    		xSteg = vx = 5;
    		ySteg = vy = 5;
    		x = xMax/2;
    		y = radie+2;
    		racket = xMax/2-längdR/2;
    	}
     
    	public void startaSpel(){
    		if (poäng < 10){
    			klocka.start();
    		}
    	}
     
    	public void stoppaSpel(){
    		klocka.stop();
    	}
     
    	public void nyttSpel(){
    		stoppaSpel();
    		nollställ();
    		startaSpel();
    	}
     
    	// Anropas av timern var 10:e ms
    	public void actionPerformed(ActionEvent e){
    		if (y+radie >= yMax-4){
    			if (x < racket || x > racket+längdR){
    				Toolkit.getDefaultToolkit().beep();
    				visaPoäng.setText(String.valueOf(++poäng));
    				if (poäng >= 10){
    					stoppaSpel();
    				}
    			}
    			else if (xSteg > 0){
    				xSteg++;
    				vy++;
    			}
    			else{
    				xSteg--;
    				vy++;
    			}
    			ySteg = -vy;
    		}
    		else if (y-radie <= 0){
    			ySteg = vy;
    		}
     
    		if (x-radie<=0 || x+radie>=xMax){
    			xSteg = -xSteg;
    		}
     
    		x += xSteg;
    		y += ySteg;
    		if (y < radie){
    			y = radie;
    		}
    		else if (y > yMax-radie){
    			y = yMax-radie;
    		}
    		if (x < radie){
    			x = radie;
    		}
    		else if (x > xMax-radie){
    			x = xMax-radie;
    		}
     
    		repaint();
    	}
     
    	// Ritar boll och racket
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.yellow);
    		g.fillOval(x-radie, y-radie, 2*radie, 2*radie);
    		g.setColor(Color.red);
    		g.fillRect(racket, yMax-4, längdR, yMax);
    	}
    }


    i´m not sure if its here i should add the codes but please help
    Im from sweden so if you can swedish it would be easier to understand and the comments in the files is on swedish

    Thx for the help


    Ex67.java
    Tennis.java
    Tennisbana.java


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Help with background and objects

    Hej,

    You should just be able to load an image using ImageIO or something and create your self a BufferedImage and then you can use one of the graphics.drawImage methods to draw this image.

        final BufferedImage bufferedImage = ImageIO.read(new File("c:/image.jpg")); // Ladda in en bild fran harddisk

    And then in the paint method.

            g.drawImage(bufferedImage, x, y, null); // Rita ut bilden

    However, be sure you dont create/load the image every time you paint, you're best of loading the image once and then just using the loaded image in the paint method.

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    Afromiffo (April 6th, 2010)

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with background and objects

    where should i put in those codes and in what file?

    Thx for the help btw

  5. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Help with background and objects

    Recommended reading:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    db

Similar Threads

  1. Background image on GUI
    By OBLITERATOR in forum AWT / Java Swing
    Replies: 3
    Last Post: March 5th, 2010, 12:10 PM
  2. JButton set background problem
    By ellias2007 in forum AWT / Java Swing
    Replies: 1
    Last Post: February 25th, 2010, 12:15 AM
  3. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM
  4. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM