This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes problem in loading  another image in a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in loading  another image in a JPanel" Watch "problem in loading  another image in a JPanel" New topic
Author

problem in loading another image in a JPanel

abhigyaan SS
Greenhorn

Joined: Jun 08, 2006
Posts: 2
hi all,
i tried to create a code which could load images....and lines could be drawn
on an image on the click of a button "line segment"..but the problem is that when i have drawn lines on an image and i want to load another image by clicking the clear button.and then the open button...the previous lines persist and the new image appears with the previously drawn lines..(ie the clear button is not clearin the Jpanel)...please help me..
i am attachin the code i tried..



//this program is for adding lines to image already loaded by openpic prog
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class openpic2 extends JFrame
{

// Instance variables
JTextField m_fileNameTF = new JTextField(15);
JFileChooser m_fileChooser = new JFileChooser();
DrawingPanel imagepanel;
Image img;int arr[];
int height,width,hcount,vcount,flag,click;
JavaFilter fJavaFilter;
int mouseX,mouseY;

public static void main(String[] args)
{
JFrame window = new openpic2();
window.setVisible(true);
}
// constructor
openpic2()
{

// set the Text field to Read Only mode
m_fileNameTF.setEditable(false);

// Choose only files, not directories
m_fileChooser.setFileSelectionMode ( JFileChooser.FILES_ONLY);

// Set filter for Java source files.
fJavaFilter = new JavaFilter();
m_fileChooser.setFileFilter (fJavaFilter);

//... Add Open Button and its listeners
JButton openButton = new JButton("Open");
JButton lineButton = new JButton("line segment");

JButton wordButton=new JButton("word segment");
JButton clearButton=new JButton("clear");
openButton.addActionListener(new OpenAction());
lineButton.addActionListener(new LineAction());
wordButton.addActionListener(new wordAction());
clearButton.addActionListener(new clearAction());
//... Create contant pane, layout components
JPanel content = new JPanel();

content.setLayout(new BorderLayout());
JPanel buttonpanel=new JPanel();
buttonpanel.add(openButton);

buttonpanel.add(m_fileNameTF);
buttonpanel.add(lineButton);

buttonpanel.add(wordButton);
buttonpanel.add(clearButton);
content.add(buttonpanel,"South");
// Create JPanel canvas to hold the picture
imagepanel = new DrawingPanel();
//imagepanel.addMouseListener(new MouseClickListener());


// Create JScrollPane to hold the canvas containing the picture
JScrollPane scroller = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setPreferredSize(new Dimension(500,300));
scroller.setViewportView(imagepanel);
scroller.setViewportBorder(
BorderFactory.createLineBorder(Color.black));
// Add scroller pane to Panel
content.add(scroller,"Center");

//Set window characteristics
this.setTitle("File Browse and View");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(content);
this.pack();
}

class DrawingPanel extends JPanel
{
int harr[]=new int[100];
int varr[]=new int[100];
int x,y;
public DrawingPanel()
{ for(int i=0;i<50;i++)
harr[i]=varr[i]=0;
setBackground(Color.white);
setPreferredSize(new Dimension(750,950));
MouseClickListener listener= new MouseClickListener();
addMouseListener(listener);
Mouserelease listen=new Mouserelease();
addMouseListener(listen);



}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (img!=null)
{
// resizing the JPanel to the pic size
//int width = img.getWidth(this);
//int height = img.getHeight(this);
//imagepanel.setPreferredSize(new Dimension(width,height));
// printing the image on the panel
g.drawImage(img, 0, 0, this);


}


if(flag==0)
{for(int x=0;x<hcount;x++)
{
g.drawLine(0,harr[x],img.getWidth(imagepanel),harr[x]);
}
}
if(flag==1)
{for(int x=0;x<vcount;x++)
g.drawLine(varr[x],0,varr[x],img.getHeight(imagepanel));

}
//darwing line at position given by mouse click
if(click==1)


{for(int x=0;x<hcount;x++)
{if(harr[x]==mouseY)
{g.setColor(Color.white);
g.drawLine(0,mouseY,img.getWidth(imagepanel),mouseY);
harr[x]=9999;
}
}

}
if(click==2)
{g.setColor(Color.black);
g.drawLine(0,mouseY,img.getWidth(imagepanel),mouseY);
harr[hcount++]=mouseY;
}
}
public void sethLine(int arre[] )
{
for(int i=0;i<hcount;i++)
this.harr[i]=arre[i];
repaint();
}
public void setvLine(int arre[])
{ for(int i=0;i<vcount;i++)
this.varr[i]=arre[i];
repaint();
}
public void setaLine()
{//g.drawLine(0,y,img.getWidth(imagepanel),y);
repaint();
}
public void setbline()
{repaint();
}

}



