Hi folks,
I have a small problem.
1) The initial oval, from paintComponent, isn't draw
2) If the applet window looses focus, even the rectangle disappears.
I suppose it's a problem of instance. But I can't see what's
wrong.
Can anybody tell me why, please?
Thanks in advance.
-steeve.
Here is my code:
////////////////////// begin cut ////////////////////////
import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JApplet;
import javax.swing.JButton;
public class binom_swing extends JApplet {
private JPanel jContentPane = null;
private JPanel jPaneltop = null;
private JPanel jPanelbottom = null;
public Graphics g;
private JButton jButton1 = null;
public binom_swing() {
super();
}
public void init() {
setSize(800,700);
setContentPane(getJContentPane());
}
private JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.setPreferredSize(new java.awt.Dimension(800,700));
jContentPane.add(getJPaneltop(), java.awt.BorderLayout.NORTH);
jContentPane.add(getJPanelbottom(), java.awt.BorderLayout.SOUTH);
}
return jContentPane;
}
private JPanel getJPaneltop() {
if (jPaneltop == null) {
jPaneltop = new JPanel();
jPaneltop.setPreferredSize(new java.awt.Dimension(800,700));
jPaneltop.add(getJButton1(), null);
}
return jPaneltop;
}
private JPanel getJPanelbottom() {
if (jPanelbottom == null) {
jPanelbottom = new JPanel();
jPanelbottom.setPreferredSize(new java.awt.Dimension(800,650));
}
return jPanelbottom;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setText("rect");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
g = jPanelbottom.getGraphics();
DrawOnJComponent canvas = new DrawOnJComponent (g);
canvas.Rectangle();
}
});
}
return jButton1;
}
}
class DrawOnJComponent extends JComponent {
private Graphics g;
DrawOnJComponent(Graphics g){
this.g=g;
}
public void paintComponent(Graphics g){
g.drawOval(50,50,250,300); // draw initial OVAL
}
public void Rectangle() {
g.drawRect (50,50,250,300); // draw RECTANGLE
}
}
////////////////////// end cut ////////////////////////
Knute Johnson - 13 Feb 2006 17:33 GMT
> Hi folks,
> I have a small problem.
[quoted text clipped - 98 lines]
> }
> ////////////////////// end cut ////////////////////////
Normally drawing is done on a JPanel in Swing apps and applets. You
have a lot of code in your program that you don't need. All of your
painting needs to be done in the paintComponent method. If you want to
draw two different things, change some variable that can be seen from
the paintComponent method and branch your code. Call repaint() and the
new picture will get drawn.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends JApplet {
public void init() {
add(new DrawPanel());
}
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
g.drawOval(50,50,250,300);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class test5 extends JApplet implements ActionListener {
final static int OVAL = 0;
final static int RECT = 1;
int shape = OVAL;
public void init() {
setLayout(new BorderLayout());
add(new DrawPanel(),BorderLayout.CENTER);
JButton oval = new JButton("Oval");
oval.addActionListener(this);
add(oval,BorderLayout.WEST);
JButton rect = new JButton("Rect");
rect.addActionListener(this);
add(rect,BorderLayout.EAST);
}
public void actionPerformed(ActionEvent ae) {
String ac = ae.getActionCommand();
if (ac.equals("Oval"))
shape = OVAL;
else if (ac.equals("Rect"))
shape = RECT;
repaint();
}
public class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
if (shape == OVAL)
g.drawOval(50,50,250,300);
else if (shape == RECT)
g.drawRect(50,50,250,300);
else
g.drawString("Error",50,50);
}
}
}

Signature
Knute Johnson
email s/nospam/knute/