Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphics in Frame

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
I was working on an applet, and this worked fine... now I've converted it into a Frame, and it's not working quite so well. Here's my main():<br><br><FONT FACE=monospace>MyClass f = new MyClass();<br>f.setSize(270, 314);<br>f.setVisible(true);<br>f.setBackground(Color.blue);<br>Graphics g = f.getGraphics();<br>g.setColor(Color.lightGray);<br>g.fillRect(0, 0, (BoardSize.rectLeft + BoardSize.rectRight), BoardSize.scoreHeight);<br>g.setColor(Color.black);<br>g.fillRect(BoardSize.rectLeft, BoardSize.rectTop, BoardSize.rectRight - BoardSize.rectLeft, BoardSize.rectBottom - BoardSize.rectTop);</font><br><br>Here's the problem. As you can see, basically what I do here is set the size, set visible, set background, and then construct g from f.getGraphics() seeing as how I don't have a Graphics object without my paint() from my applet. Then I change the color and draw a rectangle, then change the color and draw another rectangle (I have a static class that has my window size, just so it's easy to change if I need to).<br><br>The problem is... when I invoke g.setColor(), nothing happens. When I output to the screen, everything is blue. <i>What am I missing?</i><br><br>I know it's not a problem with BoardSize, because I tried filling a rectangle with actual coordinates: g.fillRect(1, 1, 11, 10). That was just a test to see if it would show up... it didn't. I'm guessing I'm missing something rather simple... I tried commenting out the setBackground() to see if the rectangles were showing up behind, but they weren't... so my clue is, f.getGraphics() is not the proper way to construct a graphics object. How do I draw to the screen with a frame?<br><br><br>Thanks all.. I'm just getting back into graphics (never got that involved in the first place, but I'm going to have to learn Swing fast), and I'm really feeling like a newbie all over again. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
Dear Liam,<br><br>I'm not going to test my answer so I could be wrong. It seems that you don't quite have an accurate understanding about how event driven GUI systems control the drawing of the screen output. Each window in it's turn is given the opportunity by the OS to draw it's display.<br><br>&gt; now I've converted it into a Frame<br><br>So I assume that your class 'MyClass' is derived from a java.awt.Frame, which inherits from Component and therefore inherits a paint() method. This is where you should do your painting. Otherwise when the default paint() method is called nothing is painted and you are left with the background color implemented in the default update() method.<br><br>In your current code this line:<br><br>&gt; f.setBackground(Color.blue);<br><br>Is probably why &quot;everything is blue&quot;.<br><br>Hope this helps<br>-pete
 
*smiles* f.setBackground(Color.blue) ... yeah, that does make things blue. I wasn't sure if paint() got called automatically.. apparently it does. I thought that was one of the differences between applet and frame... I guess it's not.<br><br>I know that each window in turn gets its chance to draw its stuff... I just didn't know about paint() getting called automatically, I thought that was an applet-only thing. This is my first application where I'm actually drawing, most of my code is command-line... which is part of the reason I'm writing this program. thanks for the info... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
a few things. First, including a paint() method broke my menubar. second, I'm trying to include panels now. I have a north panel and a center panel- is there any way I can say that my north panel should take up exactly 100 pixels? I gather not. So I'm guessing I have to set layout to null... but I still don't know how to set a panel to 100 pixels in height. Basically, here's what I want for a screen layout:<br><img src= I want to paint inside of the black area... and when I call clearRect(), I want black to show up behind it. Can anybody help me get started with this? Thanks.. Oh, and I'm working with Swing instead of AWT on everything possible, if that makes a difference to anybody (but don't hesitate to suggest anything in any case). Thanks.. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Imotic,<br><br>&gt; First, including a paint() method broke my menubar<br><br>That is interesting... could you elaborate?<br><br>Again, not going to look this up but I think you need to use GridBag to get support for pixel height and width. Then the old AWT method of perfoming what you describe would have you derive you two classes from canvas (or in your case I beleive you should be able to derive from panel if you have to) and add them to the layout where they would do their own painting. Hopefully the paint() method won't break your 'whatever' again.<br><br>Hope this helps<br>-pete
 
akk, canvas... heavyweight implementation. I'm going to try for JPanel... that's what I've been doing so far, with the bottom panel using setBorder() for the blue border...<br><br>as for the paint() breaking my menu bar... it was rather weird. I implemented paint, and it covered my menubar. I emptied out the paint() method so it was just<br>...paint() {<br>// stuff<br>}<br>and it still didn't work. The screen was flooded with (guess what color) Color.blue (my background color). Didn't matter.. was gray without it anyway. I'm not sure, I'm going to try and finish up some stuff and see if I even need a paint() method... who knows. Wish me luck. I'll probably be back with more questions when I'm done setting up these panels. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Liam,<br><br>*** this *****<br>as for the paint() breaking my menu bar... it was rather weird. I implemented paint, and it covered my menubar. I emptied out the paint() method so it was just<br>...paint() {<br>// stuff<br>}<br>and it still didn't work<br><br>****<br><br>is still unclear. Did you create an override of Component.paint? It must look like this:<br><br>Public void paint(Graphics g){<br>}<br><br>However your new approach sounds more reasonable anyway. You don't want to create an interface by painting the whole thing in one canvas! Yuck! It would be better to create distinct components that can paint their portion and perhaps conduct their own state.<br><br>-pete<br>
 
I had <FONT FACE=monospace>public void paint (Graphics g) {</font>. I think the menu bar was thrown into a panel and added to &quot;Center&quot;, so that might be it. :eek:) <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top