Graphics drawing problem
To: comp.lang.java.gui
Hi,
I'm a java gui newbie. I have this problem;
I made a class "Gui" I want to use as a template for future Gui
builds. I want to draw lines in the center. I made a "test" method
which I call from main class. For some reason the line I attempt to
draw is not visible. When I call sleep between the initiate() and the
test() methods the line will sometimes be visible.
I would appreciate any pointers of what I'm doing wrong. I paste the
source ...
import java.awt.*;
import javax.swing.*;
public class StartGui {
public static void main(String[] args) throws Exception{
Gui gui = new Gui();
gui.initiate();
//Thread.sleep(2000);
gui.test(100,100,300,300);
}
}
import javax.swing.*;
import java.awt.*;
public class Gui extends JFrame{
Color cN = Color.LIGHT_GRAY;
Color cW = Color.LIGHT_GRAY;
Color cC = Color.LIGHT_GRAY;
Color cE = Color.LIGHT_GRAY;
Color cS = Color.LIGHT_GRAY;
Color cGap = Color.BLACK;
int widthW = 200;
int widthC = 400;
int widthE = 200;
int heightN = 150;
int heightC = 400;
int heightS = 150;
Graphics graphics;
JPanel n;
JPanel s;
JPanel e;
JPanel w;
JPanel c;
public Gui() {
new JFrame("Gui");
n = new JPanel();
w = new JPanel();
e = new JPanel();
s = new JPanel();
c = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public void initiate() {
getContentPane().setLayout(new BorderLayout(2,2));
getContentPane().setBackground(cGap);
n.setBackground(cN);
n.setPreferredSize(new Dimension(widthE+widthC+widthW,heightN));
w.setBackground(cW);
w.setPreferredSize(new Dimension(widthW,heightC));
c.setBackground(cC);
c.setPreferredSize(new Dimension(widthC,heightC));
e.setBackground(cE);
e.setPreferredSize(new Dimension(widthE,heightC));
s.setBackground(cS);
s.setPreferredSize(new Dimension(widthE+widthC+widthW,heightS));
getContentPane().add(e,BorderLayout.EAST);
getContentPane().add(w,BorderLayout.WEST);
getContentPane().add(n,BorderLayout.NORTH);
getContentPane().add(s,BorderLayout.SOUTH);
getContentPane().add(c,BorderLayout.CENTER);
pack();
}
public void test(int x1, int y1, int x2, int y2){
c.setBackground(Color.BLACK);
graphics = c.getGraphics();
graphics.setColor(Color.GREEN);
graphics.drawLine(x1,y1,x2,x2);
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24