Archived

This topic is now archived and is closed to further replies.

How do I draw in JFrame?

This topic is 4944 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello , I know how to draw on applets but now I want to know how to draw at JFrame //i use this at applets public void Paint(Graphics g) { g.drawRect(.... } now how to do the same at an application?

Share this post


Link to post
Share on other sites
It's paint with a lower case p. But yes, that's what you have to do. However, there is a problem in that this also paints "behind" the frame's title bar and edges, that is, the 0,0 position on the Graphics object is at the top left corner of the frame title bar, not of its client area. What I prefer to do is add an object that extends JComponent to the frame and override its paint method, since that avoids the title bar problem.

Example:

import java.awt.*;
import javax.swing.*;

class DrawOnFrame extends JFrame {
public void main(String[] args) {
new DrawOnFrame();
}

public DrawOnFrame() {
setTitle("My Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the program if the frame is closed

getContentPane().add(new DrawPanel(), BorderLayout.CENTER);
pack(); // set frame size based on that of children (i.e. the panel)

setVisible(true); // show the frame

}

public class DrawPanel extends JPanel {
public DrawPanel() {
// In order to make sure we're a certain size, we'll set our min, max and

// preffered sizes here and let the pack() call above resize the frame

Dimension size = new Dimension(800,600);
setPrefferedSize(size);
setMinimumSize(size);
setMaximumSize(size);
}

public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawString("Hello!", 20, 20);
}
}
}


If you want to do animation, you can also do it the same way as you do in an applet.

EDIT: Looks like 2 more people posted the answer while I was editing my post .

[edited by - Matei on May 31, 2004 8:42:12 AM]

Share this post


Link to post
Share on other sites
Actually a jframe has a paint method I believe, i''m not sure, but it should have one, but in any event you wouldnt want to draw directly on the jframe especially if you are going to put buttons and other stuff on it.

But what you would want to do is make a jpanel and and attach it to the jframe. That what you can do you drawing on the panel.

Jpanel has a function
public void paintComponent(Graphics g)
{
}

that works similar to the japplet paint function

the thing is the function only gets called when the jpanel needs repainting in some way. But you can force it to repaint by calling the function repaint();

I recently made an animation using jframes and jlabels. What I did was to create a new Timer and associate it with an actionlistener. Every 10 milli secs or so the timer generates an event and fires the actionlistener and in the actionlistener I would call repaint() which would in turn call paintComponent(). In this way I acheived animation.

One thing you need to remember is that a call to paintComponent should start with super.paintComponent(g);

This make sure that certain important system things get drawn.

like this

public void paintComponent(Graphics g)
{
super.paintComponent(g);
// the rest of you drawing code here
}

of course once you''ve created your jpanel you can add it to your jframe like so
jframe.getContentPane.add(jlabel, BorderLayout.CENTER)

You can read more about these functions that i''ve mention in the java docs

I hope I''ve helped a little

Share this post


Link to post
Share on other sites
You do exactely the same as in an applet:
class someClass extends JFrame
{
public void paint(Graphics g)
{
//draw
}
}

However, the solution above is not very good. This is better:

class myPanel extends JPanel
{
public void paint(Graphics g)
{
//draw
}
}

class someClass extends JFrame
{
setContentPane(new myPanel());
}

Share this post


Link to post
Share on other sites