Results 1 to 2 of 2
  1. #1
    scheng12 is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Random Shape generator

    I have to make a simple program that generates a random object every time the mouse is clicked. I'm fairly new to Java so I'm not sure how difficult this is supposed to be. Can anyone help me or get me started? I know how to use the MouseListener and I'm able to generate random colors but I'm not sure how to implement shapes into it.

    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    
    
    public class ex4 extends JFrame implements MouseListener {
    
    	/**
    	 * 
    	 */
    Random numGen = new Random();
    
    	    Color getRandomColor() {
    	       return new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
    	    }
    
    	private String message = new String ("");
    	private Color myColor = Color.WHITE;
    
    
    
    	public ex4() {
    		setSize(400,400);
    		setTitle("ex4");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		addMouseListener(this);
    		setVisible(true);
    		this.setBackground(Color.WHITE);
    	}
    		
    	
    
    	public void paint(Graphics g) {
    		super.paint(g);
    		g.drawString(message, 100, 100);
    		g.fillOval ( 10, 10, 90, 90);
    	}
    	
    	public void mouseClicked(MouseEvent arg0) {
    		int n = new Random().nextInt(3);
    		switch(n) {
    		  case 0: createCircle(); break;
    		  case 1: createRect(); break;
    		  case 2: createEllipse(); break;
    		  default:
    		}
    
    
    	}
    	private void createEllipse() {
    		setMyColor(getRandomColor());
    		
    	}
    
    
    
    	private void createRect() {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    
    	private void createCircle() {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    
    	public void mouseEntered(MouseEvent arg0) {
    		
    	
    	}
    
    	public void mouseExited(MouseEvent arg0) {
    	
    	}
    
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new ex4();
    
    	}
    
    	public void setMyColor(Color myColor) {
    		this.myColor = myColor;
    	}
    
    	public Color getMyColor() {
    		return myColor;
    	}
    
    }

    I need help making the createCircle, Rect, Ellipse methods so that they do the right thing.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    did you also post this same question in the forums.sun.com?

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Replies: 3
    Last Post: 05-27-2008, 02:51 PM
  3. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  4. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 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
  •