By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,786 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,786 IT Pros & Developers. It's quick & easy.

Problem displaying JPanel in JApplet

Matthewborg
P: 2
I am doing an assignment which it consists of an Applet, where the user has to insert several values and the program has to draw a quadratic graph.

In my applet I want to display the graph in the border layout center and one control panel to the east side and another control panel to the south side.

For now both the control panels are the same, until I manage to make my applet display them I am not willing to waste time changing them.

I am having a problem to display these two JPanels since on the applet all I can see is empty space (where the panels should be displayed), the area of the graph is ok since it is showing in the applet.

I have 4 files which are:


GraphApplet.java
Graph.java
ControlsA.java
ControlsB.java


Now the contents of the code are as below:

GraphApplet.java

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.BorderLayout;
  3. import java.awt.Container;
  4. import java.awt.Dimension;
  5. import javax.swing.*;
  6.  
  7. public class GraphApplet extends JApplet{
  8.  
  9.  
  10.     public GraphApplet(){
  11.  
  12.         Container conn = getContentPane();
  13.         conn.setLayout(new BorderLayout());     
  14.  
  15.         Graph p = new Graph();//graph
  16.  
  17.         p.setPreferredSize(new Dimension(460,560));
  18.         conn.add(p,BorderLayout.CENTER);
  19.  
  20.         Box a = new Box(BoxLayout.Y_AXIS);//vertical
  21.  
  22.         a.setPreferredSize(new Dimension(50,50));
  23.         conn.add(a,BorderLayout.EAST);
  24.  
  25.         Box b = new Box(BoxLayout.X_AXIS);//vertical
  26.  
  27.         b.setPreferredSize(new Dimension(201,50));
  28.         conn.add(b,BorderLayout.SOUTH);
  29.  
  30.  
  31.     }
  32. }
  33.  
  34.  
