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); } }
Pravin Panicker
Ranch Hand
Joined: Oct 05, 2000
Posts: 62
posted
0
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 R Panicker<br />SCJP,SCWCD
Pravin Panicker
Ranch Hand
Joined: Oct 05, 2000
Posts: 62
posted
0
Also note that you hadnt instantiated the TheAxis object .This was causing a NullPointerException
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
"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.
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???
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4095
1
posted
0
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.