• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in drawing lines on a panel

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
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";
}
}
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
followingExamine this
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

edit: fix typo and clarify one comment
[ June 07, 2006: Message edited by: Craig Wood ]
 
cindrella ss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot,...now the cose is working as expected
reply
    Bookmark Topic Watch Topic
  • New Topic