Graph.java

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.*;
  6. import java.awt.geom.*;
  7. import javax.swing.JPanel;
  8.  
  9.  
  10. public class Graph extends JPanel {
  11.  
  12.     public Graph(){
  13.         this.setBackground(Color.yellow);
  14.     }
  15.  
  16.     public void paintComponent(Graphics p) {
  17.  
  18.             super.paintComponent(p);
  19.  
  20.             Graphics2D graph = (Graphics2D)p;
  21.  
  22.             Dimension appletSize = this.getSize();
  23.             int appletHeight = (int)(appletSize.height);
  24.             int appletWidth = appletSize.width;
  25.  
  26.             this.setBackground(Color.yellow);//set background color.
  27.  
  28.             int x,y,y1,x1,a,b,p1x,p1y,p2x,p2y;
  29.  
  30.             //line co-ordinates
  31.             //the numbers represent the number of boxes on the graph
  32.             int xstart = 7;
  33.             int ystart = 1;
  34.  
  35.             int xfinish = 3;
  36.             int yfinish = 9;
  37.  
  38.             //other variables
  39.             int i = 0;
  40.             int i2 = 0;
  41.             int m = 0;
  42.             int n = 0;
  43.             int m2 = 0;
  44.             int n2 = 0;
  45.             int f2 = 0;
  46.             int g2 = 1;
  47.  
  48.             //ranges
  49.             int f = 5;
  50.             int g = -5;
  51.  
  52.             //change -ve num to +ve
  53.             int g3 = Math.abs(g);
  54.  
  55.             int a1 = g3 + f;
  56.             int b1 = a1;
  57.  
  58.             y1 = (appletHeight);
  59.             x1 = (appletWidth);
  60.             y = (appletHeight / 2);
  61.             x = (appletWidth / 2);
  62.             a = (appletWidth / a1);
  63.             b = (appletHeight / b1);
  64.  
  65.             int d = (appletWidth / a1);
  66.             int e = (appletHeight / b1);
  67.  
  68.             /**
  69.              to determine the
  70.              ammount of pixles there
  71.              is in each box of the
  72.              graph, both y-axis and 
  73.              x-axis
  74.              */
  75.             int xbox = x1 / 10;
  76.             int ybox = y1 / 10;
  77.  
  78.             //line variables
  79.             //the xstart, ystart, etc represent the number of boxes
  80.  
  81.             //top point of the line on the graph
  82.             p1x = xbox * xstart;//start x
  83.             p1y = ybox * ystart;//start y
  84.  
  85.             //lowwer point of the line on the graph
  86.             p2x = xbox * xfinish;//finish x
  87.             p2y = ybox * yfinish;//finish y
  88.  
  89.             //draw y-axis numbers 
  90.             //(+ve)
  91.             while(f != 0){
  92.                 String s = String.valueOf(f);
  93.                 p.drawString(s,(x + 5),m + 13);
  94.                 m = m + b;
  95.                 f = f - 1;
  96.             }
  97.             //(-ve)
  98.             m2 = y;
  99.             while(f2 != g-1){
  100.                 String u = String.valueOf(f2);
  101.                 p.drawString(u,(x + 5),m2 - 3);
  102.                 m2 = m2 + b;
  103.                 f2 = f2 - 1;
  104.             }
  105.             //draw x-axis numbers.
  106.             //(-ve)
  107.             while(g != 0){
  108.                 String t = String.valueOf(g);
  109.                 p.drawString(t,n,y - 5);
  110.                 n = n + a;
  111.                 g = g + 1;
  112.             }
  113.             //(+ve)
  114.             n2 = x + a;
  115.             while(g2 != g3+1){
  116.                 String vw = String.valueOf(g2);
  117.                 p.drawString(vw,n2 -10,y - 5);
  118.                 n2 = n2 + a;
  119.                 g2 = g2 + 1;
  120.             }
  121.  
  122.             BasicStroke aLine2 = new BasicStroke(1.0F,
  123.                                                  BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  124.             graph.setStroke(aLine2);
  125.  
  126.             //notch on numbers and grid lines
  127.             //left to right, top to bottom notches
  128.             int v2 = -5;
  129.             int v5 = 0;
  130.             while(i <= a1-1){
  131.                 p.setColor(Color.lightGray);//lightgray line
  132.                 p.drawLine(a,0,a,y1);//vertical lightgray
  133.                 p.drawLine(0,b,x1,b);//horizontal lightgray
  134.                 a = a + d;
  135.                 b = b + e;
  136.                 i = i + 1;
  137.             }
  138.             //notches
  139.             while(i2 <= a1){
  140.                 p.setColor(Color.blue);//notch color
  141.                 p.drawString("x",v2+2,y+3);//xaxis
  142.                 p.drawString("x",x-4,v5+4);//yaxis
  143.                 v5 = v5 + e;
  144.                 v2 = v2 + d;
  145.                 i2 = i2 + 1;
  146.             }
  147.  
  148.             //draws the border of the graph
  149.             p.setColor(Color.black);
  150.             Rectangle2D.Float rect = new Rectangle2D.Float(0,0,x1,y1);
  151.             BasicStroke aLine = new BasicStroke(2.5F, 
  152.                                                 BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  153.             graph.setStroke(aLine);
  154.             graph.draw(rect);
  155.  
  156.             //draw cross
  157.             BasicStroke aLine3 = new BasicStroke(2.5F,
  158.                                                  BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  159.             graph.setStroke(aLine3);
  160.             p.drawLine(x,0,x,y1); //vertical line
  161.             p.drawLine(0,y,x1,y); //horizontal line
  162.  
  163.             //display the value of graph width and graph height
  164.             String aw = String.valueOf(x1);
  165.             p.drawString("Graph Width = ", 50,90);
  166.             p.drawString(aw,150,90);
  167.             p.drawString("Graph Height = ", 50,110);
  168.             String ah = String.valueOf(y1);
  169.             p.drawString(ah,156,110);
  170.  
  171.             //draw line on graph
  172.  
  173.             BasicStroke aLine4 = new BasicStroke(1.5F,
  174.                                                  BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
  175.             graph.setStroke(aLine4);
  176.             p.setColor(Color.red);
  177.  
  178.             if(p1x <= x1 && p2x <= x1 && p1y <= y1 && p2y <= y1){
  179.                 p.drawLine(p1x,p1y,p2x,p2y);
  180.                 Color c = new Color(0,0,0);
  181.                 p.setColor(c);
  182.                 p.drawString("X", p1x-4,p1y+4);
  183.                 p.drawString("X", p2x-4,p2y+4);
  184.             }
  185.             else{
  186.                 p.setColor(Color.black);
  187.                 p.drawRect(48,34,223,35);
  188.                 p.setColor(Color.white);
  189.                 p.fillRect(49,35,222,34);
  190.                 p.setColor(Color.red);
  191.                 p.drawString("Wrong co-ordinates!!!", 50,50);
  192.                 p.drawString("Values exceede applet dimensions.", 50,65);
  193.             }
  194.         }
  195.     }
  196.  
  197.     // 6 variables the user has to input
  198.     // A,B,C,D - CO-EFFICIENTS
  199.     // RANGE (EX: -5 - 5)
  200.  
  201.  
ControlsA.java

Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3.  
  4. import java.awt.*;
  5.  
  6. import javax.swing.JPanel;
  7.  
  8.  
  9. public class ControlsA extends JPanel{
  10.  
  11.     public void init (Box a) {
  12.  
  13.         a = Box.createVerticalBox();
  14.         a.add(new JLabel("Please enter the values below:"));
  15.         a.add(new JLabel("a"));
  16.         JTextField g1 = new JTextField("0.0");
  17.         g1.setMaximumSize(new Dimension(100,30));
  18.         a.add(g1);
  19.         a.add(new JLabel("b"));
  20.         JTextField g2 = new JTextField("0.0");
  21.         g2.setMaximumSize(new Dimension(100,30));
  22.         a.add(g2);
  23.         a.add(new JLabel("c"));
  24.         JTextField g3 = new JTextField("0.0");
  25.         g3.setMaximumSize(new Dimension(100,30));
  26.         a.add(g3);
  27.         a.add(new JLabel("d"));
  28.         JTextField g4 = new JTextField("0.0");
  29.         g4.setMaximumSize(new Dimension(100,30));
  30.         a.add(g4);
  31.         a.add(new JButton("Plot"));
  32.         a.add(new JButton("Refine"));
  33.  
  34.     }
  35.  
  36. }
  37.  
  38.  
ControlsB.java

Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3.  
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6.  
  7. public class ControlsB extends JPanel{
  8.  
  9.     public void init (Box b) {
  10.  
  11.         b = Box.createHorizontalBox();
  12.         b.add(new JLabel("a"));
  13.         JTextField f1 = new JTextField("0.0");
  14.         f1.setMaximumSize(new Dimension(100,30));
  15.         b.add(f1);
  16.         b.add(new JLabel("b"));
  17.         JTextField f2 = new JTextField("0.0");
  18.         f2.setMaximumSize(new Dimension(100,30));
  19.         b.add(f2);
  20.         b.add(new JLabel("c"));
  21.         JTextField f3 = new JTextField("0.0");
  22.         f3.setMaximumSize(new Dimension(100,30));
  23.         b.add(f3);
  24.         b.add(new JLabel("d"));
  25.         JTextField f4 = new JTextField("0.0");
  26.         f4.setMaximumSize(new Dimension(100,30));
  27.         b.add(f4);
  28.         b.add(new JButton("Plot"));
  29.         b.add(new JButton("Refine"));
  30.  
  31.     }
  32. }
  33.  
  34.  

These codes all compile and as mentioned above the applet runs successfully, but without displaying the "ControlsA.java" and "ControlsB.java"

Attached please find the applet result:-




Can any one please help....

Thanks a lot for your time
Attached Images
File Type: jpg Screen Shot 2012-06-16 at 13.53.01.jpg (69.4 KB, 178 views)
Jun 16 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.