class MouseClickListener extends MouseAdapter
{ public void mousePressed(MouseEvent e)
{mouseX=e.getX();
mouseY=e.getY();
click=1;
System.out.println(mouseX);
System.out.println(mouseY);
imagepanel.setaLine();





}
}

class Mouserelease extends MouseAdapter
{public void mouseReleased(MouseEvent e)
{mouseX=e.getX();
mouseY=e.getY();
click=2;
imagepanel.setbline();
}
}

// OpenAction
class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
//... Open a file dialog.
int retval = m_fileChooser.showOpenDialog(openpic2.this);
if (retval == JFileChooser.APPROVE_OPTION)
{
//... The user selected a file, process it.
File file = m_fileChooser.getSelectedFile();
// store the file path in a string and send to JNI
String s = file.getPath();
System.out.println("The Path is:\n" + s);
//... Update user interface.
img = Toolkit.getDefaultToolkit().getImage(file.getPath());
//if(img!= null)
// loadimage();
m_fileNameTF.setText(file.getName());
imagepanel.repaint();
}
}





}
//this tries to build lines
class LineAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int width = img.getWidth(imagepanel);
int height = img.getHeight(imagepanel);
flag=0;
try{ int array[]= new int[100];
int k=0;
for(int x=0;x<=height;x=x+20)
{array[k]=x;

k++;
}
hcount=k;
imagepanel.sethLine(array);
}
catch(ArrayIndexOutOfBoundsException out){}



}


}

class wordAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int width=img.getWidth(imagepanel);
int height=img.getHeight(imagepanel);

flag=1;
try{ int array[]= new int[100];
int k=0;
for(int x=0;x<=width;x=x+20)
{array[k]=x;

k++;
}
vcount=k;
imagepanel.setvLine(array);
}
catch(ArrayIndexOutOfBoundsException out){}
}



}

class clearAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{imagepanel.removeAll();
validate();
//imagepanel.repaint();
//Sy//stem.out.println("in clear button");
imagepanel.repaint();

// JFrame window = new openpic2();
//window.setVisible(true);



}



}



/** Filter to work with JFileChooser to select java file types. **/
class JavaFilter extends javax.swing.filechooser.FileFilter
{
// this function is internally used for the Filtering action
public boolean accept (File f)
{
return f.getName ().toLowerCase ().endsWith (".jpeg")
|| f.getName ().toLowerCase ().endsWith (".jpg")
|| f.getName ().toLowerCase ().endsWith (".gif")
|| f.isDirectory ();
}

// this function is internally used for the Filter Option drop down menu
public String getDescription ()
{
return "*.jpeg, *.gif, *.jpg";
}
}
}
Vishal Mungi
Greenhorn

Joined: Mar 24, 2005
Posts: 24
since you are painting the lines in paintcomponent the
"flag"s for the lines are still set.
Clear the flags once you open a new file like :

in paintcomponent() for DrawingPanel...

if(cleared)flag=2;

set cleared to false when u open a new file
set it to true while clearing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

"abhigyaan SS",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in loading another image in a JPanel
 
Similar Threads
problem in attaching mouselistener on a imagepanel
how to reflect the changes made in an inner class in main
problem in drawing lines on a panel
problem in drawing deleting a line on the click of a mouse
to draw and remove lines on click of mouse