Closed Thread Icon

Topic awaiting preservation: Stumped again - Java (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=23910" title="Pages that link to Topic awaiting preservation: Stumped again - Java (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Stumped again - Java <span class="small">(Page 1 of 1)</span>\

 
Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 11-03-2004 23:43

Hey again, I've got another question for y'all. When drawing images, etc. to a GUI, does it matter where your draw method is? Or do you have to do something a little different depending on where it is?

Here's basically what I have:

code:
public class Manager extends Core
{
public static void main(String[] args)
{
//initialize full screen GUI
init(); //in Core
scn = this.getScreenManager(); //from Core

//show splash screen
new Splash(2000).show(); //displays for 2 seconds

//show login menu
Login login = new Login();
login.setScreen(scn);
login.show();
}
}


code:
public class Login extends Menu
{
public Login()
{
super();
}

public void show()
{
//create sprites
//add sprites to an ArrayList (in DrawStuff)

display();
}

public void display()
{
Graphics2D g = getScreen().getGraphics();
draw(g);
g.dispose();
getScreen().update();
}

public void draw(Graphics2D g2d)
{
for(int i=0;i<getInUse().size();i++)
{
Sprite pfft = (Sprite)getInUse().get(i);
AffineTransform trans = g2d.getTransform();
trans.scale(getScale(), getScale());
trans.translate(Math.round(pfft.getX()), Math.round(pfft.getY()));
g2d.drawImage(pfft.getAnimation().getImage(), trans, null);
}
}
}


code:
public class Menu extends DrawStuff
{
//stuff for menus here

public Menu()
{
super();
}
}


code:
public class DrawStuff
{
private ScreenManager screen;
private ArrayList inUse;
private double scale;

public DrawStuff()
{
inUse = new ArrayList(10);
}

public void setScreen(ScreenManager screen)
{
this.screen = screen;
}

public ScreenManager getScreen()
{
return screen;
}

public ArrayList getInUse()
{
return inUse;
}

public void draw(Graphics2D g2d)
{
for(int i=0;i<this.inUse.size();i++)
{
Sprite pfft = (Sprite)this.inUse.get(i);
AffineTransform trans = g2d.getTransform();
trans.scale(this.scale, this.scale);
trans.translate(Math.round(pfft.getX()), Math.round(pfft.getY()));
g2d.drawImage(pfft.getAnimation().getImage(), trans, null);
}
}
}



If I run that the way it is, it works fine and draws the sprites correctly (using the draw method in Login). However, if I comment out that draw method so it uses the one in DrawStuff, all I get is a black screen. The sprites are still there (enough so that I can click on them), but you just can't see them. I've put various printlns in throughout both draw methods, and everything seems to be right (my ArrayList is the right size and contains the right objects, etc).
It seems like it's drawing everything, but then it's drawing the black screen over it all. Any ideas? I've been staring at this for 5-6 hours now and it's turning into one big blur...

Any thoughts are appreciated,
LF

(Edited by Lord_Fukutoku on 11-03-2004 23:44)

Skaarjj
Maniac (V) Mad Scientist

From: :morF
Insane since: May 2000

posted posted 11-04-2004 07:14

From that, and using what little knowledge I have in Java, I'd say it does matter where they are used and how... although I'm not quite sure how. Let's see what a little research turns up.


Justice 4 Pat Richard

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 11-04-2004 15:32

The thing is that it shouldn't mater...

When you try running your the draw method from the super class you might want to specify super.draw(g), I am not sure it will actually make any difference, because the compilers tend to be nice that way, but specifying super is a good method to get into it lets you know exactly what is going on.

Onto separate topics you code seems to complex. I don't know where you are drawing to, how your are drawing, etc. You seem to have a C programming style to your Java code, and a bit of Classitis. You also seem to be overriding a number of methods that already exist.

You have a method for drawing to the screen called "paint" and you have a method for updating this screen called "repaint." From these methods you shouldn't need a draw method or an update method as you seem to be using them. The way you are creating draw methods is not a really good OO method. If you want to extend the way you draw you might think of extending you Graphics2D class and adding the necessary methods there. This keeps the entire process under a similar naming scheme which would make things easier, IMO.

Then again, I could be wrong as this is only a snippet of your code, but when I read the code something just seems fundamentally off, and I can't put my finger on it.

Dan @ Code Town

Lord_Fukutoku
Paranoid (IV) Inmate

From: Back in West Texas... How disappointing
Insane since: Jul 2002

posted posted 11-04-2004 21:19

Skaarjj - Yep, that's the conclusion I came to also. I was hoping I just overlooked something that someone here could point out.

WarMage - I agree, it shouldn't matter, which is why I'm confused.
super.draw(g): Yep, I've been fairly good in this program about specifying super and this (mainly because Eclipse is good about reminding me ), but I left most of them out of this snippet.
complex code and Classitis: This bit of code does seem that way, but I left a lot of stuff out. Once I get past this hurdle, there'll be at least 5-6 classes extending Menu, a couple other classes extending DrawStuff (or possibly just one, with a couple extending it), so Id much rather just have the one draw method in DrawStuff that everything else can use instead of having the exact same method, doing the exact same thing in 8 different classes. There are a few methods up there that aren't actually needed (except to rig it so I can have the draw(g) method in each bottom class), like getInUse() and getScreen(), and display(g) will be where ever the draw(g) method is.
On that topic, should I just put those 4 lines that are in display() in the show() method for each class that needs to draw something, or put them at the beginning of the draw() method and not worry about passing the Graphics2D object to it?
paint and repaint vs. what I have: I have a full screen GUI using active rendering instead of passive, which is why I did what I did. I doubt that's the best way to do it, but using all the examples I could find, this is about what I ended up with. Everything was somewhat similar to:

code:
.
.
.
while(!done)
{
updateSprites();
Graphics2D g = screen.getGraphics();
draw(g);
g.dispose();
screen.update();
}


Then there was the draw() method similar to what I have above, and the update() method in ScreenManager which updates the display using a double buffer.

There is a method to my madness (at least a little bit). Although there might still be something fundamentally wrong somewhere. I'll be the first to admit that my coding skills and habits are nowhere close to perfect, but that's why I'm writing thousands of lines of code for this project. In the end I'm hoping I just might learn something

LF

« BackwardsOnwards »

Show Forum Drop Down Menu