Gaston7eze
2012-01-05 14:09:37
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!
Thank you very much!
2012-01-05 14:09:37
2012-01-05 14:15:44
2012-01-05 14:42:21
g.setFont(Font f); Font f = new Font(String name, int style, int size); g.setFont(new Font(null, Font.PLAIN, 18));2012-01-05 18:04:42
2012-01-05 18:13:52
2012-01-05 18:15:14
2012-01-05 18:18:13
public void draw(Graphics g){
g.setFont(new Font(null, Font.PLAIN, 18));
g.drawString("Hello world!", 0, 0);
}public void main(String[] args){
JFrame j = new JFrame("Frame");
f.setSize(50,50);
Graphics g = j.getGraphics();
draw(g);
}2012-01-05 18:21:49
g.drawString("TEXT", x, y)2012-01-05 18:24:29
2012-01-05 18:31:41
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);
}
}
2012-01-05 18:39:20
From: Richard
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);
}
}
2012-01-05 18:46:24
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);
}
}
2012-01-06 11:24:25
From: Richard
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);
}
}