Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help: Drawing Oval Shape from objects

:confused:
Hi

I was trying to draw a parent oval, and three other oval that are children to the parent oval.

The class called Goal is used to declare the format of the new goal, the problem or what I don't understand it; the paint() method in the main class passes the object to the Goal class's drawOval() method, how do I represent that object in the drawOval() method so the g2.draw understand what shape it is my to draw?

I've tried;
Ellipse2D.Double goal1 = new Ellipse2D.Double(400, 100, 120, 80);
g2.draw(goal1);
I know it is not correct, because placed the values there. I need the Ellipse shape to relate to the goal added to the ArrayList()

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Main extends JFrame implements MouseListener{
Container con = getContentPane();
ArrayList allOvals;
Goal parentOval;
String goalName ="CG1";
int x = 500, y = 100;

public Main() {
super.setTitle("Drawing Goal Hierarchy");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.addMouseListener(this);
allOvals = new ArrayList();
parentOval = null;
Goal newGoal = new Goal(goalName, 500, 100, parentOval);
allOvals.add(0, newGoal);
parentOval = newGoal;

}


public void mouseClicked(MouseEvent e){

Goal goal1 = new Goal("Test", x + 500, y + 200); // new goal that is not a parent
//allOvals.add(1, goal1);

repaint();
}

public void mouseEntered(MouseEvent e){


}

public void mouseExited(MouseEvent e){

}

public void mousePressed(MouseEvent e){

}

public void mouseReleased(MouseEvent e){

}

public String getToolTipText(MouseEvent e){
return parentOval.getToolTipText();
}

public void paint (Graphics g){
Goal goal;
Graphics2D g2 = (Graphics2D)g;
for (int i = 0; i < allOvals.size(); i++)
{

goal = (Goal)allOvals.get(i);
goal.drawGoal(g2);
}


}

public static void main(String[] args) {
Main go = new Main();
go.setSize(500, 400);
go.setVisible(true);
}

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;


public class Goal extends JPanel{
private String title;
private Point2D.Double position;
Goal parent;
int x, y , width = 100, height = 60;

private String gName, gType, gLink, gDescription, gFormal, gPattern, gOwner, dCreated, dModfied;

/** Creates a new instance of Goal */
public Goal(String title, double x, double y) {
this(title, x, y, null);

}
public Goal(String title, double x, double y, Goal parent) {
this.title = title;
position = new Point2D.Double(x, y);

this.parent = parent;
}
public void setPosition(double x, double y) {
position.x = x;
position.y = y;

}
public java.awt.geom.Point2D.Double getPosition() {
return (java.awt.geom.Point2D.Double)position.clone();
}
public void drawGoal(java.awt.Graphics2D g2) {
//draw the oval

Ellipse2D.Double goal1 = new Ellipse2D.Double(400, 100, 120, 80);
g2.draw(goal1);


//draw the title

// g2.drawString(title, 960, 540);


if (parent != null) {
g2.draw(new java.awt.geom.Line2D.Double(position, parent.getPosition()));
}
}

public void setToolTipText(String nm, String gt, String gl, String gd, String gf, String gp, String go, String dc, String dm){
this.gName = nm;
this.gType = gt;
this.gLink = gl;
this.gDescription = gd;
this.gFormal = gf;
this.gPattern = gp;
this.gOwner = go;
this.dCreated = dc;
this.dModfied = dm;

}

public String getToolTipText(){
return gName + "\n " + gType + "\n " + gLink + "\n " + gDescription +"\n " +
gFormal + "\n" + gPattern + "\n " + gOwner + "\n " + dCreated + "\n " + dModfied;

}


}
P.S. It wont post like a java code, so no indents are shown
[5416 byte] By [TSquare] at [2007-11-11 7:06:36]