Results 1 to 8 of 8

Thread: java shapes

  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Question java shapes

    i am creating a program that the user chooses a shape and it displays it. the only thing is that i dont know how to draw the shape and clear it when the user chooses it.
    here is the code
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.geom.Ellipse2D;
    
    public class Shapes extends JPanel implements ActionListener, ItemListener {
    	Shape square,rect,circle;
    	
    	 Color squareC = Color.RED;
    	 Color rectC = Color.RED;
    	 Color circleC = Color.RED;
    	 Main mm;
    	
    
    		public Shapes(Main main) {
    			this.mm = main;
    		}
    	 
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()==mm.option1){
    				
    				
    			}else if(e.getSource()==mm.option2) {
    				
    				
    			}else if(e.getSource()==mm.option3) {
    				
    			}
    			
    		}
    	 
    	 
    	 
    	 
    	 
    	
    	public void paintComponent(Graphics grap) {
    		super.paintComponent(grap);
    		this.setBackground(Color.GRAY);
    	
    		 square = new Rectangle(212, 70, 100, 100);
    		 rect = new Rectangle(212, 70, 250, 100);
    		 circle = new Ellipse2D.Float(212, 70, 100, 100);
    		
    	}
    	
    
    
    	@Override
    	public void itemStateChanged(ItemEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	}
    second class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Main extends JFrame {
    	JRadioButton option1 = new JRadioButton("Square", true);
    	 JRadioButton option2 = new JRadioButton("Rectangle");
    	 JRadioButton option3 = new JRadioButton("Circle");
    	
    	 String[] color = {"Red", "Blue", "Green", "Yellow", "Green"};
    	 JComboBox combo = new JComboBox(color);
    	
    	 JCheckBox cb = new JCheckBox("3D");
    	public Main() {
    		super("Shapes");
    		setSize(500,270);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		BorderLayout bor = new BorderLayout(5, 0);
    		setLayout(bor);
    		
    		ButtonGroup bg = new ButtonGroup();
    		bg.add(option1);
    		bg.add(option2);
    		bg.add(option3);
    		
    		
    		Shapes sh = new Shapes(null);
    		sh.setBackground(Color.WHITE);
    		add(sh, BorderLayout.CENTER);
    		
    		JPanel row = new JPanel();
    		row.add(option1);
    		row.add(option2);
    		row.add(option3);
    		
    		row.add(option1);
    		row.add(option2);
    		row.add(option3);
    		
    		row.add(combo);
    		row.add(cb);
    		add(row, BorderLayout.PAGE_END);
    		
    		Shapes sa = new Shapes(this);
    		option1.addActionListener(sa);
    		option2.addActionListener(sa);
    		option3.addActionListener(sa);
    		combo.addItemListener(sa);
    		cb.addItemListener(sa);
    		
    		
    	}
    	
    	public static void main(String[] args) {
    		Main mm = new Main();
    	}
    
    }

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Consider giving Shapes a Shape variable, say called selectedShape, in your ActionListener, have this field refer to one of your pre-made Shapes (the rectangle, square or circle) by assigning the pre-made shape to the variable. Then in your paintComponent method, cast your Graphics object into a Graphics2D object and if selectedShape is not null, use the Graphics2D object to draw the selectedShape.

    Also, don't create new objects in your paintComponent method as this method needs to be lean and mean but instead create these shapes in the Shapes constructor.

  3. #3
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    i dont think i did this right, and it is showing errors too.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.geom.Ellipse2D;
    
    public class Shapes extends JPanel implements ActionListener, ItemListener {
    	Shape square,rect,circle;
    	
    	Shape selctedshape;
    	
    	
    	 Color squareC = Color.RED;
    	 Color rectC = Color.RED;
    	 Color circleC = Color.RED;
    	 Main mm;
    	
    	 
    		public Shapes(Main main) {
    			this.mm = main;
    		}
    	 
    		public void actionPerformed(ActionEvent e) {
    			square = new Rectangle(212, 70, 100, 100);
    			 rect = new Rectangle(212, 70, 250, 100);
    			 circle = new Ellipse2D.Float(212, 70, 100, 100);
    			if(e.getSource()==mm.option1){
    				selctedshape = square;
    			}else if(e.getSource()==mm.option2) {
    				selctedshape = rect;
    			}else if(e.getSource()==mm.option3) {
    				selctedshape = circle;
    			}
    			
    		}
    	 
    	 
    	 
    	 
    	 
    	
    	public void paintComponent(Graphics grap) {
    		Graphics2D g = (Graphics2D) grap;
    		super.paintComponent(grap);
    		this.setBackground(Color.GRAY);
    		if(selctedshape==square) {
    			g.draw(square);
    			
    		}else if(selctedshape==rect){
    			g.draw(rect);
    		}else if(selctedshape==circle) {
    			g.draw(circle);
    		}
    		
    	}
    	
    
    
    	@Override
    	public void itemStateChanged(ItemEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	}

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    If you're seeing errors, then you need to post the errors and indicate which line is causing them.

    Also

    1) the paintComponent doesn't need all of those if blocks. You'll need an if to be sure the selected shape is not null, and then call g.draw(shape), that's it. Polymorphism does the rest.

    2) You don't need recreate the circle, rectangle and square objects in the actionPerformed method. Create them once in the constructor, that's it.

  5. #5
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    when i put the Shapes in the constructor it wasn't showing any errors but it is not showing any shapes when i select them now.

    here is the code
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.geom.Ellipse2D;
    
    public class Shapes extends JPanel implements ActionListener, ItemListener {
    	Shape square = new Rectangle(212, 70, 100, 100);
    	Shape rect = new Rectangle(212, 70, 250, 100);
    	Shape circle = new Ellipse2D.Float(212, 70, 100, 100);
    	
    	Shape selctedshape;
    	
    	
    	 Color squareC = Color.RED;
    	 Color rectC = Color.RED;
    	 Color circleC = Color.RED;
    	 Main mm;
    	
    	 
    		public Shapes(Main main) {
    			this.mm = main;
    		}
    	 
    		public void actionPerformed(ActionEvent e) {
    			if(e.getSource()==mm.option1){
    				selctedshape = square;
    			}else if(e.getSource()==mm.option2) {
    				selctedshape = rect;
    			}else if(e.getSource()==mm.option3) {
    				selctedshape = circle;
    			}
    			
    		}
    	 
    	 
    	 
    	 
    	 
    	
    	public void paintComponent(Graphics grap) {
    		Graphics2D g = (Graphics2D) grap;
    		super.paintComponent(grap);
    		this.setBackground(Color.GRAY);
    		if(selctedshape==square) {
    			g.draw(square);
    		}else if(selctedshape==rect){
    			g.draw(rect);
    		}else if(selctedshape==circle) {
    			g.draw(circle);
    		}
    		
    	}
    	
    
    
    	@Override
    	public void itemStateChanged(ItemEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    	}

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    1) Do you call repaint()?

    2) How many Shapes objects do you have in your Main class? Are you doing logic in one and drawing in the other? Why?

    Time to do some creative debugging.:rolleyes:

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Also, you still have this needless code in your paintComponent method:
    Java Code:
        if (selctedshape == square) {
          g.draw(square);
        } else if (selctedshape == rect) {
          g.draw(rect);
        } else if (selctedshape == circle) {
          g.draw(circle);
        }

    Wouldn't it make much more sense to simply do this (as was already suggested in my previous post -- if you don't understand the suggestions, please mention it, but don't simply ignore them)?
    Java Code:
        if (selctedshape != null) {
          g.draw(selctedshape);
        }

  8. #8
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Smile

    thank you for the help. i was just learning how to create shapes and stuff so i had some trouble. now it works!!!:)

Similar Threads

  1. erase shapes ???
    By h9h in forum Java 2D
    Replies: 1
    Last Post: 10-12-2009, 09:11 PM
  2. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 07:35 PM
  3. Colors and shapes.
    By Torgero in forum New To Java
    Replies: 14
    Last Post: 10-13-2008, 06:25 PM
  4. Help with basic shapes in java
    By carl in forum Java 2D
    Replies: 1
    Last Post: 08-01-2007, 12:40 AM
  5. Help, basic shapes using java
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 09:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •