hi everyone .. I hope to help me :) ..
i have java code.. the main idea of it the user selected from menu the equation (liner - Cubic - Quadratic ) need of them and then user enter the value of x and y and then draw
the main problem of the code can not accept the value and then can not draw :'(
and this my code u can run it and discover the error ..

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class EMnue extends JApplet implements ActionListener  {
	JMenuBar menuBar;
	
	JMenu fileMenu;
	
	JMenuItem menuItem,menuItem2,menuItem3;
	
	
    JTextField text1=new JTextField (5);
    JTextField text2=new JTextField (5);
 
    JLabel l1=new JLabel("Enter X");
    JLabel l2=new JLabel("Enter Y");
    
    LinePanel panel= new LinePanel();
   
    public EMnue() {
    }
    public void init(){
       	JPanel p=new JPanel();
      	JButton b=new JButton("Draw");
    	p.setLayout(new FlowLayout());
       	 p.add(l1);
         p.add(text1);
         p.add(l2);
         p.add(text2);
         p.add(b);
        b.addActionListener(this);
         menuBar=new JMenuBar();
         menuBar.setLayout(new BoxLayout(menuBar,BoxLayout.X_AXIS)) ;
         fileMenu=new JMenu("Equations");
         menuBar.add(fileMenu);
       	menuItem = new JMenuItem("LineEqautions");
       	fileMenu.add(menuItem);
		setJMenuBar(menuBar);
		menuItem.setActionCommand("L1");
		menuItem.addActionListener(this);
			menuItem2 = new JMenuItem("CubicEqautions");
		menuItem2.setActionCommand("L1");
		menuItem2.addActionListener(this);
		fileMenu.add(menuItem2);
		setJMenuBar(menuBar);
			menuItem3 = new JMenuItem("QuadraticEqautions");
		menuItem3.setActionCommand("L1");
		menuItem3.addActionListener(this);
		fileMenu.add(menuItem3);
		setJMenuBar(menuBar);
		//this.getContentPane().add( new BorderLayout());
        this.getContentPane().add(p, BorderLayout.NORTH);
       // this.getContentPane().add(panel, BorderLayout.CENTER);
     	
    	
    }
    public void actionPerformed(ActionEvent e){
    	if((e.getActionCommand()).equals("l1")){
    		
    		
    		String str1=text1.getText();
    		String str2=text2.getText();
    		panel.x=Integer.parseInt(str1);
    		panel.y=Integer.parseInt(str2);
    		
    	}
    }
    
    public static void main(String s[]){
        	JFrame frame=new JFrame();
        	EMnue applet=new EMnue();
    		applet.init();
    		frame.getContentPane().add(applet);
    		frame.pack();
    		frame.setVisible(true);
    	
    }
    
    
}
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class LinePanel extends JPanel {
    public int x;
    public int y;
    int point=1000;


    public LinePanel() {
    
    }
    public void paintComponent(Graphics g)

    {

     super.paintComponent(g)                                                                                                                                                                                                                                                                                                                                                                                                                                                          ;

     Graphics2D g2 = (Graphics2D) g;

       

    g2.translate(200,200);

     int x1=x;
     int y1=y;
    int x2,y2;

    for(int i=0;i<point;i++)

    {x2=i;

    y2=x*x1+y;

    g2.drawLine(x1,y1,x2,y2);

    x1=x2;

    y1=y2;

    }
    }
}

note : My code is not yet complete .. I did not write an equation for the square and cubic


thanks for all:)

Dani AI

Generated

The code in this thread has three root causes that prevent input from being accepted and the plot from appearing: inconsistent action-command handling, incorrect panel management (the visible panel is never updated or is shadowed by a newly created local variable), and wrong coordinate/mapping logic inside paintComponent. reported the original failure to "accept the value and then can not draw," and partly progressed by printing input and adding a panel — but that change creates a duplicate local panel instead of updating the existing one and still leaves parsing and painting fragile.

Concrete, targeted fixes that match the existing UI flow:

  • Make action checks consistent (actionCommand strings are case-sensitive) or test the event source (e.getSource()) for the draw button; use distinct actionCommands for menu choices.
  • Update the same LinePanel instance instead of creating a new local panel. Provide a setter on the panel (e.g., setCoefficients) and call panel.repaint() after parsing.
  • Parse with try/catch and give feedback (JOptionPane) on bad input.
  • In paintComponent, map math coordinates to pixels: choose an origin and a scale (pixels per unit), compute mathX for each pixel column, evaluate the function (line, quadratic, cubic) in math space, convert mathY back to pixel Y, and draw line segments between consecutive pixel points. This avoids using raw input values as pixel coordinates.

Example of the minimal pattern to follow (replace existing wiring and painting with this style):

