How overloaded paint() works?
Hi, I've just recently started studying Java. While I've been studying c++ for the past few years, there's a project I want to do that I feel Java best suits my needs because all the built in GUI features. I've followed a few tutorials on painting a string to a JFrame and I tried doing this on my own and I apparently didn't learn anything.
I don't understand what is required for drawing a string to a window or jframe anyway. I believe I need to create a container of some sort and draw to that because I believe I read somewhere that you can't draw directly to a JFrame object. If someone could explain the logic behind how it works that would be nice.
Code :
package rago;
import java.awt.*;
import javax.swing.*;
class rago extends JFrame
{
public rago()
{
JFrame f = new JFrame("rago");
f.setSize(600, 600);
f.setVisible(true);
}
public void paint(Graphics g)
{
g.setFont(new Font("Courier New", Font.PLAIN, 25));
g.drawString("Hello World", 250, 250);
}
}
public class Main
{
public static void main(String[] args)
{
rago r = new rago();
}
}
Re: How overloaded paint() works?
Hi maikeru .
you have made some mistakes that the Frame is a container ,you can't directly put something non-component in it.
For your sample,you should first create a panel object that hava override the paint method in order to draw anything you want.I will show you the code i 've tested in my PC
Code :
import java.awt.*;
import javax.swing.*;
class MyPanel extends JPanel{
public void paint(Graphics g){
g.setFont(new Font("Courier New", Font.PLAIN, 25));
g.drawString("Hello World", 250, 250);
}
}
public class rago extends JFrame{
MyPanel panel;
public rago(){
super("rago");
panel=new MyPanel();
this.getContentPane().add(panel,BorderLayout.CENTER);
this.setSize(600,600);
this.setVisible(true);
}
public static void main(String args[]){
rago sample=new rago();
}
}
Re: How overloaded paint() works?
I should apologize for you.I am so dazed
Actually ,you can directly draw in the Frame object.
the code has been amended
Code :
import java.awt.*;
import javax.swing.*;
public class rago extends JFrame{
public rago(){
super("rago");
this.setSize(600,600);
this.setVisible(true);
}
public void paint(Graphics g){
g.setFont(new Font("Courier New", Font.PLAIN, 25));
g.drawString("Hello World", 250, 250);
}
public static void main(String s[]){
new rago();
}
}
Re: How overloaded paint() works?
As pointed out you can draw directly to a JFrame, but its more proper to draw to a JPanel and add it to the JFrame (makes the code not only more readable but more importantly more adaptable if you are adding components to the JFrame). Something else to note when drawing in swing, its better practice to do your custom painting in the paintComponent method rather than the paint method. However you do your painting though, you should ALWAYS call super on the parent function
Code :
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setFont(new Font("Courier New", Font.PLAIN, 25));
g.drawString("Hello World", 250, 250);
}
As for the logic: to draw graphics in swing, all you really need is to over-ride the paintComponent method, call the super method, and then utilize the graphics object passed to it to do the drawing. But if you want more nitty gritty details, see Painting in AWT and Swing
Re: How overloaded paint() works?
Thank you everyone. I finally got it working.
Re: How overloaded paint() works?
Well, I don't know what I'm doing wrong, but it seems no matter what, I can't do anything from scratch after reading on something. I can't even get a simple image to draw again. You don't have to bother explaining, I just find it odd and I apparently have no understanding whatsoever of how Java works. I think it's because so much stuff is hidden in Java as compared to c++. I followed the tutorial given by Sun themselves and it won't work. I'll probably take a vent break and start over as if I know nothing. Thanks for your help anyway, though.
*edit*
I think it has something to do with Windows 7. I switched over to my Vista installation and the code works fine.