The Artima Developer Community
Sponsored Link

Java Answers Forum
Drawing Oval hierarchy with Graphics g or Graphic2D

19 replies on 2 pages. Most recent reply: Oct 20, 2005 1:21 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 19 replies on 2 pages [ 1 2 | » ]
Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 17, 2005 11:11 AM
Reply to this message Reply
Advertisement
Hi

I'm trying to draw a hierarchy of goals with are Oval shape. Parent goal called G1, and two sub goals called G2 and G3. G2 and G3 might have sub-goal too, but I just want to concentrate on the 3 mentioned.
When the Oval is draw, I'm trying to implements MouseListener such that when any of the Oval are clicked (mouseClicked), then the getToolTipText( ) for that object will be displayed. TipText are description, name and type.

I've tried:

package javaapplication6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;



/**
*
* @author UvohO
*/
public class DrawOval extends JFrame implements MouseListener{
int x, y, width, height, locationX, locationY, x2, y2;
String str = "CG1";
Container con = getContentPane();
OvalDetails det;
JToolTip tip;
/** Creates a new instance of Main */
public DrawOval() {
setTitle("Drawing Goal Hierarchy");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.addMouseListen er(this);
det = new OvalDetails();

}

public void mouseClicked(MouseEvent e){
x = e.getX();
y = e.getY();
if (e.getButton() == MouseEvent.BUTTON1){
width = 70;
height = 35;

}else{
width = 50;
height = 25;
}
repaint();
}


public void mouseEntered(MouseEvent e){
con.setBackground(Color.YELLOW);

}

public void mouseExited(MouseEvent e){
con.setBackground(Color.BLUE);
}

public void mousePressed(MouseEvent e){

}

public void mouseReleased(MouseEvent e){
if(str.equals("CG1")){
// tip.getToolTipText();
// JOptionPane.showMessageDialog(null, "Goal Details is " + det.getToolTipText());
}else{}
System.out.println("Text is: " + det.getToolTipText());
}

public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setBackground(Color.blue);
g2.drawOval(x - width , y - height, width * 2, height * 2);
g2.drawString(str, x, y);
g2.drawLine(x, y, x2, y2);

}

public static void main(String[] args) {
DrawOval oval = new DrawOval();
oval.setSize(250, 150);
oval.setVisible(true);
}

}

package javaapplication6;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
*
* @author UvohO
*/
public class OvalDetails extends JToolTip{
String goal1 = "Details Oval";


/** Creates a new instance of OvalDetails */
public OvalDetails() {
}

public void setToolTipText(String tipText){
goal1 = tipText;
}

public String getToolTipText(){
return goal1;
}

}

All this does it to create an Oval Shape every time the mouse is clickd.

Can this be modified in any way such that when the mouse is click G1 is drawn, then G2 and G3 then the line connecting them as a hierarchy is drawn?

I've just tried something like:

Ellipse2D.Double oval1 = new Ellipse2D.Double(x, y width, height);
Ellipse2D.Double oval2 = new Ellipse2D.Double(x, y width, height);
Ellipse2D.Double oval3 = new Ellipse2D.Double(x, y width, height);
String str = "G1", str1 = "G2" str2 = "G3";

public void paint(Graphics g){
Graphics2D g2 = new (Graphics2D)g;

g2.setColor(Color.blue);
g2.draw(oval1);
g2.drawString(str, x, y);
g2.setColor(Color.red);
g2.draw(oval2);
g2.drawString(str1, x, y);
g2.setColor(Color.blue);
g2.draw(oval3);
g2.drawString(str1, x, y);

Line2D.Double line = new Line2D.Double(x1, y1, x2, y2);

g2.drawLine(line)

}
How do I link the X1, Y1 (Start point) values to the object on top of the hierarchy to the object below X2, Y2 (endpoint)?

Please Help


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 17, 2005 10:58 PM
Reply to this message Reply
1. Reformat your code using the java tags as shown in the "formatting your post" section.

2. What does this mean?
"Can this be modified in any way such that when the mouse is click G1 is drawn, then G2 and G3 then the line connecting them as a hierarchy is drawn?"
"How do I link the X1, Y1 (Start point) values to the object on top of the hierarchy to the object below X2, Y2 (endpoint)?"
I really don't understand the questions.


My approach to do this would be like this
1. create a list of goals (ArrayList or whatever).
2. Every goal has this method
boolean isMouseOver(int x, int y)

