How would i get this work?

Hello

Im making a game, and need some help.

I want to make a program that calls a method from another class, but it wont work (but the compiler gives no error).

This is my code from the class thats calling the method.

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JApplet;

import java.awt.Graphics;

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

publicclass Mainextends JApplet{

Graphics gr;

Maphandler maphandlare =new Maphandler();

publicvoid paint(Graphics g){

g.drawString("this is from main class",50,50);

maphandlare.refresh(gr);

}

And this is from my Maphandler.java

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JApplet;

import java.awt.Graphics;

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

publicclass Maphandlerextends JApplet{

public Graphics gr;

private String currentmap;

public Maphandler(){

}

publicvoid setMap(String map){

}

publicvoid refresh(Graphics gr){

gr.drawString("from Maphandler",10,15);

}

}

The applet only draws the Graphic from the Main.class , how would i do to get it draw from the method (refresh) of the other class too? ( How do i solve my problem)

Please reply

[2853 byte] By [javaguy387a] at [2007-11-27 11:58:32]
# 1

public class Main extends JApplet {

Graphics gr;

Maphandler maphandlare = new Maphandler();

public void paint(Graphics g) {

g.drawString("this is from main class",50,50);

maphandlare.refresh(gr); // gr doesn't reference anything, g is the object that will be displayed

}

You need to pass g, which is a reference to the Graphics object that will actually be drawn. gr is a reference to nothing in the Main class. Get rid of those Graphics class members, they aren't used or needed. Maphandler shouldn't be an applet. If it only draws things on the Graphics context it gets passed, just put it's functionality in a method of the Main class. If it's supposed to be a component that get's added to the applet, then make it a JPanel.

hunter9000a at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...
# 2

So , i cant really paint from the Maphandler class?

If i can plz write me an working example.

javaguy387a at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...
# 3

As far as my knwoledge goes I don't think you can print it by passing graphics argument, but you can get a similar functionality by getting a string of what you want to get printed as below:

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import javax.swing.JApplet;

import java.awt.Graphics;

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Main extends JApplet {

Graphics gr;

Maphandler maphandlare = new Maphandler();

public void paint(Graphics g) {

g.drawString("this is from main class",10,10);

String s = maphandlare.printthis();

g.drawString(s,50,50);

}

}

class Maphandler {

public Maphandler() {}

public String printthis(){

return "from Maphandlers";

}

public void refresh(Graphics g) {

g.drawString("from Maphandler",70,70);

}

}

NOTE: delete your cookies and files in internet options of your IE

java_queena at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...
# 4

> So , i cant really paint from the Maphandler class?

>

> If i can plz write me an working example.

java_queen's right, you can do it the way you are, but it's not the right way. There's no reason for Maphandler to be an applet, it's only purpose is to draw a string on a Graphics. It would be better to either return the string to be drawn to the Main applet like she said (Maphandler would be more of a Model, and Main would be a view to that Model), or if Maphandler doesn't need to be it's own class, just put it's functionality in a method of Main.

As a refinement of java_queen's example:

public class Main extends JApplet {

//Graphics gr; // no need for this

Maphandler maphandlare = new Maphandler();

public void paint(Graphics g) {

g.drawString("this is from main class",10,10);

String s = maphandlare.getTheValue();

g.drawString(s,50,50);

}

}

class Maphandler {

private String theValue = "from Maphandlers";

public Maphandler() {}

public String getTheValue(){

return theValue;

}

/*public void refresh(Graphics g) {// don't need this anymore

g.drawString("from Maphandler",70,70);

}*/

}

hunter9000a at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...
# 5

Ok

Im doing an game that the Maphandler should load the map and then print it. I know i can send over as strings and then print them in Main class but it would be so many strings to send over (about 200).

Maybe ill put the Maphandler as a function in the main class instead. So i can change map from the Main class.

Would that be good?

javaguy387a at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...
# 6

From your previous posts, I think that you may be way over your head in this. You need to learn the basics first by going through a text book and the tutorials. You can't learn java by trial and error.

petes1234a at 2007-7-29 19:20:14 > top of Java-index,Java Essentials,New To Java...