Writing to the JFrame
TicTacToe
I have a program with 5 classes.
(1)Main
(2)Board
(3)TextO
(4)TextX
(5)AI
The program does the following.
main - controls the main functionality. executes constructors and methods of other helper classes.
Board - uses java2d to draw lines on the frame.
Text0 - uses java3D to draw a 3D X
TextX - uses java3D to draw a 3D O
AI - artificial intelegence to play against.
I have been unsuccessful at drawing on the frame in the helper classes that the main class created.
How would I do this?
I passed the JFrame object I created in main to the Board class.How would I draw on that frame after it has been passed?
i wrote multiple classes, then extended my board class that has extended jframe.it has allowed me to alter the frame a little more, but i have been unable to add a swing container to the frame I'm drawing on. is this possible.
You need to give us a bit more detail. We can't see the code on your PC.Are you really using a Frame - or a JFrame, because you should add Swing containers to JFrames like this..jFrame.setContentPane(myMainContainer);Regards,Tim
Sheesh I should read properly before I write - even the title said JFrame.Regards,Tim
i have attempted to setContentPaine, but no luck.answer this...can we use swing gui components on a frame, and draw to that same frame with java3d or 2d?
thanks for answering .i'm at work, and can not post code.i will do it from home, if i can not resolve this w/o posting now.
> can we use swing gui components on a frame, and draw> to that same frame with java3d or 2d?Yes. You need to override the method paintComponent() of your Swing container.Regards,Tim
how would i do this?is there a tutorial that would give me some additional input.and to clearify, you are stating to over ride the paint component, and not the paint(graphics g) method.
Here's an example I found in a posting further down this forum...public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Font messageFont = new Font("Serif", Font.PLAIN, messageSize);
g2.setFont(messageFont);
g2.drawString(messageText, messageX, messageY);
}
so from this post, are you saying i should create the swing container first, then add it to my frame, and then paint the frame with java3d or 2d last?
I just found the following website,
http://leepoint.net/notes-java/30GUI/20graphics/40drawingpanel/10drawingpanel.html
can i stick this method in my drawing class, or should it be a class of it own, or possibly some other configuration?
You subclass the Swing container (JPanel for example) and add your own paintComponent() method to it.And yes, I guess that is your "drawing" class.
i have been reading in my swing book and they state things like.
paintIcon(graphics g).
is that what you mean by overriding the components paint method?
and would that mean the paintComponent() is just psuedo code?
Or do you mean litterally I will need to write a method named paintComponent()?
> Or do you mean litterally I will need to write a> method named paintComponent()?YES - As I shown by me about three posts above, and is shown in the link you quoted.
Have a look at the Sun Javadocs for the classes Graphics, and Graphics2D to see how to draw within the paintComponent() method. The simple example I quoted in reply 9 shows how to draw text, and you can also do lines, rectangles, icons etc .Regards,Tim