简体   繁体   中英

How can i return a value from a void method in java

Ok so i have this code and obviously the "g = gd" from the render method isnt modifying the field value of "g".

How can i make it so that the render method modifies the field g?

I want to have a graphics field so i can use graphics to print strings outside the render method but i really have no idea how to do that.

private Graphics g;

private BufferedImage background;

public Tutorial(Core core){
    background = core.getResources().getImage(4);
}

public void render(Graphics gd) {
    g = gd;
    g.drawImage(background, 0, 0, null);
}

EDIT:

ok this is the whole class how can i modify it so that the displayMessage will work ? tick() is called before render() , that might be a problem?

package com.andrewxd.spaceinvaders.levels;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.andrewxd.spaceinvaders.main.Core;

public class Tutorial implements Levels{

    private Graphics g;

    private BufferedImage background;

    public Tutorial(Core core){
        background = core.getResources().getImage(4);
    }

    public void render(Graphics gd) {
        g = gd;
        g.drawImage(background, 0, 0, null);
    }

    public void tick() {
        displayMessage("Welcome", 200,200);
    }

    public void displayMessage(String message, int x, int y) {
        g.setFont(new Font("ARIAL", Font.BOLD, 20));
        g.setColor(Color.RED);
        g.drawString(message, x, y);
    }

    public void displayMessage(String message, int x, int y, Font font) {
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString(message, x, y);
    }

    public void displayMessage(String message, int x, int y, Font font, Color color) {
        g.setFont(font);
        g.setColor(color);
        g.drawString(message, x, y);
    }

}
  • You can't return a value from a void method, that's the purpose of void method, it does what it does and finish.

  • Java is pass-by-value, when you pass a reference type, its address won't be changed but its attributes will be effected by the change.

You could try to get the object from a factory class, which you want to modify... But it is not possible to use return ; in a void method...

You should use google and find best practises and patterns for this case ;-)

return if used other than void type, but you can change global variable's data. that way you can achieve it, or else use some other data type...

"void" is used when you don't want your method to return a value.

If you want it to return something then isntead of "void", Put the type you want to return. For example:

public Graphics returnG(){
 Graphics g = new Graphics();
return    g;

}

this would return a Graphics Object named g.

But again, it is not possible for a void method to return something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM