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 15 of 15

Thread: How can i add an image into my source code?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can i add an image into my source code?

    this is a racing game and i want the picture to take place for my "square cars". thanks, heres the code, everything works fine, i just wanna add the pics.
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.ImageObserver;
    import java.io.*;
    import java.net.*;
    import java.applet.AudioClip;
     
    public class GameRace extends JFrame {
     
    	final int WIDTH = 900, HEIGHT = 650;
    	boolean winnerChosen = false;
     
     
     
    	double p1Speed =15, p2Speed =15;
    	int p1Laps = 0, p2Laps = 0;
     
    	final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
     
    	int p1Direction = UP;
    	int p2Direction = UP;
     
    	Rectangle left = new Rectangle(0,0,WIDTH/9, HEIGHT);
    	Rectangle right = new Rectangle((WIDTH/9)*8,0,WIDTH/9,HEIGHT);
    	Rectangle top = new Rectangle(0,0,WIDTH,HEIGHT/9);
    	Rectangle bottom = new Rectangle(0, (HEIGHT/9)*8,WIDTH,HEIGHT/9);
    	Rectangle center = new Rectangle((int)((WIDTH/9) *2.5), (int)((HEIGHT/9)*2.5), (int)		((WIDTH/9)*5),
    		(HEIGHT/9)*4);
    	Rectangle obstacle = new Rectangle(WIDTH/2, (int)((HEIGHT/9)*7),WIDTH/10,
    		HEIGHT/9);
    	Rectangle obstacle2 = new Rectangle(WIDTH/3, (int)((HEIGHT/9)*5),
    		WIDTH/10,HEIGHT/4);
    	Rectangle obstacle3 = new Rectangle(2*(WIDTH/3), (int)((HEIGHT/9)*5),
    		WIDTH/10,HEIGHT/4);
    	Rectangle obstacle4 = new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
    	Rectangle obstacle5 = new Rectangle(WIDTH/2, (int)((HEIGHT/9)*1.5),
    		WIDTH/30,HEIGHT/4);
     
    	Rectangle finish = new Rectangle(WIDTH/9, (HEIGHT/2) -HEIGHT/9, 
    		(int)((WIDTH/9)*1.5),HEIGHT/70);
     
    	Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);
    	Rectangle p2 = new Rectangle(((WIDTH/9)+ ((int)((WIDTH/9)*1.5)/2)), (HEIGHT/2)+
    		(HEIGHT/10), WIDTH/30,WIDTH/30);
     
    	public GameRace()
    	{
     
    		super("Radical Racing");
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
     
     
    	JOptionPane.showMessageDialog(null, "Welcome To Radical Racing!\n\n"+"Game: 2 player oponent\n"+
    			"Complete 3 full laps before your oponent!\n"+"Controls:\n"+"Player1(BLUE CAR):\n"+"WASD letters, speed is automatic				\n"+"Player2(RED CAR):\n"+"IJKL letters, speed is automatic\n"+"Click OK to start"); 
     
     
    		Move1 m1 = new Move1();
    		Move2 m2 = new Move2();
    		m1.start();
    		m2.start();
    	}
     
    	public void paint(Graphics g)
    	{
     
    		super.paint(g);
     
    		g.setColor(Color.DARK_GRAY);
    		g.fillRect(0,0,WIDTH,HEIGHT);
     
    		g.setColor(Color.GREEN);
     
    		Rectangle lineO = new Rectangle(WIDTH/9,HEIGHT/2, (int)((WIDTH/9)*1.5)/2,HEIGHT/140);
    		Rectangle lineI = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+
    			(HEIGHT/10), (int)((WIDTH/9)*1.5)/2,HEIGHT/140);
     
    		g.fillRect(left.x,left.y,left.width,left.height);
    		g.fillRect(right.x,right.y,right.width,right.height);
    		g.fillRect(top.x,top.y,top.width,top.height);
    		g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
    		g.fillRect(center.x,center.y,center.width,center.height);
    		g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
    		g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
    		g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
    		g.fillRect(obstacle4.x,obstacle4.y,obstacle4.width,obstacle4.height);
    		g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
    		g.setColor(Color.BLUE);
    		g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
    		g.setColor(Color.RED);
    		g.fill3DRect(p2.x,p2.y,p2.width,p2.height,true);
     
     
    		g.setColor(Color.WHITE);
    		g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
    		g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
     
    		g.setColor(Color.YELLOW);
    		g.fillRect(finish.x,finish.y,finish.width,finish.height);
     
     
     
     
     
     
    	}
     
    	private class Move1 extends Thread implements KeyListener
    	{
    		public void run()
    		{
    			addKeyListener(this);
    			while(true) {
     
    				try {
     
     
    					repaint();
     
    					if(p1.intersects(left) || p1.intersects(right) || 
    					     p1.intersects(top) || p1.intersects(bottom) ||
    					     p1.intersects(obstacle) || p1.intersects(obstacle2) ||
    					     p1.intersects(p2) || p1.intersects(obstacle3) ||
    					     p1.intersects(obstacle4) || p1.intersects(obstacle5))
    					{
    					p1Speed = -19;
    					}
     
    					if(p1.intersects(center))
    					{
    					p1Speed = -19;
    					}
    					if(p1.intersects(finish)&&p1Direction==UP)					{
    					p1Laps++;
    					}
    					if(p1Laps>=24)
    					{
    					if(!winnerChosen)
    					{
    						winnerChosen = true;JOptionPane.showMessageDialog(null, "Player 1Wins!!!");
    						break;
    					}
    					else	
    					{
    						JOptionPane.showMessageDialog(null, "Player 1 Looser...");
    						break;
    					}
    					}
    					if(p1Speed<5)
    					{
    					p1Speed+=20;
    					}
     
    					if(p1Direction==UP)
    					{
    					p1.y-=(int)p1Speed;
    					}
    					if(p1Direction==DOWN)
    					{
    					p1.y+=(int)p1Speed;
    					}
    					if(p1Direction==LEFT)
    					{
    					p1.x-=(int)p1Speed;
    					}
    					if(p1Direction==RIGHT)
    					{	
    					p1.x+=(int)p1Speed;
    					}
     
    						Thread.sleep(75);
    					}
    					catch(Exception e) {
     
     
    						break;
    					}
    				}
    			}
     
     
    	public void keyPressed(KeyEvent event)
    	{
    	}
     
    	public void keyReleased(KeyEvent event)
    	{
    	}
     
    	public void keyTyped(KeyEvent event)
    	{
     
    		if(event.getKeyChar()=='a')
    		{
    			p1Direction = LEFT;
    		}
     
    		if(event.getKeyChar()=='s')
    		{
    			p1Direction = DOWN;
    		}
     
    		if(event.getKeyChar()=='d')
    		{
    			p1Direction = RIGHT;
    		}
     
    		if(event.getKeyChar()=='w')
    		{
    			p1Direction = UP;
    		}
    	}
    }
     
    	private class Move2 extends Thread implements KeyListener
    	{
    		public void run()
    		{
    			addKeyListener(this);
    			while(true) {
     
     
    				try {
     
     
    					repaint();
     
    					if(p2.intersects(left) || p2.intersects(right) || 
    					     p2.intersects(top) || p2.intersects(bottom) ||
    					     p2.intersects(obstacle) || p2.intersects(obstacle2) ||
    					     p1.intersects(p2) || p2.intersects(obstacle4) ||
    					     p2.intersects(obstacle3) || p2.intersects(obstacle5))
     
    					{
    					p2Speed = -19;
    					}
     
    					if(p2.intersects(center))
    					{
    					p2Speed = -19;
    					}
    					if(p2.intersects(finish)&&p2Direction==UP)
    					{
    					p2Laps++;
    					}
    					if(p2Laps>=24)
    					{
    					if(!winnerChosen)
    					{
    						winnerChosen = true;
    						JOptionPane.showMessageDialog(null, ",Player 2 WINS!!!");
    						break;
    					}
    					else
    					{
    						JOptionPane.showMessageDialog(null, "Player 2: LOOSER\n"+"Player 1:WINNER");
    						break;
    					}
    					}
     
     
     
     
     
     
     
    					if(p2Speed<=5)
    					{
    					p2Speed+=20;
    					}
    					if(p2Direction==UP)
    					{
    					p2.y-=(int)p2Speed;
    					}
    					if(p2Direction==DOWN)
    					{
    					p2.y+=(int)p2Speed;
    					}
    					if(p2Direction==LEFT)
    					{
    					p2.x-=(int)p2Speed;
    					}
    					if(p2Direction==RIGHT)
    					{	
    					p2.x+=(int)p2Speed;
    					}
     
    						Thread.sleep(75);
    					}
    					catch(Exception e)
    					{
     
    						break;
    					}
    				}
    			}
     
    	public void keyPressed(KeyEvent event)
    	{
    	}
     
    	public void keyReleased(KeyEvent event)
    	{
    	}
     
    	public void keyTyped(KeyEvent event)
    	{
     
    		if(event.getKeyChar()=='j')
    		{
    			p2Direction = LEFT;
    		}
     
    		if(event.getKeyChar()=='k')
    		{
    			p2Direction = DOWN;
    		}
     
    		if(event.getKeyChar()=='l')
    		{
    			p2Direction = RIGHT;
    		}
     
    		if(event.getKeyChar()=='i')
    		{
    			p2Direction = UP;
    		}
    	}
    }
     
    	public static void main(String args[]) {
     
    		new GameRace();
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    i want the picture to take place for my "square cars".
    I assume you mean you want to replace the calls to fillRect() with a call to drawImage()
    Read the image files into an image (see the ImageIO class) and use the drawImage method

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    yeah i already tried different ways with the ImageIO tool and none have worked culd u help me?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    Post what you have tried. And the error messages you had.

    Please edit your code and wrap it in code tags to preserve the formatting.
    See: BB Code List - Java Programming Forums
    Or use Go Advanced and use the # icon button above to right.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    Image img=null;
    try
    {
    img=ImageIO.read(new File("Leftp2.png");
    catch(IOException e)

    }

    if(p2Direction==LEFT)
    g.drawImage(img,p2.x,p2.y,this)


    the error is:



    Multiple markers at this line
    - Syntax error on token(s), misplaced
    construct(s)
    - Syntax error, insert "}" to complete Block
    - ImageIO cannot be resolved

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    g.drawImage(img,p2.x,p2.y,this);

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    You must have a catch with the try:
    try{ YOUR CODE HERE }catch(Exception x) {x.printStackTrace();}

  8. #8
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    i do, dont i?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    Not correctly coded. Look at the {}s

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    yeah u were right

    i have this now

    Image img=null;
    try
    {
    img=ImageIO.read(new File("Leftp2.png"));

    }
    catch(Exception x)
    {x.printStackTrace();}

    if(p2Direction==LEFT)
    g.drawImage(img,p2.x,p2.y,this);

    but its still gives me this error:

    ImageIO cannot be
    resolved any help? thanks

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    Did you add the import for that class?

  12. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    help please?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    Please explain the problem

  14. #14
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i add an image into my source code?

    i guess i have a lot of the needed ones

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How can i add an image into my source code?

    Read the API doc for the ImageIO class and see what package it is in. It will be java. something.

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 12:49 PM
  2. Getting Image reader to change source
    By hafunui in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 26th, 2011, 10:45 AM
  3. source code for CD BURNER
    By zeerussia in forum Java Theory & Questions
    Replies: 0
    Last Post: October 23rd, 2010, 02:23 AM
  4. Implementing HTML tags in Java Source Code
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: March 19th, 2010, 09:29 PM
  5. How can i add a new count to this source code ?
    By mm2236 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 30th, 2010, 10:21 PM