3 Replies - 630 Views - Last Post: 29 January 2010 - 02:23 AM Rate Topic: -----

#1 josemayi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 25-January 10

Problem with getting image width

Post icon  Posted 26 January 2010 - 11:17 AM

Alright so I have this applet that is supposed to print some pictures on the screen. Since the program is an applet, the code refers to the html <param> tag to get the desired image. The program is also supposed to make the images move when the user clicks and drags the mouse on the applet. The program works, except that I need to get the width in order to reset the images's positions(the program is supposed to give the user a 360 degrees view). I created a class called UI.class and a class called Mouse.class. The UI class is the one that handles all the UI things and the mouse class handles all the mouse input. I need to get the pictures width from the pictures in UI.class in order to do reset the positions in mouse.class. I think the problem is that since the <apple></applet> class refers to UI.class, when I try to use that value in mouse.class it doesn't work because it can't find it...I USED TO HAVE THE MOUSEDRAGGED() IN THE UI CLASS AND IT WORKED.....I THINK ITS GIVING A NULL EXCEPTION POINTER.....here is part of the code I have...

//UI.class(Part)
 public class UI extends JApplet
{
	private Image Center;
	private Image OneX;
	private Image TwoX;
	

	Image scrnBuf;
	Graphics scrnG;


	public void init()
   {
		Center = getImage(getCodeBase(), getParameter("Center"));
		OneX = getImage(getCodeBase(), getParameter("1X"));
		TwoX = getImage(getCodeBase(), getParameter("2X"));
   } 
   public void Update(Graphics screen)
  {
		paint(screen);
  }
  public void paint(Graphics screen)
  {
	
	   scrnG.drawImage(Center, mouse.getCenterPos("X"), mouse.getCenterPos("Y"), this);
	   scrnG.drawImage(OneX, mouse.getOnePos("X"), mouse.getOnePos("Y"), this);
	   scrnG.drawImage(TwoX, mouse.getTwoPos("X"), mouse.getTwoPos("Y"), this);

	   screen.drawImage(scrnBuf, 0 , 0 , this);
   }
				 public int getCenterWidth()
	{
		return Center.getWidth(this);
	}
	public int getOneXWidth()
	{
		return OneX.getWidth(this);
	}
	public int getTwoXWidth()
	{
		return TwoX.getWidth(this);
	}



//Mouse.class(Part)
public class Mouse extends JApplet implements MouseListener, MouseMotionListener
{

	//gets the current mouse position
	private int MouseX;
	private int MouseY;


				   //for images speed
					private int CenterX;
		private int CenterSpeedX;
					private int OneXSpeedHor;

				  //for images positions
		private int OneXX,  TwoXX;
					//for mouse old/new positions and speed
				private int mx, newX, mouseSpeedX;

