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.