Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

cindrella ss

Greenhorn
+ Follow
since May 30, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by cindrella ss

hi all,
i tried to create a code which draws lines across an image when line segment button is clicked.then on the click of a button at a point a line is already thr,the line should get replaced by a white line(that is line should be deleted),if the line is not there then on clicking at that point line should be drawn nd is mouse is dragged from a particular line,then line gets shifted from previous to position whr mouse is released...
an arrar harr[] is used to store positions whr line is drawn nd stores 9999 whr a white line is drawn...but the code doesnt delete line on the click..
please help me..
i am attaching the code



//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();int flag2=0;int t=0;
DrawingPanel imagepanel;
Image img;int arr[];
int height,width,hcount,vcount,flag=3,click,clear;
JavaFilter fJavaFilter;
int mouseX,mouseY,mx,my;
int varr[]=new int[1000];
int harr[]=new int[1000];
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 x,y;
public DrawingPanel()
{ for(int i=0;i<1000;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);
//MouseClick clicklistener=new MouseClick();
//addMouseListener(clicklistener);



}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (img!=null && clear==0)
{
// 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 && clear==0)
{for(int x=0;x<=hcount;x++)
{if(harr[x]!=9999)
g.drawLine(0,harr[x],img.getWidth(imagepanel),harr[x]);
}
//flag=4;
}
if(flag==1 && clear==0)
{for(int x=0;x<vcount;x++)
g.drawLine(varr[x],0,varr[x],img.getHeight(imagepanel));

}



if(flag==0) //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;
}
if(click==3)
{if(flag2==1)
{g.setColor(Color.white);
g.drawLine(0,mouseY,img.getWidth(imagepanel),mouseY);
System.out.println("fantastic");
}

else
{g.setColor(Color.black);
g.drawLine(0,mouseY,img.getWidth(imagepanel),mouseY);
System.out.println("Nonfantastic");
}


}

}
}


public void sethLine(int arre[] )
{
for(int i=0;i<hcount;i++)
harr[i]=arre[i];
repaint();
}
public void setvLine(int arre[])
{ for(int i=0;i<vcount;i++)
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;mx=mouseX;//flag=4;
my=mouseY;
System.out.println("in mouse press");
System.out.println(mouseX);
System.out.println(mouseY);
imagepanel.setaLine();





}
}

class Mouserelease extends MouseAdapter
{ //public void mouseDragged(MouseEvent e)
// {System.out.println("in mouse drag");
int fl=0;
public void mouseReleased(MouseEvent e)
{mouseX=e.getX();
mouseY=e.getY();
if((mx==mouseX) && (my==mouseY))

{
for(int i=0;i<hcount;i++)
{if(mouseY==harr[i])
{harr[i]=9999;
flag2=1;
fl=1;
System.out.println("in for of mousereleased");
break;
}
}
if(fl==0)
{harr[hcount++]=mouseY;

}



click=3;

imagepanel.repaint();






}

else
{


System.out.println("in mouse realase");
System.out.println(mouseX);
System.out.println(mouseY);
click=2;
imagepanel.setbline();
}

}
}
/*class MouseClick extends MouseAdapter
{public void mouseClicked(MouseEvent e)
{ System.out.println("in mouse click");
int mouseY=e.getY();
for(int i=0;i<hcount;i++)
{if(mouseY==harr[i])
harr[i]=9999;

}
imagepanel.repaint();
}

}
*/
// OpenAction
class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
//... Open a file dialog.
clear=0;
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;
click=0;
System.out.println("in line button");
try{ int array[]= new int[1000];
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;
System.out.println("in wordaction");
try{ int array[]= new int[1000];
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();
imagepanel.revalidate();
System.out.println("in clear button");
clear=1;
flag=2;
imagepanel.repaint();
}



}



