is there a way to add a graphics2d object that alreasy exists to a
jpanel?
I have overridden jpanel as seen below, but cannot see the graphic
object....
any ideaS?
public class JPanelGraphics2d extends JPanel{
private static final long serialVersionUID = 001;
Graphics2D m_g2d;
private int maxUnitIncrement = 1;
JPanelGraphics2d (Graphics2D g){
m_g2d = g;
setPreferredSize(new Dimension(450, 450));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (m_g2d != null){
Graphics2D g2 = (Graphics2D)g;
m_g2d.setBackground(Color.blue);
g2 = m_g2d;
System.out.println("always hit, but never shown on screen");
}
System.out.println("painting jpanelGraphics2d");
}
}
Knute Johnson - 30 Jan 2006 23:11 GMT
> is there a way to add a graphics2d object that alreasy exists to a
> jpanel?
[quoted text clipped - 30 lines]
> }
> }
The Graphics object passed into paintComponent() is a Graphics2D.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JPanel {
public test() {
setPreferredSize(new Dimension(400,300));
}
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLUE);
g2d.fillRect(0,0,getWidth(),getHeight());
g2d.setColor(Color.WHITE);
g2d.drawString("Hello World!",40,150);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new test());
f.pack();
f.setVisible(true);
}
}

Signature
Knute Johnson
email s/nospam/knute/
Thomas Hawtin - 30 Jan 2006 23:15 GMT
> is there a way to add a graphics2d object that alreasy exists to a
> jpanel?
In what sense?
If you want graphics operations to be painted on a Component, use
getGraphics. If you want a single Graphics object to switch between
different AWT supplied Graphics objects, write a Graphics proxy.
> I have overridden jpanel as seen below, but cannot see the graphic
> object....
>
> any ideaS?
>
> public class JPanelGraphics2d extends JPanel{
Not much point in extending JPanel over JComponent.
> private static final long serialVersionUID = 001;
Octal?
> Graphics2D m_g2d;
> private int maxUnitIncrement = 1;
[quoted text clipped - 12 lines]
> m_g2d.setBackground(Color.blue);
> g2 = m_g2d;
You've just assigned a local variable. That wont do much.
> System.out.println("always hit, but never shown on screen");
> }
> System.out.println("painting jpanelGraphics2d");
>
> }
> }
If you want to keep the image produced by a Graphics object, use
BufferedImage. Alternatively, you could create an implementation of
Graphics that records all the operations performed on it, and then play
that back.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Monique Y. Mudama - 31 Jan 2006 00:02 GMT
> is there a way to add a graphics2d object that alreasy exists to a
> jpanel?
[quoted text clipped - 3 lines]
>
> any ideaS?
Either you know about a way to use a Graphics/Graphics2D object that
I've never seen, or you are trying to use a Graphics2D object in a way
that it was never intended to support.
Could you explain what you are trying to accomplish? Maybe there's a
better way to do it.
In the meantime, have you tried removing the paintComponent() method
entirely and just putting this in the constructor:
setBackground(Color.BLUE);
?
> public class JPanelGraphics2d extends JPanel{
>
[quoted text clipped - 22 lines]
> }
> }

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
6e - 31 Jan 2006 02:56 GMT
What Im trying to do in the end is to create a graphic object that can
be inserted into a JScrollPane. I would like to implement it in such a
way so that I can detect the text that is drawn to the screen, and when
the text is clicked on, I want to give the user an opportunity to edit
the text.
The text will appear in both the graphic and a table of information.
So by clicking on the graphic, if I cannot edit the text directly, I
will select the row on the table that the user needs to edit in order
for the editting to be done, while highlighting the text in the
graphic. When the user is done editting the text, I want to refresh
the graphic... If it cannot be editted in real time.
Does that explain it alright?
Andrey Kuznetsov - 31 Jan 2006 11:43 GMT
> What Im trying to do in the end is to create a graphic object that can
> be inserted into a JScrollPane. I would like to implement it in such a
[quoted text clipped - 10 lines]
>
> Does that explain it alright?
I suppose that you have deep misinterpreting of JFC.
Why don't you buy some swing book and read it first?

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Monique Y. Mudama - 31 Jan 2006 16:43 GMT
> What Im trying to do in the end is to create a graphic object that
> can be inserted into a JScrollPane. I would like to implement it in
[quoted text clipped - 10 lines]
>
> Does that explain it alright?
Can you explain what effect you want without referring to any java
components? Like, pretend that you're just a user and you're
describing an application you used.
Something like: "It's a dialog box that presents the user with the
name, address, and date of birth that we have on record for them. If
any of that information is out of date, they can click on the
associated field and edit it, then click on an OK button to save the
changes."
The reason I ask is that I get a really strong vibe that you don't
understand how to use Swing components, but if we had a description
of the end effect you want, maybe we could help you get there.
Is there any reason you can't use a JTextField or JTextArea? Why do
you need to draw anything at all? And why wouldn't you be able to
edit the text directly?

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html