This is for all Java programers I have a challenge for you, I have created such a program that when you enter the equation of a circle its draws the circle now the challenge for you is to create a same program with Straight Line equation
Here is the code:- import javax.swing.*; import java.awt.*; import java.awt.event.*; class Circles extends JComponent implements ActionListener { JTextField t1=new JTextField(2); JTextField t2=new JTextField(2); JTextField t3=new JTextField(3); JTextField t4=new JTextField(3); JTextField t5=new JTextField(4); JFrame f=new JFrame("Co-ordiante"); double tanX=0,tanY=0; protected static int startX=0,startY=0,radius=0; protected static int Xco[]=new int[100]; protected static int Yco[]=new int[100]; protected static int rad[]=new int[100]; protected static int increment=0; protected static boolean b=false; public void paint(Graphics g) { Graphics2D g2d=(Graphics2D)g.create(); g2d.setPaint(Color.darkGray); for(int i=getWidth()/2;i>=0;i=i-10) { g2d.drawLine(i,0,i,getHeight()); } for(int i=getWidth()/2;i<=getWidth();i=i+10) { g2d.drawLine(i,0,i,getHeight()); } for(int i=getHeight()/2;i>=0;i=i-10) { g2d.drawLine(0,i,getWidth(),i); } for(int i=getHeight()/2;i<=getWidth();i=i+10) { g2d.drawLine(0,i,getWidth(),i); } g2d.setStroke(new BasicStroke(2)); g2d.drawLine(getWidth()/2,0,getWidth()/2,getHeight()); g2d.drawLine(0,getHeight()/2,getWidth(),getHeight()/2); g2d.setStroke(new BasicStroke(2)); if(b==true) { for(int i=0;i<increment;i++) {g2d.drawOval(getWidth()/2+Xco[i],getHeight()/2-Yco[i],rad[i]*2,rad[i]*2);} } } public static void main(String args[]) { JFrame accept=new JFrame("Data"); accept.setSize(400,100); Circles c=new Circles(); JPanel p=c.setParameters(); accept.add(p); accept.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); accept.setResizable(false); accept.setLocationRelativeTo(null); accept.setVisible(true); } public JPanel setParameters() { JPanel p=new JPanel(); JLabel l1=new JLabel("x^2"); JLabel l2=new JLabel("y^2"); JLabel l3=new JLabel("x"); JLabel l4=new JLabel("y"); p.add(t1); p.add(l1); p.add(new JLabel("+")); p.add(t2); p.add(l2); p.add(new JLabel("+")); p.add(t3); p.add(l3); p.add(new JLabel("+")); p.add(t4); p.add(l4); p.add(new JLabel("+")); p.add(t5); p.add(new JLabel("=0")); t5.addActionListener(this); return p; } public void actionPerformed(ActionEvent e) { double x2=Double.parseDouble(t1.getText()); double y2=Double.parseDouble(t2.getText()); double g2=Double.parseDouble(t3.getText()); double f2=Double.parseDouble(t4.getText()); double c=Double.parseDouble(t5.getText()); if(x2==y2) { if(x2!=1 && y2!=1) { g2=g2/x2; f2=f2/y2; c=c/x2; } double centerX=(-1)*(g2/2); double centerY=(-1)*(f2/2); double r=Math.sqrt((centerX*centerX)+(centerY*centerY)-c); //System.out.println("The circle is {("+centerX+","+centerY+");"+r+"}"); tanX=centerX-r; tanY=centerY+r; tanX=tanX*10; tanY=tanY*10; r=r*10; if(tanX>0) {tanX=Math.ceil(tanX);} else {tanX=Math.floor(tanX);} if(tanY>0) {tanY=Math.ceil(tanY);} else {tanY=Math.floor(tanY);} if(r>0) {r=Math.ceil(r);} else {r=Math.floor(r);} startX=(int)tanX; startY=(int)tanY; radius=(int)r; Xco[increment]=startX; Yco[increment]=startY; rad[increment]=radius; increment=increment+1; b=true;; f.setVisible(false); f.add(new Circles()); f.setSize(1280,658); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); } else {System.out.println("Equation does not satisfy a circle");} } }
no comments :/
challenge accepted
lets see what u do
Join our real-time social learning platform and learn together with your friends!