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 / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

problem getting the window to repaint()

Thread view: 
Joe Vasher - 16 May 2004 21:43 GMT
below is the JFrame class that I'm experimenting with. While doing so, I
discovered, I don't have a clue how the overridden paint() method works.

I tried several experiments to get the window to resize correctly.

the DayPlanner class is basically a fillRect();  which I included the
source at very bottom.

I have tried two listeners and addNotify  to get the rectangle to refill
the window. But it never gets repainted during resize. I did have
repaint() in the listeners. But took them out.

If you could link me an example of what I'm trying to do or explain what
I'm missing I would surely appreciate it.

I plan on adding a scroll bar to the CalendarView, that is why I'm using
JFrame. DayPlanner is going to be a self contained  component (eventually.)

/*
 * CalendarView.java
 *
 * Created on May 12, 2004, 1:16 AM
 */

package jnj_development.com.view;

import jnj_development.com.util.PDataStorage;
import javax.swing.*;
import java.awt.*;

/**
 *
 * @author  jvasher
 */
public class CalendarView extends javax.swing.JFrame
{
    DayPlanner dayPlanner;
    Graphics graphics;
    Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    PDataStorage prgData;
    Rectangle viewLocation = new Rectangle(); // main frame location.

    private int x, y, width, height;

    /** Creates new form CalendarView */
    public CalendarView()
    {
        prgData = new PDataStorage( "CalendarView" );
        initComponents();
        dayPlanner = new DayPlanner( graphics = this.getGraphics() );
        setTitle( "Schedule Assistant");
        viewLocation.width = prgData.intValue( "width", 500 );
        viewLocation.height = prgData.intValue( "height", 380 );
        viewLocation.x = prgData.intValue( "startX", (screenDim.width
- viewLocation.width) / 2 );
        viewLocation.y = prgData.intValue( "startY", (screenDim.height
- viewLocation.height) / 2 );
        setBounds( viewLocation );

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent
evt) {
                CalenderViewResized(evt);
            }
        });
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent
evt) {
                CalendarViewStateChanged(evt);
            }
        });

        pack();
    }

    private void CalenderViewResized(java.awt.event.ComponentEvent evt) {
        // TODO add your handling code here:
    }

    private void CalendarViewStateChanged(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
         // sets screen start location and size then stores.
        prgData.setIntValue( "height",  this.getHeight() );
        prgData.setIntValue( "width",   this.getWidth()  );
        prgData.setIntValue( "startX",  this.getLocation().x );
        prgData.setIntValue( "startY",  this.getLocation().y );
        prgData.store();
        System.exit(0);
    }

    public void addNotify()
    {
        super.addNotify();
        repaint();
    }

    public void paint( Graphics graphics )
    {
        Rectangle dayPlannerDim = new Rectangle();
        dayPlannerDim.width  = this.getWidth();
        dayPlannerDim.height = this.getHeight();
        dayPlannerDim.x = 0;
        dayPlannerDim.y = 28;

        dayPlanner.setBounds( dayPlannerDim );
        dayPlanner.drawDayPlanner();
    }
    /**
     * @param args the command line arguments
     */
    public static void main( String args[] )
    {
        new CalendarView().show();
    }

    // Variables declaration - do not modify
    // End of variables declaration

}

DAYPLANNER CLASS

/*
 * DayView.java
 *
 * Created on May 16, 2004, 3:39 PM
 */

package jnj_development.com.view;

import java.awt.*;

/**
 *
 * @author  jvasher
 */
public class DayPlanner
{
    Graphics2D g2D;
    Rectangle dayPlannerDim;

    /** Creates a new instance of DayView */
    public DayPlanner( Graphics g )
    {
         g2D = (Graphics2D)g;
    }

    public void setBounds( Rectangle dim )
    {
        dayPlannerDim = dim;
    }

    public void drawDayPlanner()
    {
        g2D.setColor( Color.YELLOW );
        g2D.fillRect( dayPlannerDim.x, dayPlannerDim.y,
                      dayPlannerDim.width, dayPlannerDim.height );
    }
}
Andrew Thompson - 16 May 2004 22:14 GMT
> below is the JFrame class that I'm experimenting with. While doing so, I
> discovered, I don't have a clue how the overridden paint() method works.

<http://www.physci.org/guifaq.jsp#2.4>
Not paint, paintComponent..

For an example of rendering in Swing..
<http://www.physci.org/launcher.jsp#JAnimateFrame>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Roedy Green - 17 May 2004 06:07 GMT
>below is the JFrame class that I'm experimenting with. While doing so, I
>discovered, I don't have a clue how the overridden paint() method works.

see http://mindprod.com/jgloss/paint.html
http://mindprod.com/jgloss/repaint.html
and chase links.

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.



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



©2008 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.