display | more...

If you're planning to use paint, understand what I have written and you won't fall into a nasty trap.

In Java, a user interface widget, called a Component, is supposed to draw all of it's data in the paint method. This is compounded by the fact that Java programs have little control over the main area the have visual access to.

This is actually a good thing, but there is a problem in the Component class.

I'm writing this for myself, partly, because of my forgetfulness, and for others, because this is an issue that is usually ignored by Java graphics tutorials. In order to make this useful, I am including complete code that will compile in the JDK.

My example code does not use components because that would require using seperate source files, which would be annoying for you. Instead, it's an example using applets, and if you have trouble extending this information to Component, send me a msg.


Some people, after reading up on graphical programming in Java, know that paint() will be called by magic when the screen should be updated.




/*

*/ import java.applet.*; import java.awt.*; public class FirstTry extends Applet { Panel appletPanel; public void start() { //appletPanel isn't necessary, but it should help you get a 'feel' for the situation. //All drawing is done on FirstTry itself. appletPanel = new Panel(); appletPanel.setLayout(null); appletPanel.setSize(20,20); appletPanel.setLocation(450,50); appletPanel.setBackground(Color.green); appletPanel.setVisible(true); this.setLayout(null); this.setSize(490,490); this.add(appletPanel); this.setVisible(true); } public void paint(Graphics g) { g.setColor(Color.red); g.drawLine(0,0,500,500); System.out.println("debug: testing FirstTry::paint(Graphics g)"); } }

Compile with the JDK as usual. The source code contains just enough information for Java to load it as an html file. Run it with: appletviewer FirstTry.java or java -sun.applet.AppletViewer FirstTry.java . If this doesn't work, install the Java Runtime Environment(JRE), even if you have already installed the whole JDK.

Unfortunately, our enterprising programmer will decide that paint is unacceptable, because it will fail to be called in many cases when windows are moved over it, it's minimized then maximized, etc. Why is this unfortunate? Because they have also learnt that getGraphics() will return a suitable object for drawing many things to.

The logical conclusion, of course, is that they should just draw directly to getGraphics()

Which brings us to the SecondTry


/*

*/ import java.applet.*; import java.awt.*; import java.applet.*; import java.awt.event.*; import java.applet.AppletContext; public class SecondTry extends Applet { Panel appletPanel; public void start() { //appletPanel isn't necessary, but it should help you get a 'feel' for the situation. //All drawing is done on FirstTry itself. appletPanel = new Panel(); appletPanel.setLayout(null); appletPanel.setSize(20,20); appletPanel.setLocation(450,50); appletPanel.setBackground(Color.green); appletPanel.setVisible(true); this.setLayout(null); this.setSize(490,490); this.add(appletPanel); this.setVisible(true); class AnActionListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Action performed!"); paint(getGraphics()); } }; appletPanel.addActionListener(new AnActionListener()); } public void paint(Graphics g) { g.setColor(Color.red); g.drawLine(0,0,500,500); System.out.println("debug: testing FirstTry::paint(Graphics g)"); } }

Log in or register to write something here or to contact authors.