					UI ui; //instance of UI
public void init()
{
CenterX = 0;

OneXX = 800;
TwoXX = 3200;


CenterSpeedX = 1;
		
OneXSpeedHor = 1;
mx = -1;

newX = 0;

mouseSpeedX = 0;

addMouseListener(this);
		addMouseMotionListener(this);
}


//HERE IS WHERE IM HAVING PROBLEMS....WHERE I AM ASKING FOR THE WIDTH OF THE PICTURES.....I USED TO HAVE THIS CODE IN UI.CLASS AND IT WORKED FINE, BUT WHEN I MOVED IT HERE IT GIVES ME A NULLNULL POINTER EXCEPTION FOR EACH WIDTH
public void mouseDragged( MouseEvent e )
	{
		
		
		
		newX = e.getX();
	
		
			
		
		
		if(mx == -1)
		{
			mx = newX;
			return;
		}
		mx-=newX;
		
		mouseSpeedX = mx;
		
		//makes pictures have a negative X speed
		if(mx > 0)
		{
			
			CenterSpeedX += mouseSpeedX;
			
			OneXSpeedHor += mouseSpeedX;
				
			if(CenterSpeedX > 30)
				CenterSpeedX = 30;
			if(OneXSpeedHor > 30)
				OneXSpeedHor = 30;
				
			//accelerates each image
			CenterX -= CenterSpeedX;
			OneXX -= CenterSpeedX;
			TwoXX -= CenterSpeedX;
			ThreeXHor -= CenterSpeedX;
			
			
			
			//make the user "rotate" clockwise in X
			if(CenterX < 0-ui.getCenterWidth())
				CenterX = TwoXX + ui.getTwoXWidth(); 
			
			if(OneXX < 0-ui.getOneXWidth())
				OneXX = CenterX + ui.getCenterWidth();
			
			if(TwoXX < 0-ui.getTwoXWidth())
				TwoXX = OneXX + ui.getOneXWidth();
			
			
			
		}
		
		
		if(mx < 0)
		{
			
			CenterSpeedX -= mouseSpeedX;
			
			OneXSpeedHor -= mouseSpeedX;
				
			if(CenterSpeedX < -30)
				CenterSpeedX = -30;
			if(OneXSpeedHor < -30)
				OneXSpeedHor = -30;
				
			//accelerates each image
			CenterX += CenterSpeedX;
			OneXX += CenterSpeedX;
			TwoXX += CenterSpeedX;
			ThreeXHor += CenterSpeedX;
			
			/*//make the user "rotate" counter-clockwise in X
			if(TwoXX > getWidth())
				TwoXX = CenterX -TwoX.getWidth(this);
			
			if(OneXX > getWidth())
				OneXX = TwoXX - OneX.getWidth(this);
			if(CenterX > getWidth())
				CenterX = OneXX - Center.getWidth(this);
			*/
		
			
			
			
		}

		mx = newX;
		
		e.consume();
	}




ANY HELP WOULD BE GREATLY APPRETIATED!!

This post has been edited by josemayi: 26 January 2010 - 06:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with getting image width

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2813
  • View blog
  • Posts: 11,909
  • Joined: 20-September 08

Re: Problem with getting image width

Posted 27 January 2010 - 08:51 AM

Load your images synchronously and you should be OK:

Center = javax.imageio.ImageIO.read(new URL(getCodeBase(), getParameter("Center")));


Was This Post Helpful? 1
  • +
  • -

#3 josemayi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 25-January 10

Re: Problem with getting image width

Posted 27 January 2010 - 09:37 PM

Thank you for your help g00se! Although it didn't work out that way because it was giving me a compiling error, I found another way. All I did was declare the picture variables as static so that I could be able to use without declare an instance of UI in the mouse class.(I did it this way because I don't have to use UI class anywhere else.)


//UI.class(Part)
static Image Center;
static Image OneX;
static Image TwoX;

private Image scrnBuf;
private Graphics scrnG;
Mouse mouse = new Mouse();

public void init()
{

	 Center = getImage(getCodeBase(), getParameter("Center"));
	 OneX = getImage(getCodeBase(), getParameter("1X"));
	 TwoX = getImage(getCodeBase(), getParameter("2X"));

	 scrnBuf = createImage(800,600);
	 scrnG = scrnBuf.getGraphics();
	 mouse.initMouse();

}
public void Update(Graphics screen)
{
	 paint(screen);
}
public void paint(Graphics screen)
{

		scrnG.drawImage(Center, mouse.getCenterPos("X"),			  mouse.getCenterPos("Y"), this);
		scrnG.drawImage(OneX, mouse.getOnePos("X"), mouse.getOnePos("Y"), this);
		scrnG.drawImage(TwoX, mouse.getTwoPos("X"), mouse.getTwoPos("Y"), this);

		screen.drawImage(scrnBuf, 0 , 0 , this);
}




//mouse.class(part) implements MouseListener and MouseMotionListener

 private int CenterX, CenterY;
 private int CenterSpeedX;
 private int OneXX, OneXY, TwoXX, TwoXY;

public init()
{
	CenterX = 0;
	CenterY = 0;
		
	OneXX = 800;
	OneXY = 0;
		
	TwoXX = 3200;
	TwoXY = 0;
		
	CenterSpeedX = 10;

	mx = -1;
	my = -1;
	newX = 0;
	newY = 0;

	addMouseListener(this);
	addMouseMotionListener(this);
}
public void MouseDragged(MouseEvent e)
{
		newX = e.getX();
	
		
			
		
		
		if(mx == -1)
		{
			mx = newX;
			return;
		}
		mx-=newX;
		

		
		//makes pictures have a negative X speed
		if(mx > 0)
		{				
			//accelerates each image
			CenterX -= CenterSpeedX;
			OneXX -= CenterSpeedX;
			TwoXX -= CenterSpeedX;
			
			
			
			
			/*
				  *HERE IS WHERE I NEEDED TO USE THE WIDTH FROM UI.JAVA
			  *make the user "rotate"Clockwise in X
				  */
			if(CenterX < 0-UI.Center.getWidth(this))
				CenterX = TwoXX + UI.TwoX.getWidth(this); 
			
			if(OneXX < 0-UI.OneX.getWidth(this))
				OneXX = CenterX + UI.Center.getWidth(this);
			
			if(TwoXX < 0-UI.TwoX.getWidth(this))
				TwoXX = OneXX + UI.OneX.getWidth(this);
			
			
			
		}
		
		
		if(mx < 0)
		{
			
		
				
			//accelerates each image
			CenterX += CenterSpeedX;
			OneXX += CenterSpeedX;
			TwoXX += CenterSpeedX;
			
			
				 /*
				  *HERE IS WHERE I NEEDED TO USE THE WIDTH FROM UI.JAVA
			  *make the user "rotate" counter-clockwise in X
				  */
			if(OneXX > 0)
				CenterX = OneXX -UI.Center.getWidth(this);
			
			if(TwoXX > 0)
				OneXX = TwoXX - UI.OneX.getWidth(this);
			if(CenterX > 0)
				TwoXX = CenterX - UI.TwoX.getWidth(this);
		}

		mx = newX;
		
		e.consume();
}



Now all I have to do is encapsulate the code properly by declaring my static variables as private and creating public get functions for each one of them and I'm all set for that part!

This post has been edited by josemayi: 27 January 2010 - 09:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 kevin_k1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-September 09

Re: Problem with getting image width

Posted 29 January 2010 - 02:23 AM

Thank you for this tip!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1