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

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 19, 2005 5:36 AM
Reply to this message Reply
Advertisement
Please run the code and see the error, it looks very straight forward, but just not working. I like to see what happens when I click, so I can modify it.

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;
    double x, y;
    
    /** Creates a new instance of Main */
    public DrawOval() {
        super.setTitle("Drawing Goal Hierarchy");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        con.addMouseListener(this);
        allOvals = new ArrayList<Goal>();
        parentOval = null; 
       
        
    }
    
    public void mouseClicked(MouseEvent e){
        x = e.getX();
        y = e.getY();
 
        Goal newGoal = new Goal("title", x , y , parentOval);
        if(parentOval == null){
            parentOval = newGoal;
        }
    }
    
    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){
        
        Graphics2D g2 = (Graphics2D)g;
        for (Goal goal : allOvals){
            goal.drawGoal(g2);
        }
    }
 
    public static void main(String[] args) { 
        Goal go = new Goal("CG1", x, y, parentOval);
        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 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 19, 2005 6:45 AM
Reply to this message Reply
Would be easier if you would simply post the error message.

Anyway, I got this error message (and others):
D:\Projekte\Cam Interface\src\DrawOval.java:58: non-static variable x cannot be referenced from a static context
Goal go = new Goal("CG1", x, y, parentOval);


Is this the error wich causes you trouble?

a "static context" is a method or a variable wich can be called without initializing the class.

The main method is such a method: The Java runtime routine simply calls it without creating an instance of DrawOval.

Therefore the variables x and y don't exist yet.

In the main method you should only create a new DrawOval and show it, nothing else.

Create Goals only when clicking with the mouse.

Just as a question: Why do you store x and y in DrawOval? They are only needed when you click or move the mouse and then you can get them from the MouseEvent.

Tony Otite

Posts: 10
Nickname: totite
Registered: Oct, 2005

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 19, 2005 12:18 PM
Reply to this message Reply
OK, I got the class names mixed up

I created the instance of DrawOval in the main method just like you said, and made it visible

The frame showed up, but when I click nothing happens, I output (with
System.out.println
) a String just to make sure something happens when I click. The test string shows up, but not the oval shape.

Does the Oval shape show up with your system?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 19, 2005 10:40 PM
Reply to this message Reply
You didn't add the new goals (newGoal) to the list of goals (allGoals).

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Drawing Oval hierarchy with Graphics g or Graphic2D Posted: Oct 20, 2005 1:21 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
How do the above rantings relate to your capabilities of
indenting your code???

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