2

I am designing a JApplet, and basically this applet will allow a user to draw a quadratic equation graph and it inserts the ranges of both x-axis and y-axis. But there is much more to do to reach that point.

I am still in the phase of designing the interface.

Here is my code:

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;


public class Applet extends JApplet {
    JPanel p1;
    JPanel p2;
    JPanel p3;

    JScrollPane s1;

    public Applet() {

    p1 = new JPanel();
    p2 = new JPanel();
    p3 = new JPanel();

    s1 = new JScrollPane(p3,s1.VERTICAL_SCROLLBAR_ALWAYS,s1.HORIZONTAL_SCROLLBAR_ALWAYS);
    }

    @Override
    public void init() {
    super.init();

    for(int i=0;i<100;i++)
    {
        p3.add(new JButton("Hello"));
        p3.add(new JLabel("blah"));
        p3.add(new JButton("Sup"));
    }

    p1.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
    p2.setPreferredSize(new Dimension(this.getWidth(),(int) (this.getHeight()*0.6667)));

    p3.setLayout(new BoxLayout(p3,BoxLayout.PAGE_AXIS));
    s1.setPreferredSize(new Dimension(this.getWidth(),(int)(this.getHeight()*0.33333)));

    p1.add(p2);
    p1.add(s1);

    this.add(p1);
    }

}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
MBC870
  • 373
  • 3
  • 8
  • 16
  • My answer is already given by others :-), that too well nicely explained. Simply use nested Box, to put components where you want :-) +1 to all, though did that already yesterday :-) – nIcE cOw Jun 17 '12 at 09:20

2 Answers2

4
  1. to have the x-axis and y-axis controls on top of each other, you should have two panels, one including the labels and text fields for the x-axis in one and those for the y-axis in the other. You would then add those to a panel that is aligned vertically. (Box.createVerticalBox(), for example)

  2. You can make your graph.java an ActionListener of the 'Plot' and 'Refine' buttons. In the actionPerformed method of graph.java, you can initiate a repaint, gathering the ranges from the 'ControlsB' instance.

EDIT: responding to your comments...

'how to add another panel in order for me to place the x-axis above the y-axis'

this can be as simple as (in ControlsB.java):

b = Box.createHorizontalBox();
b.add(new JLabel("Please enter range:  "));

Box b0 = Box.createVerticalBox();//create a vertical box to stack the controls

Box b1 = Box.createHorizontalBox(); // create a horizontal box for the x-axis

b1.add(new JLabel(" x-axis "));
b1.add(new JLabel("from"));
JTextField f1 = new JTextField("-5");
f1.setMaximumSize(new Dimension(100,30));
b1.add(f1);
b1.add(new JLabel(" to "));
JTextField f2 = new JTextField("5");
f2.setMaximumSize(new Dimension(100,30));
b1.add(f2);
b1.add(new JLabel(".   "));

Box b2 = Box.createHorizontalBox(); // create a second horizontal box for the y-axis
b2.add(new JLabel("y-axis "));
b2.add(new JLabel("from"));
JTextField f3 = new JTextField("5");
f3.setMaximumSize(new Dimension(100,30));
b2.add(f3);
b2.add(new JLabel("to"));
JTextField f4 = new JTextField("-5");
f4.setMaximumSize(new Dimension(100,30));
b2.add(f4);

b0.add(b1); // add the x-axis to the vertical box
b0.add(b2); // add the y-axis to the vertical box
b.add(b0);  // add the vertical box to the parent

b.add(new JButton("Plot"));
b.add(new JButton("Refine"));
add(b); //is this necessary?
}

' and how to gather the ranges from the ControlsB instance...'

You should look into ActionListeners in this tutorial to understand how to make the button click events trigger action in a separate class.

Also, two critiques:

  1. in your main class, GraphApplet, you are creating a Box before passing it into each of ControlsA and ControlsB's constructor. In the constructor, you then reassign the Box that you've passed in. I don't think that you need to do that. Either create the correctly aligned box in GraphApplet, pass that in and don't reassign, or don't pass anything in at all.

  2. Your ControlsA and ControlsB classes both extend JPanel. Although you go to the trouble of adding your Box containers to each of these at the end of their constructors, you don't ever add those Controls+ objects to any parent container. In your current implementation, i would suggest that extending JPanel is not necessary.

Community
  • 1
  • 1
akf
  • 38,619
  • 8
  • 86
  • 96
  • Thank you for your reply. I am new to java, therefore if possible I would like to ask you for further details on the above answer. Thanks :) – MBC870 Jun 16 '12 at 15:06
  • 1
    @Matthew: you should ask *specifically* about what isn't clear. – Hovercraft Full Of Eels Jun 16 '12 at 15:07
  • how to add another panel in order for me to place the x-axis above the y-axis and how to gather the ranges from the ControlsB instance... – MBC870 Jun 16 '12 at 15:10
  • Dear all, regarding the code you provided so that the x-axis is displayed above the y-axis is brilliant. Thanks a lot. Now regarding the issue I have with the action listener, in order for me to accept the values the user inputs, can anyone give me a tip on how to code it? thanks again – MBC870 Jun 17 '12 at 11:55
  • Matthew, i would suggest considering this question complete and creating a new question targeting the `ActionListener` issue. – akf Jun 17 '12 at 22:19
4

Recommendations:

  • As for having one component on top of another, use a layout manager that would facilitate this such as a vertical BoxLayout.
  • For having your display respond to a change in numeric data, I recommend that you design your program in a way to make this easy to implement, by using a Model-View-Controller type application design. Pressing a "plot" or "refine" button would trigger the controller which would update the model's data. The view or GUI would listen for model changes and when they occur would re-plot the graph based on the model's latest data.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373