Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: I cant draw in java

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I cant draw in java

    Hellow i am a new in forum and java codding also.
    I have to make an arraylist wich take circle and square .
    The suser give tha point x,y and colour for the shape but additional cycle gets the radius and the square of the 2 sideσ.
    and draw the cicles and sqares with the colour give user and the x,y in the end inside of every shape write the perimetr and area.

    heare is my code
    import java.awt.Graphics;
     
     
    public class Circle extends Shape{
     
        private double radius;
     
     
        public Circle(double x,double y, double radius){
           setPosition(x,y);
           this.radius=radius;
        }
     
        public Circle(double radius){
        	setPosition(0, 0);
        	this.radius=radius;
        	}
     
     
        	public double area() {
        	return 2 * Math.PI * radius * radius;
        	}
        	public double perimeter(){
        	return 2*3.14*radius;
        	}
        	public String toString()
        	{ return super.toString() + ": Center " +
        	"with Radius = " + radius + " with area : " + this.area(); }
     
     
     
    }

    import java.awt.Graphics;
     
    import javax.swing.JPanel;
     
     
    public class Square extends Shape  {
     
    	private double height, width;
     
    	    public Square(double x,double y, double height,double width){
    	    setPosition(x,y);
    	       this.height=height;
    	       this.width=width;
    	    }
     
     
    	    public Square(double height, double width)
    	    {
    	    super.setPosition(0,0);
    	    this.height=height;
    	    this.width=width;
    	    }
    	    public void setdimensions(double h, double w) {
    	    height = h;
    	    width = w;
    	    }
    	    public double area() {
    	    return height * width;
     
    	    }
    	    public double perimeter()
    	    {return 2*height+2*width;}
    	    public String toString() {
    	    return super.toString() + ": Rectangle(" + height + " x " + width + ")" + " with area : " + this.area();
    	    }
     
     
     
    	    }

    import java.awt.Graphics;
     
    import javax.swing.JPanel;
     
     
    public abstract  class Shape  {
     
    	private double x,y;
     
    	public void setPosition(double x, double y){
    		this.x=x;
    		this.y=y;
     
    	} 
     
     
    	protected double getX() { return x; }
    	protected double getY() { return y; }
    	public abstract double area();
     
    	public abstract double perimeter();
    	public String toString() {
    	return "Shape(" + x + ", " + y + ")";
    	}
     
     
    }

    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class Main extends JPanel{
     
    	static ArrayList<Shape> shape=new ArrayList<Shape>();
     
    	public void init()                                      //called by another class method  
        {  
            JFrame frame = new JFrame("schematics");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     
            Shape myPanel = new Shape();   
            myPanel.setBackground(Color.WHITE);  
            frame.add( myPanel );  
            frame.setSize(1000, 540 );  
            frame.setVisible( true );  
            myPanel.setSize(1000, 500);  
            myPanel.setVisible(true);  
           }  
    	@Override 
    	public void paintComponent(Graphics g) {  
            super.paintComponent(g);  
            g.setColor(Color.BLACK);  
            g.translate(0,500);  
           for(int i = 0; i<shape.size(); i++){       
     
            g.fillOval(shape.get(i).getX(),shape.get(i).getX(), 50, 50);          
                }  
     
     
     
        public static void main( String args[] )  
        {  
        	int epil ;
        	double Rad,Sid,Sid1,X,Y;
     
        	Scanner scan=new Scanner(System.in);
     
        	System.out.println("Give choice : 1 Circle   2 Square ");
        	epil=scan.nextInt();
        	while(epil!=0){
     
        		if(epil==1){
     
        			System.out.println("Give coordinates X and Y");
            		X=scan.nextDouble();
            		Y=scan.nextDouble();
            		System.out.println("Give Radius");
            		Rad=scan.nextDouble();
     
            		Circle cir=new Circle(Rad);
            		cir.setPosition(X,Y);
        			shape.add(cir);	
     
            		//d.createCircle(X,Y,Rad);
        					}
        				if(epil==2){
     
        		  System.out.println("Give coordinates X and Y");
        	      X=scan.nextDouble();
        	      Y=scan.nextDouble();
        	      System.out.println("Give Sides");
        	      Sid=scan.nextDouble();	
        	      Sid1=scan.nextDouble();
     
        	      Square sqr=new Square(Sid,Sid1);
        	      sqr.setPosition(X,Y);
      			  shape.add(sqr);	
     
        	      //d.createCircle(X,Y,Sid);
        	 }
        				System.out.println("Give choice : 1 Circle   2 Square ");
        		    	epil=scan.nextInt();}
     
     
        				System.out.println(shape);
     
     
     
     
        				    // shape.get(k).draw(g);	
     
     
     
     
     
        } 
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I cant draw in java

    Can you explain what the problem is? Does the code compile and execute? What happens?
    If there are errors, please copy and paste the full text here.

    One problem with the code is the class Shape. That is the name of a Java SE interface. You should NOT use Java SE names for your classes. It makes for confusion.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I cant draw in java

    Quote Originally Posted by Norm View Post
    Can you explain what the problem is? Does the code compile and execute? What happens?
    If there are errors, please copy and paste the full text here.

    One problem with the code is the class Shape. That is the name of a Java SE interface. You should NOT use Java SE names for your classes. It makes for confusion.
    everything is working but
    The problem is i can't take and draw the cirle and the squre from array list.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I cant draw in java

    Where does the posted code call the methods that do the drawing? I don't see where that is done.

    Add some println statements to the code where the drawing is done to print out messages when the methods are called, execute the code and see if anything is printed.

    What does the code display when it is executed?

    The code you posted does NOT compile without errors. There is no way to test code that does not compile without errors.

    The code is poorly formatted. There are several }s that are hidden at the end of statements.
    All }s should be on a line by themselves.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I cant draw in java

    here is the code i cant make object shape beacause i abstract class..
    i dont know how to make to work is the method init i have make.
    public class Main extends JPanel{
     
    	static ArrayList<Shape> shape=new ArrayList<Shape>();
     
    	public void init()                                      //called by another class method  
        {  
            JFrame frame = new JFrame("schematics");  
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
     
            Shape myPanel = new Shape();   
            myPanel.setBackground(Color.WHITE);  
            frame.add( myPanel );  
            frame.setSize(1000, 540 );  
            frame.setVisible( true );  
            myPanel.setSize(1000, 500);  
            myPanel.setVisible(true);  
           }  
    	@Override 
    	public void paintComponent(Graphics g) {  
            super.paintComponent(g);  
            g.setColor(Color.BLACK);  
            g.translate(0,500);  
           for(int i = 0; i<shape.size(); i++){       
     
            g.fillOval(shape.get(i).getX(),shape.get(i).getX(), 50, 50);          
                }  
     
     
     
        public static void main( String args[] )  
        {  
        	int epil ;
        	double Rad,Sid,Sid1,X,Y;
     
        	Scanner scan=new Scanner(System.in);
     
        	System.out.println("Give choice : 1 Circle   2 Square ");
        	epil=scan.nextInt();
        	while(epil!=0){
     
        		if(epil==1){
     
        			System.out.println("Give coordinates X and Y");
            		X=scan.nextDouble();
            		Y=scan.nextDouble();
            		System.out.println("Give Radius");
            		Rad=scan.nextDouble();
     
            		Circle cir=new Circle(Rad);
            		cir.setPosition(X,Y);
        			shape.add(cir);	
     
            		//d.createCircle(X,Y,Rad);
        					}
        				if(epil==2){
     
        		  System.out.println("Give coordinates X and Y");
        	      X=scan.nextDouble();
        	      Y=scan.nextDouble();
        	      System.out.println("Give Sides");
        	      Sid=scan.nextDouble();	
        	      Sid1=scan.nextDouble();
     
        	      Square sqr=new Square(Sid,Sid1);
        	      sqr.setPosition(X,Y);
      			  shape.add(sqr);	
     
        	      //d.createCircle(X,Y,Sid);
        	 }
        				System.out.println("Give choice : 1 Circle   2 Square ");
        		    	epil=scan.nextInt();}
     
     
        				System.out.println(shape);
     
     
     
     
        				    // shape.get(k).draw(g);	
     
     
     
     
     
        } 
        }

Similar Threads

  1. How to draw a houses using Java Applets and Awt?
    By Heizzer10 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 31st, 2012, 04:08 AM
  2. Java Code Help on Draw method
    By RADical in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2012, 07:17 PM
  3. Re: Draw labyrinth ( I need help)
    By tubluforu in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2012, 12:08 AM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  5. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM