Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,466 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,706 people online right now. Registration is fast and FREE... Join Now!




Third "Graphics g" question

 

Third "Graphics g" question

agentkirb

1 Jul, 2009 - 08:26 AM
Post #1

D.I.C Head
**

Joined: 28 Dec, 2008
Posts: 164



Thanked: 1 times
My Contributions
I've asked two questions based on this already, but this "graphics g" thing is still killing me. Here's the code:

CODE



package ancestortree;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
/**
*
* @author jachambe
*/
public class Drawing extends JFrame{
        private int max_h;
        public int getMax()
        {
            return max_h;
        }//end getMax
        public Drawing()
         {
            
         //method to draw the diagram
         super( "Ancestral Tree" );
         Graphics g;//variable g is not used
               BinaryNode node_start=new BinaryNode();
         int draw_width_end=1000;
         int draw_width_start=0;
         int draw_height_end=1000;
         int draw_height_start=30;
         setSize(draw_width_end, draw_height_end );
         setBackground(Color.white);
         draw(node_start,draw_width_start,draw_height_start,draw_width_end,/g);//variable g might not have been initialized

         show();
         }//public Shapes

      
         public void draw(BinaryNode node_start,int draw_width_start,int draw_width_end,int draw_height_start,Graphics g)
         {//method to draw entire drawing
         //start at root node
            
             int ws,we,hs,h;
             int height_item=60;
             int width_item=100;
             ws=draw_width_start;
             we=draw_width_end;
             hs=draw_height_start;
             h=(hs-30)/100;//current height
             if(h>max_h) max_h=h;
             //super.paint(g);     // that will repaint the background

         Graphics2D g2d = ( Graphics2D ) g;
         g2d.setPaint( Color.black);
         g2d.setStroke( new BasicStroke( 6.0f ) );
         g2d.draw( new Ellipse2D.Double( (we-ws)/2-width_item/2, hs, width_item, height_item ) );
             //display info, create label and ellipse

        
             if(node_start.hasLeftChild())draw(node_start.getLeftChild(),ws,(we-ws)/2+ws,hs+100,g);
             //if it exists node_start.hasleftchild
             if(node_start.hasRightChild())draw(node_start.getRightChild(),(we-ws)/2+ws,we,hs+100,g);
             //if it exists node_start.hasrightchild
         }//end draw

         /*
          public void paint( Graphics g )//eventually put this in draw function
         {
        super.paint(g);     // that will repaint the background
         // create 2D by casting g to Graphics2D
         Graphics2D g2d = ( Graphics2D ) g;

         // draw 2D ellipse filled with a blue-yellow gradient
         g2d.setPaint( Color.black);
         g2d.setStroke( new BasicStroke( 6.0f ) );
         g2d.draw( new Ellipse2D.Double( 720/2-100/2, 50, 100, 60 ) );
         }//end paint
         */

}//end class


The issue is specifically here:

CODE

        public Drawing()
         {
            
         //method to draw the diagram
         super( "Ancestral Tree" );
         Graphics g;//variable g is not used warning
               BinaryNode node_start=new BinaryNode();
         int draw_width_end=1000;
         int draw_width_start=0;
         int draw_height_end=1000;
         int draw_height_start=30;
         setSize(draw_width_end, draw_height_end );
         setBackground(Color.white);
         draw(node_start,draw_width_start,draw_height_start,draw_width_end,g);//variable g might not have been initialized

         show();
         }//public Shapes


In the drawing constructor. I put comments by what where the error was with what the error said. I'm completely stumped here. I've tried different things, including setting Graphics g as an argument for the drawing default constructor. But then its not a default constructor anymore and when I create a new "drawing" object, it gives me an error.

If it was just one call to the function I think I could do it like the "paint" function I have commented out. But I want to recursively call the function multiple times because the position of one ellipse depends on the position of the root node before it.

User is offlineProfile CardPM
+Quote Post


Fuzzyness

RE: Third "Graphics G" Question

1 Jul, 2009 - 08:37 AM
Post #2

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
Have you tried changing it to Graphics g = getGraphics(); I dont see it making a difference because your not using it. but you could atleast assign it a value to shut up the compiler smile.gif
User is offlineProfile CardPM
+Quote Post

NeoTifa

RE: Third "Graphics G" Question

1 Jul, 2009 - 08:40 AM
Post #3

Yay caek! ZOMG!!!
Group Icon

Joined: 24 Sep, 2008
Posts: 6,292



Thanked: 79 times
Dream Kudos: 150
My Contributions
You know, Sun's tutorials are really great for explaining this kind of thing. Since I don't have an IDE here at work, I'm reading through them all (so I don't miss anything) and they are really informative. You should check it out.
User is offlineProfile CardPM
+Quote Post

agentkirb

RE: Third "Graphics G" Question

1 Jul, 2009 - 08:48 AM
Post #4

D.I.C Head
**

Joined: 28 Dec, 2008
Posts: 164



Thanked: 1 times
My Contributions
QUOTE(Fuzzyness @ 1 Jul, 2009 - 08:37 AM) *

Have you tried changing it to Graphics g = getGraphics(); I dont see it making a difference because your not using it. but you could atleast assign it a value to shut up the compiler smile.gif


And we have a winner...

Edit: BTW, this means your solution worked.

QUOTE
You know, Sun's tutorials are really great for explaining this kind of thing. Since I don't have an IDE here at work, I'm reading through them all (so I don't miss anything) and they are really informative. You should check it out.


I'll look into that, thank you.

This post has been edited by agentkirb: 1 Jul, 2009 - 08:48 AM
User is offlineProfile CardPM
+Quote Post

cfoley

RE: Third "Graphics G" Question

1 Jul, 2009 - 11:43 PM
Post #5

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 645



Thanked: 60 times
Dream Kudos: 25
My Contributions
You shouldn't need getGraphics. You should do your painting in the paint() method. If you've done it in the constructor, what happens when you resize the window? Java calls paint and only finds the default provided by JFrame.

You can use other methods to hold the code for actually doing the painting but call them from the paint method.

java
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
drawThis(g2);
draw that(g2);
drawTheOther(g2);
}

private void drawThis(Graphics2D g2) {
....
}

private void drawThat(Graphics2D g2) {
....
}

private void drawTheOther(Graphics2D g2) {
....
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 02:34AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month