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
Author

problem in drawing lines on a panel

cindrella ss
Greenhorn

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 ....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";
}
}
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
Greenhorn

Joined: May 30, 2006
Posts: 6
thanks a lot,...now the cose is working as expected
 
I agree. Here's the link: http://aspose.com/file-tools
 
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