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);
}
}