PDA

View Full Version : A g.drawString() example?



Gaston7eze
01-05-2012, 07:09 PM
I cant get my script working.I dont know why.Can you give me an example of a simple script using "g.drawString"?
Thank you very much!

Man16
01-05-2012, 07:15 PM
Oops, just realised its the java section. Umm, depends on what you are doing, if you are coding for something like rsbot's api, you need onRepaint graphics with a parameter of Graphics g.

Example just drawing a string, I cant remember what they stand for, sorry.

g.drawString("v3.0", 378, 470);

Richard
01-05-2012, 07:42 PM
You'll need to set a font for the graphics object, otherwise it'll have nothing to write the string with:

g.setFont(Font f);

And to create a font, it has three parameters: name, style and size:


Font f = new Font(String name, int style, int size);

You can actually combine the two into a single line statement, as below:


g.setFont(new Font(null, Font.PLAIN, 18));

And the above post shows how to actually draw a string.

Note: in these examples it assumes g is an instance of Graphics, or its subclasses

Gaston7eze
01-05-2012, 11:04 PM
So the entire script should be?
I need an example so i can understand what should i do.
Thanks!

Stuart
01-05-2012, 11:13 PM
What are you trying to make your "script" for?

And what graphics object are you trying to drawString onto?

Gaston7eze
01-05-2012, 11:15 PM
Im trying to make it to pop up a window and then write "hello".

Richard
01-05-2012, 11:18 PM
The font is just a variable, so it can either be global, or within the graphic method itself. This is just a small example:


public void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}

If you draw that onto a component, in a main method as follows:


public void main(String[] args){
JFrame j = new JFrame("Frame");
f.setSize(50,50);
Graphics g = j.getGraphics();
draw(g);
}

Don't take my word for that code entirely, I broke my JDK earlier, so can't check...

eXoTiK
01-05-2012, 11:21 PM
g.drawString("TEXT", x, y)

That's what it means.

Gaston7eze
01-05-2012, 11:24 PM
Code:
package java;

import java.awt.Font;
import java.awt.Graphics;

public class applet {
public void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}
}

Got this error :(

Could not find the main class: Frame.Program will exit.

Richard
01-05-2012, 11:31 PM
Try this:


package java;

import java.awt.Font;
import java.awt.Graphics;
javax.swing.JFrame;

public class applet {

public static void main(String[] args){
JFrame j = new JFrame("Frame");
j.setSize(50,50);
Graphics g = j.getGraphics();
draw(g);
}

public void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}
}

Gaston7eze
01-05-2012, 11:39 PM
Try this:


package java;

import java.awt.Font;
import java.awt.Graphics;
javax.swing.JFrame;

public class applet {

public static void main(String[] args){
JFrame j = new JFrame("Frame");
j.setSize(50,50);
Graphics g = j.getGraphics();
draw(g);
}

public void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}
}

Thanks for trying to help but it gives me an error;

Code:

package java;

import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.applet;

public class applet {

public static void main(String[] args){
JFrame j = new JFrame("Frame");
j.setSize(50,50);
Graphics g = j.getGraphics();
draw(g);
}

public static void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}
}


Error: Could not find the main class: java.applet.Program will exit.
Thanks!

Richard
01-05-2012, 11:46 PM
Give me 5 minutes, see if my JDK will be working then :p

EDIT: Right, took my long enough to get my bits sorted. Firstly, you have java as your package name, and that is prohibited because it is one of the main package names that the default java library uses, so name is something different or use the default package.

Secondly, the code. To draw straight onto a JFrame, you need to inherit from the JFrame for the whole class, and then override the paint() method that would normally be used. In the example I've put below, we've simple put a white background on the JFrame, and then added the string. You'd then go through all of the usual bits to show a JFrame, setting the title, size, close operation, etc. Then Making it visible. Finally you have to get the graphics component of the JFrame, and then paint it. Like below:


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

import javax.swing.JFrame;

public class Test extends JFrame{

public static void main(String[] args){
Test t = new Test();
}

public Test(){
setTitle("Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50,50,100,100);
setVisible(true);
Graphics g = getGraphics();
paint(g);
}

@Override
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.setFont(new Font(null, Font.PLAIN, 12));
g.drawString("Hello World!", 50, 50);
}
}

Gaston7eze
01-06-2012, 04:24 PM
Give me 5 minutes, see if my JDK will be working then :p

EDIT: Right, took my long enough to get my bits sorted. Firstly, you have java as your package name, and that is prohibited because it is one of the main package names that the default java library uses, so name is something different or use the default package.

Secondly, the code. To draw straight onto a JFrame, you need to inherit from the JFrame for the whole class, and then override the paint() method that would normally be used. In the example I've put below, we've simple put a white background on the JFrame, and then added the string. You'd then go through all of the usual bits to show a JFrame, setting the title, size, close operation, etc. Then Making it visible. Finally you have to get the graphics component of the JFrame, and then paint it. Like below:


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

import javax.swing.JFrame;

public class Test extends JFrame{

public static void main(String[] args){
Test t = new Test();
}

public Test(){
setTitle("Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50,50,100,100);
setVisible(true);
Graphics g = getGraphics();
paint(g);
}

@Override
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
g.setFont(new Font(null, Font.PLAIN, 12));
g.drawString("Hello World!", 50, 50);
}
}

Thank you very much Richard!Im gonna check it out.