• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I am using GridBag.....

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below program, I am able to set up the window like I want. The only problem is when I run it, I can't see the graph.
What should I do??
================================================================
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Axis extends JFrame {
TheAxis ta;
void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}


public Axis() {
super("The window");
setSize(500,500);


JButton start = new JButton("Start");
JButton stp = new JButton("Stop");
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
pane.setLayout(gridbag);
buildConstraints(constraints, 0,0,1,1,0,80);
constraints.fill = GridBagConstraints.NONE;
gridbag.setConstraints(ta, constraints);
pane.add(ta);
buildConstraints(constraints, 0,1,1,1,50,20);
gridbag.setConstraints(start, constraints);
pane.add(start);
buildConstraints(constraints, 1,1,1,1,50,20);
gridbag.setConstraints(stp, constraints);
pane.add(stp);
setContentPane(pane);


}
public static void main(String[] arguments) {
Axis frame = new Axis();
ExitWindow exit =new ExitWindow();
frame.addWindowListener(exit);
frame.show();
}

}
class TheAxis extends JPanel {
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
setBackground(Color.white);
comp2D.setColor(Color.black);
Line2D.Float ln1 = new Line2D.Float(250f,0f,250f,500f);
Line2D.Float ln2 = new Line2D.Float(0f,250f,500f,250f);
comp2D.fill(ln1);
comp2D.fill(ln2);
comp2D.draw(ln1);
comp2D.draw(ln2);
}
}

class ExitWindow extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bshni
I did some modification to get your code running.
Is this what you wanted ??
-----------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Axis extends JFrame {
TheAxis ta;
void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public Axis() {
super("The window");
setSize(600,600);

JButton start = new JButton("Start");
JButton stp = new JButton("Stop");
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel pane = new JPanel();
pane.setLayout(gridbag);
buildConstraints(constraints, 0,0,1,1,0,80);
constraints.fill = GridBagConstraints.NONE;
ta = new TheAxis();
gridbag.setConstraints(ta, constraints);
pane.add(ta);
buildConstraints(constraints, 0,1,1,1,50,20);
gridbag.setConstraints(start, constraints);
pane.add(start);
buildConstraints(constraints, 1,1,1,1,50,20);
gridbag.setConstraints(stp, constraints);
pane.add(stp);
setContentPane(pane);

}
public static void main(String[] arguments) {
Axis frame = new Axis();
ExitWindow exit =new ExitWindow();
frame.addWindowListener(exit);
frame.show();
}

}
class TheAxis extends Canvas { // --------------- Note extending Canvas to do your painting
TheAxis()
{
setSize(500,500); // --------------- Set Size
}
public void paint(Graphics comp)// --------------- Note this
{
paintComponent(comp);
}
public void paintComponent(Graphics comp) {
Graphics2D comp2D = (Graphics2D)comp;
setBackground(Color.white);
comp2D.setColor(Color.blue);
Line2D.Float ln1 = new Line2D.Float(250f,0f,50f,500f);
Line2D.Float ln2 = new Line2D.Float(0f,250f,500f,250f);
comp2D.fill(ln1);
comp2D.fill(ln2);
comp2D.draw(ln1);
comp2D.draw(ln2);
}
}

class ExitWindow extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
 
Pravin Panicker
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that you hadnt instantiated the TheAxis object .This was causing a NullPointerException
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Bshni",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp .
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.
 
Bshni
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you soooo much. I have been asking eGroups for help on this program for weeks. You have given me a clear and understandable correction. You have made my day!!!
I am currently a math teacher changing careers. I do not have any programming experience except from what I read in
SAMS learn Java in 21 days. I am now also reading Thinking in Java by Bruce Eckel. He seems to be explaining it quite well.
I'm also signed up for a 5 day Sun Java course in Edison, NJ for the Java test.
Anything else I should do??
I'm going for Java certification and realized that swing is not on the exam. Could I bother you one more time and ask how does this change JButton, JPanel, and JFrame???
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
probably your only concern about swing vs awt(for test) is that default layout of Applet is FlowLayout. I believe it is BorderLayout for JApplet.
reply
    Bookmark Topic Watch Topic
  • New Topic