/** 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";
}
}
}
15 years ago
hi all,
i am trying to add a mouselistener in a panel which is a part of a frame.....but the listener is not responding to click of mouse........please help me in finding where i m wrong.....

i am attching the whole code


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");
openButton.addActionListener(new OpenAction());
lineButton.addActionListener(new LineAction());
wordButton.addActionListener(new wordAction());
//... 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);
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);




}

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)
g.drawLine(0,y,img.getHeight(imagepanel),y);

}

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(int x,int y)
{
repaint();
}
}



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





}
}


// 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){}
}



}
/** 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";
}
}
}
15 years ago
thanks a lot,...now the cose is working as expected
15 years ago
suppose i want to remove a line already made by previous program on a mouse event and also draw a new line at the new position where i click mouse....i added a mouse event listener class in the imagepanel but it is not working....what to do...???
i m posting the part of code where i made changes...rest of the rogram is same as the previous one
class DrawingPanel extends JPanel
{ public DrawingPanel()
{MouseClickListener listener =new MouseClickListener();
addMouseListener(listener);


}

public 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);

//g.drawLine(0,0,100,100);
}
}

private class MouseClickListener extends MouseAdapter
{ public void MouseClicked(MouseEvent e)
{int mouseX=e.getX();
int mouseY=e.getY();
int width = img.getWidth(imagepanel);
Graphics g=imagepanel.getGraphics();
g.drawLine(0,mouseY,width,mouseY);

}
}
15 years ago
hi,i have created a jframe which loads a image then on click of a button it draws random lines horizontally or vertically dependng upon th button choosed but when th scroll bar is moved th lines drawn are vanishing....i think i need to put reapint somewhere but where???also please tell how can i take the dimensionms of full image ....in the foloowing program i have drawn lines using the dimension of panel....i want lines to b drawn on the whole image and they shud not vanish on scrolling.....please help me




//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 openpic1 extends JFrame
{

// Instance variables
JTextField m_fileNameTF = new JTextField(15);
JFileChooser m_fileChooser = new JFileChooser();
JPanel imagepanel;
Image img;
JavaFilter fJavaFilter;

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

// 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 seg");
JButton wordButton=new JButton("word segment");
openButton.addActionListener(new OpenAction());
lineButton.addActionListener(new LineAction());
wordButton.addActionListener(new wordAction());
//... Create contant pane, layout components
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(openButton);
content.add(lineButton);

content.add(wordButton);
content.add(m_fileNameTF);
// Create JPanel canvas to hold the picture
imagepanel = new DrawingPanel();
imagepanel.setBackground(Color.white);
imagepanel.setPreferredSize(new Dimension(700,500));
// 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);

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

class DrawingPanel extends JPanel
{
public 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);

//g.drawLine(0,0,100,100);
}
}
//public void update(Graphics g)
//{repaint();
//}

}

// OpenAction
class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
//... Open a file dialog.
int retval = m_fileChooser.showOpenDialog(openpic1.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());
m_fileNameTF.setText(file.getName());
}
}
}
//this tries to build lines
class LineAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int width = img.getWidth(imagepanel);
int height = img.getHeight(imagepanel);

System.out.println(width);
System.out.println(height);
//public void paint(Graphics g)
// {g.drawLine(5,10,30,30);
/*drawLines dL = new drawLines(img) ;
DrawingPanel panel;
panel = new DrawingPanel();
panel.add(dL);*/
Graphics g=imagepanel.getGraphics();

for(int x=0;x<=height;x=x+30)

g.drawLine(0,x,width,x);

}


}

class wordAction implements ActionListener{
public void actionPerformed(ActionEvent e)
{
int width=img.getWidth(imagepanel);
int height=img.getHeight(imagepanel);
Graphics g=imagepanel.getGraphics();
for(int x=0;x<=width;x=x+20)
g.drawLine(x,0,x,height);
}
}



}
/** 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";
}
}
15 years ago
i hva problem in loadinga package javax.imageio...ne soln
15 years ago