3. create a int variable called mouseOverIndex, initialize it with -1
4. On MouseMoved event
4.1 check if there is a ToolTip shown and hide it.
4.2 set mouseOverIndex to -1
5. On MouseClickedEvent
    boolean found = false;
    for (int i = mouseOverIndex+1; !found && i< allGoals.size(); i++) {
        if (allGoals.isMouseOver(mousepos.x, mousepos.y)) {
            found = true;
            mouseOverIndex = i;
        }
    }
    for (int i = 0; !found && i <= mouseOverIndex; i++) {
        if (allGoals.isMouseOver(mousepos.x, mousepos.y)) {
            found = true;
            mouseOverIndex = i;
        }
    }
    if (mouseOverIndex >= 0) {
        //showToolTip (mouseOverIndex);
    } 

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 4:12 AM
Reply to this message Reply
Hi

What this means is that;

I'm trying to draw an Oval called G1 which is the top of a hierarchy and two other Oval G2 and G3 which are sub-Oval to G1.

G1

G2 G3

The Question is, without placing(specifying) the x1, y1, x2, y2 value to draw a line from G2 to G1 and from G3 to G1, can a mouse click achieve this?
In other words, G2 and G3 has to use the x and y position of G1 as a start point of the line.

 g.drawOval(x, y, width, heigth);

g.drawLine(x1, y1, x2, y2);


So, x2 and y2 will use the value x and y values of G1, while x1 and y1 is the staeting point of the line from G2

P.S
g.drawString(str, x, y) is also requested because str can either be G1, G2 or G3

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 4:15 AM
Reply to this message Reply
Thanks, i'll try using the Arraylist for the tooltip text

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 6:13 AM
Reply to this message Reply
I use the ArrayList in case there are several goals positioned one above the other. This way with every click you switch between the goals occupying the same position on the screen.

To draw the lines you must know wich goal depends on wich goal.

I would design the class Goal like
Note: Since you still didn't reformat your code, I still didn't read it. So this is a new approach to solve your problem, not a modification

public class 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 java.awt.geom.Point2D(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
        //draw the title
        if (parent != null) {
            g2.draw(new java.awt.geom.Line2D.Double(position, parent.getPosition()));
        }
    }
    private String title;
    private java.awt.geom.Point2D position;
}

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 6:16 AM
Reply to this message Reply
Re: Hi, I have formatted the code. Thanks

I'm trying to draw a hierarchy of goals with are Oval shape. Parent goal called G1, and two sub goals called G2 and G3. G2 and G3 might have sub-goal too, but I just want to concentrate on the 3 mentioned.
When the Oval is draw, I'm trying to implements MouseListener such that when any of the Oval are clicked (mouseClicked), then the getToolTipText( ) for that object will be displayed. TipText are description, name and type.

I've tried:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 
import java.awt.geom.*; 
 
public class DrawOval extends JFrame implements MouseListener{
int x, y, width, height, locationX, locationY, x2, y2;
String str = "CG1";
Container con = getContentPane();
OvalDetails det;
JToolTip tip;
/** Creates a new instance of Main */
public DrawOval() {
setTitle("Drawing Goal Hierarchy");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.addMouseListen er(this);
det = new OvalDetails();
 
}
 
[java]public void mouseClicked(MouseEvent e){
x = e.getX();
y = e.getY();
if (e.getButton() == MouseEvent.BUTTON1){
width = 70;
height = 35;
 
}else{
width = 50;
height = 25; 
}
repaint();
}
 
 
public void mouseEntered(MouseEvent e){
con.setBackground(Color.YELLOW);
 
}
 
public void mouseExited(MouseEvent e){
con.setBackground(Color.BLUE);
}
 
public void mousePressed(MouseEvent e){
 
}
 
public void mouseReleased(MouseEvent e){
if(str.equals("CG1")){
// tip.getToolTipText();
// JOptionPane.showMessageDialog(null, "Goal Details is " + det.getToolTipText());
}else{}
System.out.println("Text is: " + det.getToolTipText());
}
 
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setBackground(Color.blue);
g2.drawOval(x - width , y - height, width * 2, height * 2);
g2.drawString(str, x, y);
g2.drawLine(x, y, x2, y2);
 
}
 
