-
What are you doing...?
Forward ref or not, you are not doing the image load like an applet is
supposed to. Try this.
Code:
/**
* Load image.
* If load fails then set the appropriate display
* message and return null.
*/
private Image loadImage(String imgParamName) throws Exception {
Image img=null;
imgURLStr=getParameter(imgParamName);
if (imgURLStr==null)) {
this.msgStr="parameter ["+imgParamName+"] not defined.";
return null;
}
try {
URL imgURL = new URL(imgURLStr);
img = this.getImage(imgURL);
if (img==null) {
this.msgStr=imgURLStr+" not found.";
return null;
}
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
// the paint job draws this string if no image is present
this.msgStr="loading "+imgURLStr;
this.repaint();
mt.waitForID(0);
imgDim=new Dimension(img.getWidth(this), img.getHeight(this));
if (memG==null) makeImgBuffer();
if (imgDim.width == 0 && imgDim.height == 0)
throw new Exception("Image load failed dim: "+imgDim.toString());
return img;
}
catch (InterruptedException ex) {
this.msgStr="upload image ["+imgURLStr+"] was interrupted.";
return null;
}
catch (MalformedURLException ex) {
this.msgStr=" image URL ["+imgURLStr+"] is invalid.";
return null;
}
catch (IllegalArgumentException ie) {
this.msgStr=imgURLStr+" FAILED";
return null;
}
}
/**
* prepare for double buffered image drawing.
*/
private void makeImgBuffer() {
this.memImg=this.createImage(this.getSize().width,
this.getSize().height);
this.memG=this.memImg.getGraphics();
this.memG.setFont(zoomFnt);
}
Last edited by sjalle; 04-28-2005 at 05:51 PM.
-
this is what I had done
Code:
MediaTracker trk = new MediaTracker(this);
imgBlkFrog = getImage(getCodeBase(),"BlkFrog.gif"); // Variable for the frog image
imgWhtFrog = getImage(getCodeBase(),"WhtFrog.gif"); // Variable for the frog image
imgBoard = getImage(getCodeBase(),"background.gif"); //import background
imgToadstool = getImage(getCodeBase(),"myToadstool.gif"); //import toadstool image
imgCongrats = getImage(getCodeBase(),"Congratulations.gif"); //import toadstool image
trk.addImage(imgBlkFrog, 0);
trk.addImage(imgWhtFrog, 1);
trk.addImage(imgBoard, 2);
trk.addImage(imgToadstool, 3);
trk.addImage(imgCongrats, 4);
try {
trk.waitForAll() ;
} catch (InterruptedException e) {
// Error...
}
and it seems to work okay apart from the fact that the array
Code:
public static int [][] FROG_PARAMS = { // array of co-ordinates and sizes (used in initially making the frogs)
{20, 0, 75, 20 , 0}, //( x co-ord, y co-ord, diameter of holding rect, x co-ord for locator, y co-ord for locator)
{500, 0, 75, 500, 0},
{20, 480, 75, 20, 480},
{500, 480, 75, 500, 480}
};
public Image [] FROG_COLOURS = { // use 2 black frogs and 2 white
imgBlkFrog,
imgBlkFrog,
imgWhtFrog,
imgWhtFrog
};
Seems to be looking for imgBlkFrog and ImgWhtFrog before they have been initialised. Prior to transferring the panel to an applet I was loading the images for the black and white frogs right after the line
public class Frogs extends Applet implements MouseListener, MouseMotionListener {
to overcome the problem I was having with the illegal forward reference in the array to the images, this code all works fine except for the fact that now I have no frogs 
and even worse java console reports no errors, so I cant see whats wrong that way.
Last edited by pwnedjoo; 04-29-2005 at 04:25 AM.
-
Piece of cake
You must do four things:
Declare your FROG_COLORS (or rather []frogColors[]) array like:
Image [] frogColors=null;
Also decalre a boolean to keep track of when the images are ready to be painted.
boolean imagesReady=false;
Then immediately after the statement:
trk.waitForAll();
you do this:
frogColors=new Image[4];
frogColors[0]=imgBlkFrog;
frogColors[1]=imgBlkFrog;
frogColors[2]=imgWhtFrog;
frogColors[3]=imgWhtFrog;
imagesReady=true;
repaint();
In your paint/update you then implement a logic that checks the
imagesReady -flag; if false then just draw a string like:
g.drawString("Images not ready", 20,20);
... that was four things.. wasn't it ?
PS: I feel partly responsible for where you are now, so if you like you could
zip you code and post it, and I could have a good look at it. I promise I will
not butcher it
Last edited by sjalle; 04-29-2005 at 04:33 AM.
-
lol, Sure was, You've had your coffee this morning...I can tell .... thanks by the way.
**EDIT**
That worked a treat,
Thankyou again for all your help.
Last edited by pwnedjoo; 04-29-2005 at 04:44 AM.
-
Ive got a question about the orginal topic, I have gave this puzzle a go and took the same route as pwnj00, here's my code
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class RollThemOut extends Applet implements MouseListener, MouseMotionListener {
public Point WhiteBl1, WhiteBl2, WhiteBl3, WhiteBl4, BlackBl1, BlackBl2, BlackBl3, BlackBl4, mouse;
private int select;
private Image dbImage;
private Graphics screen;
public void init(){
this.addMouseMotionListener(this);
this.addMouseListener(this);
select=0;
setSize(800,300);
WhiteBl1 = new Point(15, 117 );
WhiteBl2 = new Point(60, 117 );
WhiteBl3 = new Point(105, 117 );
WhiteBl4 = new Point(150, 117);
BlackBl1 = new Point (195, 110);
BlackBl2 = new Point (240, 110);
BlackBl3 = new Point (285, 110);
BlackBl4 = new Point (330, 110);
mouse = new Point ();
}
public void paint(Graphics g){
drawBox(g);
g.setColor(Color.red);
g.fillOval(WhiteBl1.x, WhiteBl1.y, 30, 30);
g.fillOval(WhiteBl2.x, WhiteBl2.y, 30, 30);
g.fillOval(WhiteBl3.x, WhiteBl3.y, 30, 30);
g.fillOval(WhiteBl4.x, WhiteBl4.y, 30, 30);
g.setColor(Color.black);
g.fillOval(BlackBl1.x, BlackBl1.y, 45, 45);
g.fillOval(BlackBl2.x, BlackBl2.y, 45, 45);
g.fillOval(BlackBl3.x, BlackBl3.y, 45, 45);
g.fillOval(BlackBl4.x, BlackBl4.y, 45, 45);
g.drawString("Use the mouse to move the marbles", 10, 100);
}
public void mouseDragged(MouseEvent e) {
mouse = e.getPoint();
//continuously change the co-ord's of the selected marble
if(select == 1) WhiteBl1 = mouse;
if(select == 2) WhiteBl2 = mouse;
if(select == 3) WhiteBl3 = mouse;
if(select == 4) WhiteBl4 = mouse;
if(select == 5) BlackBl1 = mouse;
if(select == 6) BlackBl2 = mouse;
if(select == 7) BlackBl3 = mouse;
if(select == 8) BlackBl4 = mouse;
repaint();
}
public void mouseMoved(MouseEvent e) {}
//required for the interface
public void mousePressed(MouseEvent e) { //select a marble using the mouse
mouse = e.getPoint();
if((mouse.x > WhiteBl1.x) && (mouse.x < WhiteBl1.x + 30) && (mouse.y > WhiteBl1.y) && (mouse.y < WhiteBl1.y + 30)) select = 1;
if((mouse.x > WhiteBl2.x) && (mouse.x < WhiteBl2.x + 30) && (mouse.y > WhiteBl2.y) && (mouse.y < WhiteBl2.y + 30)) select = 2;
if((mouse.x > WhiteBl3.x) && (mouse.x < WhiteBl3.x + 30) && (mouse.y > WhiteBl3.y) && (mouse.y < WhiteBl3.y + 30)) select = 3;
if((mouse.x > WhiteBl4.x) && (mouse.x < WhiteBl4.x + 30) && (mouse.y > WhiteBl4.y) && (mouse.y < WhiteBl4.y + 30)) select = 4;
if((mouse.x > BlackBl1.x) && (mouse.x < BlackBl1.x + 30) && (mouse.y > BlackBl1.y) && (mouse.y < BlackBl1.y + 30)) select = 5;
if((mouse.x > BlackBl2.x) && (mouse.x < BlackBl2.x + 30) && (mouse.y > BlackBl2.y) && (mouse.y < BlackBl2.y + 30)) select = 6;
if((mouse.x > BlackBl3.x) && (mouse.x < BlackBl3.x + 30) && (mouse.y > BlackBl3.y) && (mouse.y < BlackBl3.y + 30)) select = 7;
if((mouse.x > BlackBl4.x) && (mouse.x < BlackBl4.x + 30) && (mouse.y > BlackBl4.y) && (mouse.y < BlackBl4.y + 30)) select = 8;
}
public void mouseReleased(MouseEvent e) {
select = 0;
}
// Update - Method, implements double buffering
public void update (Graphics g){
// initialize buffer;
if (dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
screen = dbImage.getGraphics ();
}
// clear screen in background
screen.setColor (getBackground ());
screen.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
screen.setColor (getForeground());
paint (screen);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
//required for interface
public void mouseClicked(MouseEvent event){}
//public void mouseReleased(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
public void drawBox(Graphics g)
{
g.setColor(Color.black);
g.fillRect(10,100,685 ,68);
g.fillOval(410, 55, 65, 65);
g.fillRect(410, 85, 65, 25);
g.fillRect(690, 107, 20, 55);
g.setColor(Color.white);
g.fillRect(15,105,675,58);
g.fillOval(415, 60, 55, 55);
g.fillRect(415, 90, 55, 20);
g.fillRect(690, 112, 30, 45);
}
}
the balls seem to move ok, (the point in Pwnj00's code seemed to be the problem, Pwnj00, your code does work but your pickup point was on the edge of the circle) but I would like to put them in an array to stop them from being able to cross over each other.
sjalle, Ive read your code but thats a bit beyond me, at best i know the basic's, Im guessing I'll need to use boolean on the 2D array. The more I think about this the more I get confused.
-
Why do you make the balls into Points and then (by a truckload of code) threat them
like Rectangles ?. If they are rectangles (or even better: Polygons) you can just
do:
if (aBall.contains(mouseEvent.getPoint()) {
// a hit
}
In one of my previous posts here I store the balls in an array and sort it on x-position
after each move. The sort is there to simplyfy the logic of checking if the ball to
be moved is "locked in". Its a pity that my code is "beyond you", cause I think the
logics for detecting "lockedinness" without an x-pos sorted array willl be a bit
beyond what you like, - very nittygritty.
eschew obfuscation
-
 Originally Posted by sjalle
Why do you make the balls into Points and then (by a truckload of code) threat them
like Rectangles ?. If they are rectangles (or even better: Polygons) you can just
do:
if (aBall.contains(mouseEvent.getPoint()) {
// a hit
}
In one of my previous posts here I store the balls in an array and sort it on x-position
after each move. The sort is there to simplyfy the logic of checking if the ball to
be moved is "locked in". Its a pity that my code is "beyond you", cause I think the
logics for detecting "lockedinness" without an x-pos sorted array willl be a bit
beyond what you like, - very nittygritty.
Its just the way Ive been shown, I guess. This is the most advanced thing Ive tried with java. Theres a lot of stuff in your code that Ive not come across before, and although I'd like to just copy & paste, I'd like to know what Im doing, as their 'd be no point otherwise, I wouldnt really learn anything.
Im Rod, Im a java n00b.
-
Go for it !
eschew obfuscation
-
Right so ive done a bit of further reading on the 2D arrays and how they work, I can accomplish 2D arrays using string and integers, but I cant figure our how I would define the array for graphical object such as the balls, would it be by co-ordinates? and if it is how can you split the tube up into different area's.
Would I be also right in saying I will need to use boolean to check the array to see if a ball can take that position?
-
You are using the Ball class, right ?
Its coded in the 5th post in this thread (page 1). This is basically just a
class for keeping a color, a diameter and a location. and it takes care of
its own rendering.
So, an array of balls would be like:
Code:
Ball [] balls=new Ball[5];
or
Code:
Ball [] balls={
new Ball(100,100,50,Color.blue),
new Ball(150,100,50,Color.green),
etc..
};
An use them like any other class (e.g. in the paint method):
Code:
for (int j=0; j<balls.length; j++) {
balls[j].draw(g);
}
The balls are objects with the capability to make graphics, so the can
probably be calles graphical objects, but that doesn't set
them apart from, say, String objects when it comes to storing them in arrays,
its just objects 
The easiest way to determine if a ball has room to move is to have the
balls in a sorted array (check further up this thread). That way, when
checking that ball[j] can be moved, you just have to check the position of
ball[j-1] and ball[j+1] for room (space).
Splitting the tube up in different areas .... the way you do this depends on
why you do it ... 
If its for being able to push two or more balls at the same time you can use
the sorted array here too; If the client tries to move ball 3 out of six balls
to the rigth (no space between), and there is room to the right of ball #6
then the dragging operation must move balls 3,4,5 & 6. In other words,
the dividing of the tube into different areas is really a question of
grouping the balls into temporary "clusters". For this you could use
an arraylist.
Last edited by sjalle; 05-02-2005 at 08:31 AM.
eschew obfuscation
-
Mmmm, maybe I should just stick with networking, ive been at it a while now, no further forward.
So much to do, so little time.
Thanks for the help anyway SJalle, much appreciated, some people like myself just are'nt meant to program.
-
-
I might come back to it once Ive got more time to put into it. *cough* maybe *cough*
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks