0

I've written a code tat reads x and y co ordinates from a txt file and drawlines on the panel. i need to save the written geometry on the panel as a jpeg image, can any one help me out wat should be written in the action listener part of the btSaveScript in the below code

//MynewGUI.java

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MynewGUI extends JFrame implements ActionListener {

	String strTitle = "Geometrical Feature Recognition";
	JFrame frmMain = new JFrame(strTitle);
	DrawPanel1 dp = new DrawPanel1();
	ShowPanel1 sp = new ShowPanel1();
	JButton btNewScript = new JButton("New");
	JButton btSaveScript = new JButton("Save");
	JButton btClearScript = new JButton("Clear");
	JButton btRecognize = new JButton("Recognize");
	
	
	int CurrentScript=0;
    int stroke;

	public MynewGUI() {
		// gui definitions
		JFrame.setDefaultLookAndFeelDecorated(true);
		frmMain.setLayout(new GridLayout(4, 3));
		    
		frmMain.setResizable(true);
		frmMain.setBounds(300, 300, 800, 600);
		frmMain.getContentPane().setLayout(null);
		frmMain.setLocationRelativeTo(null);
		frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		dp.setBounds(20, 20, 580, 520);
		sp.setBounds(620, 300, 165, 200);
		sp.setBackground(Color.WHITE);
		frmMain.getContentPane().add(dp);
		frmMain.getContentPane().add(sp);

		btNewScript.setBounds(frmMain.getWidth() - 180, 170, 160, 20);
		btNewScript.addActionListener(this);
		frmMain.getContentPane().add(btNewScript);

		btClearScript.setBounds(frmMain.getWidth() - 180, 200, 160, 20);
		btClearScript.addActionListener(this);
		btClearScript.setEnabled(false);
		frmMain.getContentPane().add(btClearScript);

		btSaveScript.setBounds(frmMain.getWidth() - 180, 230, 160, 20);
		btSaveScript.addActionListener(this);
		btSaveScript.setEnabled(false);
		frmMain.getContentPane().add(btSaveScript);

		btRecognize.setBounds(frmMain.getWidth() - 180, 260, 160, 20);
		btRecognize.addActionListener(this);
		btRecognize.setEnabled(false);
		frmMain.getContentPane().add(btRecognize);

		frmMain.setVisible(true);
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == btNewScript) {
			
			dp.Clear();
			dp.setEnabled(true);

			btClearScript.setEnabled(true);
			btNewScript.setEnabled(false);
			File selectedFile = null;
			if(CurrentScript<=1)
			{
				btSaveScript.setEnabled(true);
			}

			JFileChooser dlg = new JFileChooser();
			FileFilter ft = new FileNameExtensionFilter("Text Files","txt");
			dlg.addChoosableFileFilter(ft);

			int rVal = dlg.showOpenDialog(this);

			if (rVal == JFileChooser.APPROVE_OPTION) {

				selectedFile = dlg.getSelectedFile();

			}

			try {

				//String fileName = selectedFile.getAbsolutePath();
				FileInputStream fstream = new FileInputStream(selectedFile);
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(in));
				String strLine;
				java.util.ArrayList<Point2D> arrPoints = new java.util.ArrayList<Point2D>();

				while ((strLine = br.readLine()) != null) {

					StringTokenizer tk = new StringTokenizer(strLine,"\t");
					while (tk.hasMoreTokens()) {
						Double X = Double
						.parseDouble(tk.nextToken().toString());
						Double Y = Double
						.parseDouble(tk.nextToken().toString());
						Double Y_MAX = 450.0;
						Double Y_VALUE = Y_MAX - Y;

						arrPoints.add(new Point2D.Double(X, Y_VALUE));
					}

				}
				dp.setDraw(true);
				dp.setArrPoints(arrPoints);
				dp.repaint();
			} catch (IOException e) {

				e.printStackTrace();

			}
		}

		else if (evt.getSource() == btClearScript) {
			dp.Clear();
			btNewScript.setEnabled(true);
			btClearScript.setEnabled(false);
		}
                  else if(evt.getSource() == btSaveScript)  {

//done know wat should be written here.
                  }
	}
	

	
	public static void main(String[] args) {
		new MynewGUI();
	}
}

//DrawPanel1.java

import java.awt.Graphics2D;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.Graphics;


public class DrawPanel1 extends JPanel {

	private Boolean draw = false;
	

	private java.util.ArrayList<Point2D> arrPoints = new java.util.ArrayList<Point2D>();

	public Boolean getDraw() {
		return draw;
	}

	public void setDraw(Boolean draw) {
		this.draw = draw;
	}

	public java.util.ArrayList<Point2D> getArrPoints() {
		return arrPoints;
	}

	public void setArrPoints(java.util.ArrayList<Point2D> arrPoints) {
		this.arrPoints = arrPoints;
	}

	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		if (draw) {
			g.translate(150, 60);
			for (int i = 0; i < arrPoints.size() - 1; i++) {
				Point2D p1 = arrPoints.get(i);
				Point2D p2 = arrPoints.get(i + 1);
				g2.draw(new Line2D.Double(p1.getX(), p1.getY(), p2.getX(), p2
						.getY()));
               	}	
		}
	}
	public void Clear()
	{
		draw=false;
		arrPoints.clear();
		repaint();
	}
}

//ShowPanel1.java

import java.awt.Graphics2D;
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;

public class ShowPanel1 extends JPanel {
	private Boolean draw = false;

	public Boolean getDraw() {
		return draw;
	}

	public void setDraw(Boolean draw) {
		this.draw = draw;
	}

	protected void paintComponent(Graphics g)

	{
		g.setColor(new Color(0, 0, 128));
		g.setFont(new Font("Verdana", Font.BOLD, 12));

		super.paintComponent(g);

		Graphics2D g2 = (Graphics2D) g;

	}
}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.