public static void main(String[] args) {
DrawOval oval = new DrawOval();
oval.setSize(250, 150);
oval.setVisible(true);
}
 
}


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
 
 
public class OvalDetails extends JToolTip{
String goal1 = "Details Oval";
 
public OvalDetails() {
} 
 
[java]public void setToolTipText(String tipText){
goal1 = tipText;
}
 
public String getToolTipText(){
return goal1;
}
 
}



All this does it to create an Oval Shape every time the mouse is clickd.

Can this be modified in any way such that when the mouse is click G1 is drawn, then G2 and G3 then the line connecting them as a hierarchy is drawn?

I've just tried something like:

Ellipse2D.Double oval1 = new Ellipse2D.Double(x, y width, height);
Ellipse2D.Double oval2 = new Ellipse2D.Double(x, y width, height);
Ellipse2D.Double oval3 = new Ellipse2D.Double(x, y width, height);
String str = "G1", str1 = "G2" str2 = "G3";


public void paint(Graphics g){
Graphics2D g2 = new (Graphics2D)g;
 
g2.setColor(Color.blue);
g2.draw(oval1); 
g2.drawString(str, x, y);
g2.setColor(Color.red);
g2.draw(oval2); 
g2.drawString(str1, x, y);
g2.setColor(Color.blue);
g2.draw(oval3); 
g2.drawString(str1, x, y);
 
Line2D.Double line = new Line2D.Double(x1, y1, x2, y2);
 
g2.drawLine(line)
 
}


To draw the link between G1 and G2, and G1 and G3, the drawLine will be used
How do I link the X1, Y1 (Start point of the Line) values to the object on top of the hierarchy(G1) to the object below(G2 & G3) X2, Y2 (endpoint of the Line) using a mouse click.
Then for the Oval using the Ellipse2D, do I use an Arraylist, instead of creating several Oval shapes?



[/java]

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 6:35 AM
Reply to this message Reply
It boggles me how a simple concept such as indentition
seems to be way beyond most peoples intelligence!!

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 7:06 AM
Reply to this message Reply
Formated code would look like this:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class DrawOval extends JFrame implements MouseListener{
    int x, y, width, height, locationX, locationY, x2, y2;
    String str = "CG1";
    Container con = getContentPane();
    OvalDetails det;
    JToolTip tip;
    
    /** Creates a new instance of Main */
    public DrawOval() {
        setTitle("Drawing Goal Hierarchy");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con.addMouseListener(this);
        det = new OvalDetails();
        
    }
    
    public void mouseClicked(MouseEvent e){
        x = e.getX();
        y = e.getY();
        if (e.getButton() == MouseEvent.BUTTON1){
            width = 70;
            height = 35;
            
        }else{
            width = 50;
            height = 25;
        }
        repaint();
    }
    
    
    public void mouseEntered(MouseEvent e){
        con.setBackground(Color.YELLOW);
        
    }
    
    public void mouseExited(MouseEvent e){
        con.setBackground(Color.BLUE);
    }
    
    public void mousePressed(MouseEvent e){
        
    }
    
    public void mouseReleased(MouseEvent e){
        if(str.equals("CG1")){
// tip.getToolTipText();
// JOptionPane.showMessageDialog(null, "Goal Details is " + det.getToolTipText());
        }else{}
        System.out.println("Text is: " + det.getToolTipText());
    }
    
    public void paint(Graphics g){
        Graphics2D g2 = (Graphics2D)g;
        g2.setBackground(Color.blue);
        g2.drawOval(x - width , y - height, width * 2, height * 2);
        g2.drawString(str, x, y);
        g2.drawLine(x, y, x2, y2);
        
    }
    
    public static void main(String[] args) {
        DrawOval oval = new DrawOval();
        oval.setSize(250, 150);
        oval.setVisible(true);
    }
}
 
public class OvalDetails extends JToolTip{
    String goal1 = "Details Oval";
    
    public OvalDetails() {
    }
    
    public void setToolTipText(String tipText){
        goal1 = tipText;
    }
    
    public String getToolTipText(){
        return goal1;
    }
    
}


At the moment on every right click you draw an ellipse and that's it.

As far as I understood you want that on every click an ellipse is created. The first one is independent, every other new ellipse should draw a line to the first one.

Th class I created in my previous posting would be good for storing and drawing the ellipses.

I used "Goal" instead of "OvalDetails", just rename it.
It also needs infos about the tooltext and the isMouseOver method.

