Use JFrame and a canvas

B

Ben

Hi,
Could anyone please advise me on how to achieve this. At the
moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
Is it possible to draw using awt.graphics in to a sub window inside it?
If so what do I need to look at in order to do this? If not then how
can something similar to this be achieved?

Thanks.

Ben.
 
V

Vova Reznik

Ben said:
Hi,
Could anyone please advise me on how to achieve this. At the
moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
Is it possible to draw using awt.graphics in to a sub window inside it?
If so what do I need to look at in order to do this? If not then how
can something similar to this be achieved?

Thanks.

Ben.
What is sub window?

You may use JPanel and its super JComponent method

public void paintComponent(Graphics g){
// you may want to call super.paintComponent(g)
// because painting in Swing is different than painting in awt
}
 
R

Rhino

Ben said:
Hi,
Could anyone please advise me on how to achieve this. At the
moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
Is it possible to draw using awt.graphics in to a sub window inside it?
If so what do I need to look at in order to do this? If not then how
can something similar to this be achieved?
Are you sure you really want a java.awt.Canvas in the midst of your Swing
controls? It is much more common to see people draw on a JPanel, which is
the Swing equivalent of a Canvas.

That's exactly what I do in one of my applications. It was originally
written for AWT before Swing came along; then I converted the Canvas to a
JPanel. I don't think I changed the drawing code (g.drawLine(),
g.drawOval(), etc. etc.) very much if at all.

If you mix AWT controls with Swing controls in the same GUI you get a lot of
ugliness due to interactions between the heavyweight AWT controls and the
lightweight Swing controls.
 
B

Ben

Sorry, what I mean is that I am tring to create a program. That has a
main window and sub windows inside it. One of which can be drawn on.
Sort of like having multiple documents open is sub windows in microsoft
word.

Thanks.

Ben.
 
B

Ben

Thanks for your replies.

After looking around on the net, I found this.

/** Panel on which to draw two shapes. **/
class ShapesPanel extends JPanel
{
public void paintComponent (Graphics g) {
// First paint background
super.paintComponent (g);

// The context that is passed is actually
// the Graphics2D sub-class of Graphics. So
// we can cast it to Graphics2D and take advantage
// of the additional tools that it has.
Graphics2D g2 = (Graphics2D)g;

// Options on the rendering, i.e. the actual pixel settings
// of the drawing, can be chosen via RenderingHints
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Create the two shapes, a circle and a square.
Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
Shape shape2 = new Rectangle2D.Double (65.0, 35.0, 70.0, 70.0);

// Now draw the shapes.
g2.draw (shape1);
g2.draw (shape2);
} // paintComponent

} // class ShapesPanel

After the frame and panel have been created and made visible. Is it
possible to make the panel created using the above function redraw?
For example if the shapes specified in changed every second so. Could
I have another function that kept telling the panel to redraw. Is
this possible? If so how?

Thanks.

Ben.
 
V

Vova Reznik

Ben said:
Thanks for your replies.

After looking around on the net, I found this.

/** Panel on which to draw two shapes. **/
class ShapesPanel extends JPanel
{
public void paintComponent (Graphics g) {
// First paint background
super.paintComponent (g);

// The context that is passed is actually
// the Graphics2D sub-class of Graphics. So
// we can cast it to Graphics2D and take advantage
// of the additional tools that it has.
Graphics2D g2 = (Graphics2D)g;

// Options on the rendering, i.e. the actual pixel settings
// of the drawing, can be chosen via RenderingHints
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Create the two shapes, a circle and a square.
Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
Shape shape2 = new Rectangle2D.Double (65.0, 35.0, 70.0, 70.0);

// Now draw the shapes.
g2.draw (shape1);
g2.draw (shape2);
} // paintComponent

} // class ShapesPanel

After the frame and panel have been created and made visible. Is it
possible to make the panel created using the above function redraw?
For example if the shapes specified in changed every second so. Could
I have another function that kept telling the panel to redraw. Is
this possible? If so how?

Thanks.

Ben.
repaint() will call paintComponent(g).
So replace your numbers
Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
with variables
Shape shape1 = new Ellipse2D.Double (?, ?, ?, ?);

update variables and call yourPanel.update()
 
V

Vova Reznik

Vova said:
repaint() will call paintComponent(g).
So replace your numbers
Shape shape1 = new Ellipse2D.Double (40.0, 60.0, 70.0, 70.);
with variables
Shape shape1 = new Ellipse2D.Double (?, ?, ?, ?);

update variables and call yourPanel.update()

Ups, repaint(), not update().
Sorry (
 
R

Roedy Green

Could anyone please advise me on how to achieve this. At the
moment I have a JFrame with a number of JButtons, JPanels and a JMenu.
Is it possible to draw using awt.graphics in to a sub window inside it?
If so what do I need to look at in order to do this? If not then how
can something similar to this be achieved?

To custom paint for JFrames, paint on a JPanel and put your code in
paintComponent instead of paint.
 
B

Ben

Hi,

I have now created a class that extends JPanel and I have my own code
in the paintcomponent that paints to a panel (similar layout of code to
above). The class that initialises the JFrame and JPanel runs
repaint() every second and updates the panel just fine.
However when I try to use a layout manager on the frame.
Everything appears but the panel doesn't get drawn. After playing
around I used the following code to add the panel and a button (cp is
the content pane of the frame).

cp.add(button1, BorderLayout.SOUTH);
cp.add(panel1, BorderLayout.NORTH);

When it runs I noticed that you can just see the top of one of the
circles that gets drawn on the frame. It is at the top of the panel.
This makes me think something is getting painted over the top of my
frame.

Does anyone have any suggestion of what I might be doing wrong?

Thanks.

Ben.
 
T

Thomas Weidenfeller

Ben said:
Does anyone have any suggestion of what I might be doing wrong?

You code in paintComponent() (the code you didn't show), makes certain
assumptions which are not true.

Or, the JPanel can't be layed out properly, because it doesn't return
useful size values.

Or ... whatever, we can only guess.

/Thomas
 
V

Vova Reznik

Ben said:
Hi,

I have now created a class that extends JPanel and I have my own code
in the paintcomponent that paints to a panel (similar layout of code to
above). The class that initialises the JFrame and JPanel runs
repaint() every second and updates the panel just fine.
However when I try to use a layout manager on the frame.
Everything appears but the panel doesn't get drawn. After playing
around I used the following code to add the panel and a button (cp is
the content pane of the frame).

cp.add(button1, BorderLayout.SOUTH);
cp.add(panel1, BorderLayout.NORTH);

When it runs I noticed that you can just see the top of one of the
circles that gets drawn on the frame. It is at the top of the panel.
This makes me think something is getting painted over the top of my
frame.

Does anyone have any suggestion of what I might be doing wrong?

Thanks.

Ben.
http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

You may try to put your panel into CENTER or set preferred size
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top