I have an assignment in which I have to allow a user to plot a graph using a quadratic equation.
I managed to draw the skeleton of the graph, and now I am trying to display the "control panel" for the user to input the values.
I have 4 files:
graph.java
panel.java
panelB.java
panelC.java
My problem is when I run the code it is displaying only the panel.java
even in the container where it should display the other two panels.
panel.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
public class panel extends JPanel {
public panel(){
this.setBackground(Color.yellow);
}
}
Can anyone please advise what changes I should do to fix this problem?
I have done some changes to the graph.java file:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.*;
public class GraphApplet extends JApplet{
public GraphApplet(){
raph p = new Graph();//graph
p.setPreferredSize(new Dimension(760,500));
conn.add(p,BorderLayout.CENTER);
}
And now all that is being displayed is the graph.
Regarding the other code, I also made some changes to the class names:
gnjk;.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
public class Graph extends JPanel {
public Graph(){
this.setBackground(Color.yellow);
}
public void paintComponent(Graphics p) {
super.paintComponent(p);
Graphics2D graph = (Graphics2D)p;
this.setBackground(Color.yellow);//set background color.
int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
int xstart = 7;
int ystart = 1;
int xfinish = 3;
.......
bhfvhn.java
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
public class ControlsA extends JPanel{
public void init (Box g) {
a = Box.createVerticalBox();
a.add(new JLabel("Please enter the values below:"));
a.add(new JLabel("h"));
}
}
jknmk.java
import javax.swing.*;
import java.awt.Component;
import java.awt.Dimension;
public class ControlsB extends JPanel{
public void init (Box b) {
b = Box.createHorizontalBox();
b.add(new JLabel("a"));
JTextField f1 = new JTextField("0.0");
f1.setMaximumSize(new Dimension(100,30));
b.add(f1);
}
}
Here is an updated to my project:
jkl.java
import java.awt.BorderLayout;
import java.awt.Container;
public class GraphApplet extends JApplet{
public GraphApplet() {
public void init(){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
Container conn = getContentPane();
conn.setLayout(new BorderLayout());
Graph z = new Graph();
conn.add(p,BorderLayout.CENTER);
fasfae a = new ControlsA(box1);
conn.add(a,BorderLayout.LINE_START);
adsfawef b = new ControlsB(box2);
conn.add(b,BorderLayout.PAGE_END);
}
});
}
}
/*Container conn = getContentPane();
conn.setLayout(new BorderLayout());
Graph p = new Graph();//graph
p.setPreferredSize(new Dimension(460,560));
conn.add(p,BorderLayout.CENTER);
Box a = new Box(BoxLayout.Y_AXIS);
a.setPreferredSize(new Dimension(50,50));
conn.add(a,BorderLayout.EAST);
Box b = new Box(BoxLayout.X_AXIS);
b.setPreferredSize(new Dimension(201,50));
conn.add(b,BorderLayout.SOUTH);*/
//this code is commented not to loose it
vtk.java
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.JPanel;
class Graph extends JPanel {
public Graph(){
this.setBackground(Color.yellow);
}
@Override
public Dimension getPreferredSize(){return (new Dimension(460,560)); }
public void paint(Graphics z) {
Graphics graph = (Graphics2D)z;
this.setBackground(Color.yellow).
int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
//line co-ordinates
//the numbers represent the number of boxes on the graph
int xstart = 7;
int ystart = 1;
int x = 3;
int y = 9;
//other variables
int i = 0;
int i2 = 0;
int m = 0;
int n = 0;
int m2 = 0;
int n2 = 0;
int f2 = 0;
int g2 = 1;
//ranges
int f = 5;
int g = -5;
//change -ve num to +ve
int g3 = Math.abs(g);
int a1 = g3 + f;
int b1 = a1;
a = (Height);
f = (Width);
}
}
}
// 6 variables the user has to input
}
@Override
public Dimension getPreferredSize() {return (new Dimension(200,100));}
}
nllkl.java
@Override
public Dimension getPreferredSize(){return (new Dimension(201,50));}
}
Still no improvement. I cannot understand what is going on.
Please try to run this code now, tell me is this closer to what you wanted ? if so do let me know, so that I can delete my answer. Do watch the changes I had done :
PanelC
class, where instead of usinginit()
method, I made the constructor.setPreferredSizes(...)
calls, and instead I had overriddengetPreferredSize()
for each Class extendingJPanel
. For this visit each Class Panel, PanelB and PanelC.JPanel
, which is to go to theLEFT/LINE_START
side, to something that can be seenHere is the HTML file I used :
Here is the output I am getting :
The main problems in that code are:
panel
as opposed to one each ofpanel
,panelB
&panelC
.panelB
&panelC
ever adds theBox
to the panel, so it will not appear.panelB
should be vertically aligned, which means it would better fit in theLINE_START
(WEST
) of theBorderLayout
, as opposed to theNORTH
.public void paint(Graphics p) {..
is wrong forpanel
. Sincepanel
is a SwingJPanel
it should bepublic void paintComponent(Graphics p) {..
Once those things are attended to, this is how it might appear.
Other problems.
panel
. In fact, even thepanel
could be changed to show aBufferedImage
(the graph) inside aJLabel
panelB
andpanelC
are entirely redundant, just add aBox
directly to the layout area of the parent component needed.EachWordUpperCase
GraphApplet
, the graphing areaGraph
, ..I am not sure what to call the last 2 panels, since they carry identical components. If there were but one, I might call itControls
as a class (which is overkill for this), orcontrols
if it were an instance of a plainJPanel
orBox
.CENTER
of applet, a size will be suggested by the applet width/height specified in HTML, minus the natural size of the other components (theCENTER
component will get 'the rest of the space').Update
Change:
To:
Is the simplest way. Note that it changes if you decide to add the
Box
directly.