Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Newbe,  paintComponent problem.

Thread view: 
steeve_dun@SoftHome.net - 13 Feb 2006 11:18 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.