questions about the implications of multiple JFrames using dispose() on closing

R

rfractal30

Hi

I've come up against some perplexing questions caused by the creation
of multiple instances of JFrame.

obj is referencing a JButton click. This then loads a FileDialog for
opening an image and displaying it in an extended JFrame called
'ResizeFrame'. JFrame then loads a panel which displays the Image. The
code as it stands allows multiple instances of ResizeFrame.

ResizeFrame basically does most of the work and calls methods to load
the Image - which has been opened in it - to be drawn in ResizePanel.

ResizeFrame is coded to 'dispose' on closing.

What I am trying to understand is what happens when a frame is opened,
then closed, and then a new frame is opened. Does the instance of
ResizeFrame (all named the same) that was initially opened, still
exist, or has it been over-written by the new version.

Am I making a mistake allowing several JFrames to be opened at once? I
know that when a frame is disposed that its claims on screen resourses
are freed up - but if it is still existing even after it has been
disposed it must still be using some memory resources?

Also, what are the implications of having several objects existing with
the same name?

Another thing I've noticed. My ResizeFrame has a method for loading a
panel and then is made visible within ResizeFrame. What I've noticed is
that if I load a ResizeFrame Image, then close it, then open up the
same image file - that the picture will appear much faster than if I
open up a different image. I'm not understanding how this is happening!

I could change my code so that it is only possible to have 1
ResizeFrame open at a time - however this would require a considerable
amount of simplification (i.e. beyond my hackish skills!). I would only
do it if I am convinced that the way I have done things is BAD with a
capital B (which it probably is - but then I am a very inexperienced
programmer).

Thanks for any advice.

Michael


//-----------------------------
if (obj == newdepth) {
File f = loadimage("depth\\", FileDialog.LOAD);
if (!(f == null)) {
File q = new File(f.getParent());
if (q.isDirectory()) {
String w = f.getPath();
p.LoadPic(w);
ResizeFrame jeff = new ResizeFrame((w + " - Depth Map"), new
Color(205, 219, 227), false);
jeff.LoadPic(w);
jeff.addpanel();
}
else {
status.setText("invalid directory");
}
}
}
//--------------------------------


//--I've included the full code for ResizeFrame and ResizePanel hoping
for constructive critism. Also, it might help to make my questions
clearer.

//-----------ResizeFrame----------
import java.awt.*;
import javax.swing.*;
import myroot.frames.*;
import java.awt.event.*;

import java.io.*;
import java.awt.image.*;


import osbaldeston.image.BMP;

public class ResizeFrame extends JFrame implements ImageObserver {
public Image myimage;
public ResizePanel mypanel;
public Color col;
public boolean resizing;



public void LoadPic(String inpic){
myimage = null;

int typepic = getTypePic(inpic);


if (typepic == 1) {
BMP dave = new BMP(new File(inpic));
myimage = dave.getImage();
}
if (typepic == 2) {
myimage = Toolkit.getDefaultToolkit().getImage ( inpic);
}


WaitImage(myimage);

}

public int getTypePic(String in) {
if (in.length() > 4) {
String test = in.substring(in.length() - 4, in.length());
test = test.toLowerCase();

if (test.equals(".bmp")) return 1;
if (test.equals(".gif")|| test.equals(".png") ||
test.equals(".jpg") || test.equals("jpeg")) return 2;

}
return 0;
}
public void WaitImage(Image im) {
boolean wait = true;
wait = ImageUtilities.waitForImage(im, this);
}

public void addpanel() {
if (resizing == true)
setExtendedState(Frame.MAXIMIZED_BOTH);
mypanel = new ResizePanel(myimage, col);

JScrollPane scroller = new JScrollPane(mypanel,
//JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

scroller.getVerticalScrollBar().setUnitIncrement( 16 );

getContentPane().add(scroller);
if (resizing == false) {
int w;
int h;

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
if ((myimage.getWidth(this) +40) > d.width)
w = d.width;
else
w = myimage.getWidth(this) + 40;
if ((myimage.getHeight(this) +60) > (d.height -30))
h = d.height -30;
else
h = myimage.getHeight(this) + 60;



setSize(w,h);
}
setVisible(true);
//mypanel.repaint();
}

public void setSize(){

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setSize(d.width, d.height);
}

public ResizeFrame(String title, Color c, boolean res) {


super(title);
resizing = res;
col = c;
if (resizing == true) {
setSize();
}



setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});


}
}
//-------------------------

//---------ResizePanel--------

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;


public class ResizePanel extends JPanel{
public Image myimage;
public Color col;

public ResizePanel(Image im, Color c) {
myimage = im;
col = c;
repaint();
}




public Dimension getPreferredSize(){
return (new Dimension(myimage.getWidth(this),
myimage.getHeight(this)));
}

public void paint(Graphics g) {
int h =0, v = 0;
h = (this.getWidth()-myimage.getWidth(this))/2;
v = (this.getHeight()-myimage.getHeight(this))/2;
if (h< 0)
h = 0;

if (v<0) v = 0;
g.setColor(col);
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.drawImage(myimage, h,v, this);

}

}
//--------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top