aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem in drawing lines on  a panel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in drawing lines on  a panel" Watch "problem in drawing lines on  a panel" New topic

problem in drawing lines on a panel

cindrella ss

Joined: May 30, 2006
Posts: 6
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 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.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();
// constructor
openpic1() {

// set the Text field to Read Only mode

// 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());

// Create JPanel canvas to hold the picture
imagepanel = new DrawingPanel();
imagepanel.setPreferredSize(new Dimension(700,500));
// Create JScrollPane to hold the canvas containing the picture
scroller.setPreferredSize(new Dimension(500,300));
// Add scroller pane to Panel

//Set window characteristics
this.setTitle("File Browse and View");

class DrawingPanel extends JPanel
public void paintComponent(Graphics 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);

//public void update(Graphics g)


// 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());
//this tries to build lines
class LineAction implements ActionListener
public void actionPerformed(ActionEvent e)
int width = img.getWidth(imagepanel);
int height = img.getHeight(imagepanel);

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

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




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)

/** 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";
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
followingExamine this
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535

edit: fix typo and clarify one comment
[ June 07, 2006: Message edited by: Craig Wood ]
cindrella ss

Joined: May 30, 2006
Posts: 6
thanks a lot, the cose is working as expected
I agree. Here's the link:
subject: problem in drawing lines on a panel
Similar Threads
how to reflect the changes made in an inner class in main
to draw and remove lines on click of mouse
problem in attaching mouselistener on a imagepanel
problem in drawing deleting a line on the click of a mouse
problem in loading another image in a JPanel