#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Shape disappearance


    Ok.
    Im new in java. i usually work with vb, so i hope my quetion is not too stupid...
    Im trying to make a program thats show some oval on the main form, so i start with a simple form and 1 oval with this code ( working with eclips)
    Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Panel;
    import java.awt.Point;
    
    import javax.swing.JFrame;
    
    
    public class Arc5 extends JFrame {
    Point p;
    int radius;
    Color clr;
    
    Arc5(Point p1,int r1,Color c1){
    	p=p1;
    	radius=r1;
    	clr=c1;
    	
    }
    	public void paint(Graphics g){
    		g.setColor(clr);
    		g.fillOval(p.x, p.y, radius, radius);
    	
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    Arc5 arc=new Arc5(new Point(50,50),50,Color.GREEN);
    arc.setSize(400, 400);
    arc.setVisible(true);
    
    	}
    
    }
    and i get this beautiful oval....

    ....i didnt succeed to upload the picture... try to imagen...


    now im trying to add another oval, and i dont want to use the same paint function of the main class because i want to add it latter in the program.so if i create another object of Arc5 i will get 2 frames.

    so i made a new class like this

    Code:
    class circle extends Panel{
    	
    	Point p;
    	int radius;
    	Color clr;
    
    	circle(Point p1,int r1,Color c1){
    		p=p1;
    		radius=r1;
    		clr=c1;
    		
    	}
    		public void paint(Graphics g){
    			g.setColor(clr);
    			g.drawOval(p.x, p.y, radius, radius);
    		
    		}
    }
    and make a new object in the main function :

    circle cr=new circle(new Point(250,250),100,Color.red);
    cr.setSize(260, 260);
    arc.add(cr);

    and now i can see my new oval but the first oval is gone

    my quetions are :
    1. where is my first green oval went ???
    2. can i add a new circles to the form without extend the Panel class(i tried it and it wont work) ?

    thanks
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    When you place the circle Panel, you are placing somekind of a "layer" onto you frame, wich hide the other circle
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    (Have more time now for better answer)

    I would not extend JPanel for the circle, but instead call the paint in Circle from the paint function in the JFrame/Arc5 class.

    If you want to have several Circle's you can use ArrayList.

    Further comment:
    - You should set default close on the Frame using setDefaultCloseOperation
    - Class name usually start with a capital letter

    If you want, you can try take a look at those classes:

    Arc5.java
    java Code:
     
    import java.awt.Point;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.WindowConstants;
    import java.util.ArrayList;
     
    public class Arc5 extends JFrame {
    	private ArrayList<Circle> circleList;
     
    	Arc5(){
    		circleList = new ArrayList<Circle>();
    	}
     
    	public void paint(Graphics g){
    		for(Circle nextCircle: circleList){
    			nextCircle.paint(g);
    		}
     
    	}
     
    	public void addCirle(Circle newCircle){
    		circleList.add(newCircle);
    	}
     
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		Arc5 arc=new Arc5();
    		arc.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // close the frame correct
    		arc.addCirle(new Circle(new Point(50,50),50,Color.GREEN));
    		arc.addCirle(new Circle(new Point(250,250),100,Color.RED));
    		arc.addCirle(new Circle(new Point(100,100),150,Color.BLUE));
    		arc.setSize(400, 400);
    		arc.setVisible(true);
    	}
     
    }


    Circle.java
    java Code:
     
    import java.awt.Point;
    import java.awt.Color;
    import java.awt.Panel;
    import java.awt.Graphics;
     
     
    class Circle {
     
    	Point p;
    	int radius;
    	Color clr;
     
    	Circle(Point p1,int r1,Color c1){
    		p=p1;
    		radius=r1;
    		clr=c1;
     
    	}
     
    	public void paint(Graphics g){
    		g.setColor(clr);
    		g.drawOval(p.x, p.y, radius, radius);
     
    	}
    }


    There might be something you haven't seen before, but then you have something to look up.

IMN logo majestic logo threadwatch logo seochat tools logo