Your frame needs:
    ArrayList<OvalDetails> allOvals = new ArrayList<OvalDetails>();
    OvalDetails parentOval = null;


In the MouseClicked event create a new Oval like this:
    OvalDetail newOval = new OvalDetail(String title, double x, double y, parentOval);
    if (parentOval == null)
        parentOval = newOval;


Change the paint method like this:
    public void paint(Graphics g) {
        super.paint();
        Graphics2D g2 = (Graphics2D)g;
        for (OvalDetail od : allOvals) {
            od.draw(g2);
        }
    }


I hope I guessed right what you want to achive or that at least you got some ideas how to do it right.

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 7:30 AM
Reply to this message Reply
Thanks you are a genius. For your previous posting, Goal Class.

The line with: position = new java.awt.geom.Point2D(x, y);

Throws an exception that state, java.awt.geom.Point2D and it can not be instantiated.

I try a different way like
 position = new Point2D(Point2D x, Point2D y); 


And I declared the
 import java.awt.geom.*; 


Then
 private Point2D position; 


But it still won't work.

Thank for your help, it for my thesis, no one know how to use the Graphics thing even the java lecturer.

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 7:41 AM
Reply to this message Reply
Thanks, it is Ok, I forgot to put the .Double at the end of Point2D

Thanks once again

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 7:59 AM
Reply to this message Reply
> Thank for your help, it for my thesis, no one know how to
> use the Graphics thing even the java lecturer.

You're at the upper level of Higher Education and you can't
even indent your code??? Thank heavens I din't pursue the
world of academia any further - lest all my code would
be unreadable and I wouldn't be able to fit into the
professional world after spending all that time getting
that piece of paper that tells me I have the edge,
whilst this forum prooves that its clearly garbage.

There seems to be a *slight* ounce of truth to Kanye's
high school drop out. Thank you folks for continually
adding meaning to Kanye's music.

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 8:07 AM
Reply to this message Reply
haha,
I'm not from a software background, from the Electronics background. The code was not indented because it was pasted to a Word Document for correct, it was originally indented in NetBeans. Then I just copied and posted the word document version.

Thanks for understanding my ideas

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 8:13 AM
Reply to this message Reply
Yeah, Point2D.Double was my fault.

Always keep in mind that this is an object oriented language, so If you have to represent a "thing", create a object with all it's informations.
If this "thing" has to be drawn somewhere, create the draw method in this object.

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 12:36 PM
Reply to this message Reply
Hi
I could not resolve the error to do with the line of code
Goal newGoal = new Goal(String title, double x, double y, parentOval);
in the DrawOval Class

Here is the classes:
package javaapplication1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
 
 
public class DrawOval extends JFrame implements MouseListener{
    Container con = getContentPane();
    ArrayList<Goal> allOvals;
    Goal parentOval;
    
    /** Creates a new instance of Main */
    public DrawOval() {
        setTitle("Drawing Goal Hierarchy");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con.addMouseListener(this);
        allOvals = new ArrayList<Goal>();
        parentOval = new Goal(null);
        
    }
    
    public void mouseClicked(MouseEvent e){
 
        Goal newGoal = new Goal(String title, double x, double y, parentOval);
        if(parentOval == null){
            parentOval = newOval;
        }
    }
    
    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){
        
    }
    
    public void paint (graphics g){
        super.paint();
        Graphics2D g2 = (Graphics2D)g;
        for (Goal goal : allOvals){
            goal.drawGoal(g2);
        }
    }
 
    public static void main(String[] args) { 
        Goal gl = new Goal();
        gl.setSize(250, 150);
        g1.setVisible(true);
    }
    
}


package javaapplication1;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
 
 
public class Goal extends JToolTip{
    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 oval1 = new Ellipse2D.Double(x, y, width, height);
        g2.draw(oval1);
 
        //draw the title
        g2.drawString(title, x, y);
        
        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;
        
    }
       
 
}

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 18, 2005 10:36 PM
Reply to this message Reply
My fault again. Seems yesterday I didn't read my own postings very carefully.

Write
Goal newGoal = new Goal("Tabaluga", e.getX(), e.getY(), parentOval);

Flat View: This topic has 19 replies on 2 pages [ 1  2 | » ]
Topic: Need Help with String Search Previous Topic   Next Topic Topic: How do we induce the locale in to an exception?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use