S
steeve_dun
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 ////////////////////////
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 ////////////////////////