• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

problem in drawing lines on a panel  RSS feed

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
// 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";
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
followingExamine this
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

edit: fix typo and clarify one comment
[ June 07, 2006: Message edited by: Craig Wood ]
cindrella ss
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot,...now the cose is working as expected
This will take every ounce of my mental strength! All for a tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!