// read input and update existing panel
try {
  double a = Double.parseDouble(text1.getText().trim());
  double b = Double.parseDouble(text2.getText().trim());
  plotPanel.setCoefficients(a, b);    // update internal state
  plotPanel.repaint();                // redraw the existing panel
} catch (NumberFormatException ex) {
  JOptionPane.showMessageDialog(this, "Please enter valid numbers", "Input error", JOptionPane.ERROR_MESSAGE);
}

And in the panel's paintComponent, map pixels to math coordinates and draw:

protected void paintComponent(Graphics g) {
  super.paintComponent(g);
  int w = getWidth(), h = getHeight();
  int originX = w/2, originY = h/2;
  double scale = 20.0; // pixels per unit
  int prevY = Integer.MIN_VALUE;
  for (int px = 0; px < w; px++) {
    double mathX = (px - originX) / scale;
    double mathY = a * mathX + b;            // use chosen equation here
    int py = originY - (int)(mathY * scale);
    if (prevY != Integer.MIN_VALUE) g.drawLine(px-1, prevY, px, py);
    prevY = py;
  }
}

Small practical tips: call SwingUtilities.invokeLater for the GUI thread, give the panel a preferred size (setPreferredSize), call frame.setJMenuBar(...) when running as a JFrame (not applet), and avoid heavy computation inside paintComponent (cache if needed). These changes align with the existing posts while fixing the three blocking problems so the Draw button reliably updates the visible plot.

package com;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class EMnue extends JApplet implements ActionListener  {
	JMenuBar menuBar;
	
	JMenu fileMenu;
	
	JMenuItem menuItem,menuItem2,menuItem3;
	
	
    JTextField text1=new JTextField (5);
    JTextField text2=new JTextField (5);
 
    JLabel l1=new JLabel("Enter X");
    JLabel l2=new JLabel("Enter Y");
    
    LinePanel panel= new LinePanel();
 	JButton b;
 	JFrame frame;
    public EMnue(JFrame frame) {
    	this.frame=frame;
    }
    public void init(){
    	setLayout(new BorderLayout());
       	JPanel p=new JPanel();
     b=new JButton("Draw");
    	p.setLayout(new FlowLayout());
       	 p.add(l1);
         p.add(text1);
         p.add(l2);
         p.add(text2);
         p.add(b);
        b.addActionListener(this);
         menuBar=new JMenuBar();
         menuBar.setLayout(new BoxLayout(menuBar,BoxLayout.X_AXIS)) ;
         fileMenu=new JMenu("Equations");
         menuBar.add(fileMenu);
       	menuItem = new JMenuItem("LineEqautions");
       	fileMenu.add(menuItem);
		setJMenuBar(menuBar);
		menuItem.setActionCommand("L1");
		menuItem.addActionListener(this);
			menuItem2 = new JMenuItem("CubicEqautions");
		menuItem2.setActionCommand("L1");
		menuItem2.addActionListener(this);
		fileMenu.add(menuItem2);
		setJMenuBar(menuBar);
			menuItem3 = new JMenuItem("QuadraticEqautions");
		menuItem3.setActionCommand("L1");
		menuItem3.addActionListener(this);
		fileMenu.add(menuItem3);
		setJMenuBar(menuBar);
		//this.getContentPane().add( new BorderLayout());
        this.getContentPane().add(p, BorderLayout.NORTH);
       // this.getContentPane().add(panel, BorderLayout.CENTER);
        this.getContentPane().add(panel, BorderLayout.CENTER);
     	
    	
    }
    public void actionPerformed(ActionEvent e){
    	if(e.getSource()== b){
    		
    		//remove(panel);
    		String str1=text1.getText();
    		String str2=text2.getText();
    		System.out.println(str1);
    		System.out.println(str2);
    		// create panel object
    		LinePanel panel= new LinePanel();
    		panel.x=Integer.parseInt(str1);
    		panel.y=Integer.parseInt(str2);
    		//panel.repaint();
    		//add new panel to the frame
    	 this.getContentPane().add(panel, BorderLayout.CENTER);
    		   
    		 validate();
    		 
    		
    	}
    }
    
    public static void main(String s[]){
        	JFrame frame=new JFrame();
        	EMnue applet=new EMnue(frame);
    		applet.init();
    		frame.getContentPane().add(applet);
    		//frame.pack();
    		frame.setVisible(true);
    	
    }
    
    
}


 class LinePanel extends JPanel {
    public int x;
    public int y;
    int point=1000;


    public LinePanel() {
    
    }
    public void paintComponent(Graphics g)

    {

     super.paintComponent(g)                                                                                                                                                                                                                                                                                                                                                                                                                                                          ;

     Graphics2D g2 = (Graphics2D) g;

    g2.translate(200,200);

     int x1=x;
     int y1=y;
    int x2,y2;

    for(int i=0;i<point;i++)

    {x2=i;

    y2=x*x1+y;

    g2.drawLine(x1,y1,x2,y2);

    x1=x2;

    y1=y2;

    }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.