Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 19th, 2006, 08:44 PM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 12
A little help please :(

Trying to make a coordinate system that draws a polynomial of 3rd degree after user inputs values for A,B,C,D, and after pressing a button. So far I've got the 4 textfields, the button and a coordinate system. I still need to somehow make the program draw the graph. Thats the one part I dont understand. If u can help me with it I will fix other bugs, like having the graph upsidedown by myself. Thank you.
Main:
-----------------------------------------------------------
//Plotter.java

import javax.swing.JFrame;

public class Plotter
{
public static void main (String[] args)
{
JFrame frame = new JFrame ("Plotter");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
PlotterPanel panel = new PlotterPanel();

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
-----------------------------------------------------------
And
-----------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel;

public class PlotterPanel extends JPanel
{
private JTextField input1, input2, input3, input4;
private JButton button;
private int A, B, C, D;
double Y, X;

public void paintComponent(Graphics g)
{
super.paintComponent (g);
g.setColor (Color.white);
g.fillRect (0,50,500,500);
g.setColor (Color.black);
g.drawLine (250,50,250,550);
g.drawLine (0,275,500,275);
//now the graph:
// i want this part to be done whenever I push the button
/*g.setColor (Color.red);
int Y2 = (int)Y;
int X2 = (int)X;
System.out.println (X2+" , "+Y2);
g.drawLine ((X2*25+250),(Y2+275),(X2*25+250)+10,(Y2+275)+10); */
}
public PlotterPanel()
{
input1 = new JTextField(4);
input2 = new JTextField(4);
input3 = new JTextField(4);
input4 = new JTextField(4);
button = new JButton("plot");
button.addActionListener (new TempListener());


add (input1);
add (input2);
add (input3);
add (input4);

add (button);

setPreferredSize (new Dimension(500,550));
setBackground (Color.yellow);

//************************************************** ***************************

//************************************************** ***************************
}

private class TempListener implements ActionListener
{

public void actionPerformed (ActionEvent event)
{
String text1 = input1.getText();
String text2 = input2.getText();
String text3 = input3.getText();
String text4 = input4.getText();

A = Integer.parseInt (text1);
B = Integer.parseInt (text2);
C = Integer.parseInt (text3);
D = Integer.parseInt (text4);
X=-10;


while (X<10)
{

Y = A*Math.pow(X,3) + B*Math.pow(X,2) + C*X + D;
System.out.println (X + "," + Y);
X=X+0.01;

}
}

}



}

Reply With Quote
  #2  
Old March 20th, 2006, 10:38 PM
daniel_g daniel_g is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 60 daniel_g User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 3 m 50 sec
Reputation Power: 12
:( found more problems

Tried to debug it without the JButton. It seems to work. If u wanna try it, at first, type a number, press TAB, and do that again until you get to the 4th dialog box. Then resize the window and a nice graph should show up.
The problem is that it only seems to work for whole numbers. I tried a couple decimal and it just did some weird things. Can anyone figure out why? Also, anyone know why it draws lines and not points?

Code:
//Plotter.java

   import javax.swing.JFrame;

    public class Plotter
   {
       public static void main (String[] args)
      {
         JFrame frame = new JFrame ("Plotter");
         frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
         PlotterPanel panel = new PlotterPanel();
      
         frame.getContentPane().add(panel);
         frame.pack();
         frame.setVisible(true);
      }
   }

Code:
//PlotterPanel

   import java.awt.*;
   import java.awt.event.*; 
   import javax.swing.*; 
   import javax.swing.JPanel;

    public class PlotterPanel extends JPanel
   {
      private JTextField input1, input2, input3, input4;
      private JLabel inputLabel1, inputLabel2, inputLabel3, inputLabel4;
      private JButton button;
      private int A, B, C, D;
      double Y, X;
   	
   	
   	public PlotterPanel()
      {
         inputLabel1 = new JLabel ("Ax^3");
         input1 = new JTextField(4);
         inputLabel2 = new JLabel ("Bx^2");
         input2 = new JTextField(4);
         inputLabel3 = new JLabel ("Cx");
         input3 = new JTextField(4);
         inputLabel4 = new JLabel ("D");
         input4 = new JTextField(4);
         
      	add (inputLabel1);
         add (input1);
         add (inputLabel2);
         add (input2);
         add (inputLabel3);
         add (input3);
         add (inputLabel4);
         add (input4);
      
               
         setPreferredSize (new Dimension(500,550));
         setBackground (Color.yellow);
      
        
      }
   	
       public void paintComponent(Graphics g)
      {
         super.paintComponent (g);
         g.setColor (Color.white);
         g.fillRect (0,50,500,500);
         g.setColor (Color.black);
         g.drawLine (250,50,250,550);
         g.drawLine (0,275,500,275);
         //now the graph:
         String text1 = input1.getText();
         String text2 = input2.getText();
         String text3 = input3.getText();
         String text4 = input4.getText();
         
         A = Integer.parseInt (text1);
         B = Integer.parseInt (text2);
         C = Integer.parseInt (text3);
         D = Integer.parseInt (text4);
         X=-10;
         	
         					
         while (X<10)
         {
              
            Y = A*Math.pow(X,3) + B*Math.pow(X,2) + C*X + D;
            //System.out.println (X + "," + Y);
            X=X+0.001;
            g.setColor (Color.red);
            int Y2 = (int)Y;
            int X2 = (int)X;
            //System.out.println (X2+" , "+Y2);
            g.drawLine ((X2*25+250),(Y2+275),(X2*25+250)+10,(Y2+275)+10);   // i want this part to be done whenever I push the button
         
         	   
         }
      
        
      }
       
    	
            
         
   }

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > A little help please :(


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap