dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: probelm drives me crazy//drwing stuff

  1. #1
    Join Date
    Jan 2005
    Posts
    14

    probelm drives me crazy//drwing stuff

    hi guys i'm trying to make program that when the user put number the program draw for him shapes depends of his number but this shapes should drawn randomly with random color sonds nice yeah
    but i have problem in the code


    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    import java.awt.geom.*;
    import java.awt.event.*;
    class Drawing extends JPanel
    {
    JTextField textField;
    JLabel label;
    JPanel panel1;
    JPanel panel2;
    java.awt.Shape []shapes;
    Color[] colors = {Color.red,Color.blue,Color.magenta,Color.orange,Color.black,Color.yellow,Color.green};
    Random random = new Random();
    JFrame frame ;
    public Drawing()
    {
    textField = new JTextField(10);
    textField.addMouseListener(new MouseListener()
    {
    public void mouseClicked(MouseEvent e)
    {
    int d = Integer.parseInt(textField.getText());
    makeShapes(d);
    repaint(); //here is my problem when i call it doesnt work i dont
    //even though i put the method paintComponent
    System.out.println("we've problem here");
    }
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mousePressed(MouseEvent e)
    {}
    public void mouseReleased(MouseEvent e)
    {}

    });

    label = new JLabel("number of shapes drawing");
    panel1 = new JPanel();
    panel2 = new JPanel();
    panel1.setLayout(new FlowLayout());
    panel1.add(label);
    panel1.add(textField);
    JFrame frame = new JFrame();
    Container panel3 = frame.getContentPane();
    panel3.setLayout(new BorderLayout());
    panel2.setBackground(Color.white);
    panel3.add(panel1,"North");
    panel3.add(panel2,"Center");
    frame.setSize(300,300);
    frame.setVisible(true);
    }

    public void makeShapes(int length)
    {
    if(length >0)
    {
    shapes = new java.awt.Shape [length];

    for(int i = 0; i < length; i = i+3)
    {

    //if(panel2.getHieght()>0,panel2.getWidth()>0)
    //{
    System.out.println("i'm here");
    shapes[random.nextInt(length)] = new Rectangle2D.Double(random.nextInt(panel2.getWidth()),random.nextInt(panel2.getWidth()) ,random.nextInt(panel2.getHeight()) ,random.nextInt(panel2.getHeight()) ) ;
    if(i < length)
    shapes[random.nextInt(length)] = new Line2D.Double(random.nextInt(panel2.getWidth()),random.nextInt(panel2.getWidth()),random.nextInt(pan el2.getHeight()),random.nextInt(panel2.getHeight()));
    if(i < length)
    shapes[random.nextInt(length)] = new Ellipse2D.Double(random.nextInt(panel2.getWidth()),random.nextInt(panel2.getWidth()),random.nextInt( panel2.getHeight()),random.nextInt(panel2.getHeight()));
    //shapes[i+1] = new Line2D.Double(random.nextInt(getX()),random.nextInt(getX()),random.nextInt(getX()),random.nextInt(ge tX()));


    }
    }
    }
    public void paintComponent(Graphics g) //here is paintComponent

    {
    System.out.println("i;m not here");
    //Graphics2D f = (Graphics2D)g;
    super.paintComponent(g);
    for(int i =0;i<shapes.length;i++)
    {

    if(shapes[i] instanceof Rectangle2D.Double)
    {
    Rectangle2D.Double ff = (Rectangle2D.Double)shapes[i];
    g.drawRect((int)ff.getX(),(int)ff.getY(),(int)ff.getWidth(),(int)ff.getHeight());
    System.out.println("trrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
    }
    //f.fill(shapes[i]);
    if(shapes[i] instanceof Line2D.Double)
    {
    Line2D.Double rr = (Line2D.Double)shapes[i];
    g.drawLine((int)rr.getX1(),(int)rr.getX2(),(int)rr.getY1(),(int)rr.getY2());
    System.out.println("trrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
    }
    if(shapes[i] instanceof Ellipse2D.Double)
    {
    Ellipse2D.Double hh = (Ellipse2D.Double)(shapes[i]);
    g.drawOval((int)hh.getX(),(int)hh.getY(),(int)hh.getWidth(),(int)hh.getHeight());

    }
    }
    }

    public static void main(String []args)
    {
    new Drawing();
    }
    }


    please anyone suggest a solution because it driving me crazy
    thank u in advanced
    Attached Files Attached Files

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    seems like u have some serious logical problems, re-write the thing. If you need further help then let us know.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles