Wrox Programmer Forums
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 3rd, 2006, 04:02 PM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default fractals

Hi all
I have a problem in my program about fractals
my program should display fram cotains textfields, labels, and button draw. When the user enters values in textfields and then press draw button,the program should draw the shap
my program contain two files

file number 1

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.*;
////////////////////////////////////////////////////
 class Turtle extends JFrame {
    int x=200,y=200 ;
    float d=-90;
    GeneralPath p = new GeneralPath();
    Stack stack = new Stack();
    public Turtle (){
        super("Fractals");
        //setSize(400,400);
        //setVisible(true);
    }
    public Turtle(int x,int y, float d)
    {
        this.x=x;
        this.y=y;
        this.d=d;
    }
    void turn (float angle){
        d+=angle;// to get the new angle
    }
    void move (int length){
        //to calculate new position
        x+=Math.round (length*Math.cos(d*Math.PI/180));
        y+=Math.round (length*Math.sin(d*Math.PI/180));
        p.lineTo(x,y);
    }
        void move (int length,boolean b){
        //to calculate new position
        x+=Math.round (length*Math.cos(d*Math.PI/180));
        y+=Math.round (length*Math.sin(d*Math.PI/180));
        if (b)
        p.lineTo(x,y);
        else p.moveTo(x,y);
    }

    void makeObj(int length,float angle,String s)
    {
        char c;
        for( int i=0;i<s.length();i++)
        {
            c=s.charAt(i);
            if(c=='+')
            turn(angle);
            else if(c=='-')
            turn(-angle);
            else if(c=='F')
            move(length,true);
            else if(c=='f')
            move(length,false);
            else if (c=='[')
            saveTurtle();
            else if (c==']')
            loadTurtle();

        }
    }
    void saveTurtle()
    {
     stack.push(new Turtle(x,y,d));
    }
    void loadTurtle()
    {
        Turtle t= (Turtle)(stack.pop());
        x=t.x;
        y=t.y;
        d=t.d;
        p.moveTo(x,y);
    }
    String makeString(String initial,String rule,int iter)
    {
        String s1=initial;
        String s2;
        char c;
        for(int i=1;i<=iter;i++)
        {
            s2="";
            for(int j=0;j<s1.length();j++)
            {
                c=s1.charAt(j);
                if (c=='F')
                s2+=rule;
                else s2+=c;

            }
            s1=s2;
        }
        return s1;
    }

}

file number 2

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
////////////////////////////////////////////////////////
class panel1 extends JPanel implements ActionListener
{
    JLabel l1,l2,l3,l4,l5,l6;
    JTextField iteration,length,angle,initial,rule;//lengthInc;
    JButton draw;
    public panel1()
    {

     l1=new JLabel("Iteration: ");
     iteration=new JTextField(15);
     l2=new JLabel("Length: ");
     length=new JTextField(15);
     //l3=new JLabel("LengthInc:");
     //lengthInc=new JTextField(15);
     l4=new JLabel("angle: ");
     angle=new JTextField(15);
     l5=new JLabel("Initial: ");
     initial=new JTextField(15);
     l6=new JLabel("Rule: ");
     rule=new JTextField(15);
     draw=new JButton("Draw");
       draw.addActionListener(this);
       add(l1); add(iteration);
       add(l2); add(length);
       //add(l3); add(lengthInc);
       add(l4); add(angle);
       add(l5); add(initial);
       add(l6); add(rule);
       add(draw);

    }
        public void actionPerformed(ActionEvent e)
        {
        int it,len;//lenIn;
        float a;
        String i,r;
           if (e.getSource()==draw)
           {
               it = Integer.parseInt(iteration.getText());
               len = Integer.parseInt(length.getText());
               //lenIn= Integer.parseInt(lengthInc.getText());
               a = Float.parseFloat(angle.getText());
               i = initial.getText();
               r = rule.getText();
               Turtle t=new Turtle();
            String s=t.makeString(i,r,it);
               t.makeObj(len,a,s);
            }

        }
      }
///////////////////////////////////////////////////////////////
class panel2 extends JPanel
{
    JTextArea txtArea;
    public panel2()
    {
      txtArea = new JTextArea(15,35);
      add(txtArea);
    }
    /*Turtle t=new Turtle();
    public void paint(Graphics g)
    {
        Graphics2D g2d=(Graphics2D) g;
        //p.moveTo(x,y);
        //panel1 p1=new panel1();
        //g2d.draw(p);
    }*/
}
//////////////////////////////////////////////////////////////
class Turtle3 extends Turtle
{
    Container c;
    panel1 p1;
    panel2 p2;
    public Turtle3()
    {

        c=getContentPane();
        c.setLayout(new GridLayout(2,1));
        c.add(p1 = new panel1());
        c.add(p2 = new panel2());
        setSize(275,400);
        setVisible(true);

    }

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

file number 1 is correct but file number 2 has a problem
please help me and tell me what is the problem



 
Old May 5th, 2006, 02:43 AM
Registered User
 
Join Date: May 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<center>thanks for every one read it but any body can help me please </center>










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.