7 Replies - 1060 Views - Last Post: 04 May 2008 - 09:43 AM Rate Topic: ***** 1 Votes

#1 GreenCamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 08

Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 02:56 PM

I am having trouble with the teleprompter I am trying to make. It is being made as a proof of concept for a program that would manage files when a person is debating. I am now just trying to see if a person will be able to use a teleprompter to read debate cases, as it would in this program I will try to make over the summer.

Right now, all it does is open a window, and does not display any text.

No, this is not for school work, so you are not helping anybody cheat.



import java.awt.Container;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.lang.InterruptedException;
public class DebateLine extends JFrame implements MouseListener
{
	private DPanel canvas;
	public DebateLine()
	{
		super("Debate Software- Proof of Concept");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,600);
		setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);//I just copied this code off the net, it maximizes the window
		Container con = this.getContentPane();
		canvas = new DPanel();
		con.add(canvas);
		setVisible(true);
	}
	private boolean clicked=false;
	public void readLines()
	{
		canvas.repaint();
		while(!canvas.getWhetherDone())
		{
			while(!clicked)
			{
				try{
					Thread.sleep(10);
				}catch(InterruptedException e)
					{return;}
			}
			canvas.repaint();
			canvas.show();
			
		}
		
	}
	public static void main(String arg[])
	{
		DebateLine deb=new DebateLine();
		deb.readLines();
	}
	public void mouseClicked(MouseEvent e)
	{
		clicked=true;
	}
	public void mousePressed(MouseEvent e) 
	{
	}
	public void mouseReleased(MouseEvent e)
	{
	}
	public void mouseEntered(MouseEvent e) 
	{
	}
	public void mouseExited(MouseEvent e)
	{
	}
}


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JPanel;
import java.io.IOException;
public class DPanel extends JPanel
{
	import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Font;
import java.awt.FontMetrics;
import javax.swing.JPanel;
import java.io.IOException;
public class DPanel extends JPanel
{
	private String letters;
	private String[] feed;
	private int distanceInList;
	private boolean done=false;
	public boolean getWhetherDone(){return done;}
	
	public DPanel()
	{
		FileChararray generator = new FileChararray();
		distanceInList=0;
		try
		{
			feed=generator.entireText();
		}
		catch(IOException e)
		{
			return;
		}
	}
	public void paintComponent(Graphics g)
	{
		
		super.paintComponent(g);
		Graphics2D g2d=(Graphics2D)g;
		g2d.setFont(new Font("serif",Font.BOLD,80));
		FontMetrics fm=g2d.getFontMetrics();
		
		int down=20;
		for(int spot=distanceInList;spot<feed.length;)
		{
			if(fm.stringWidth(letters+feed[spot])<getWidth())//as long as there is enough space for words, more words will be added
			{
				letters+=feed[spot]+" ";
				distanceInList++;
			}
			else if(getHeight()<down+fm.getHeight())// when the line is full of words,the line is displayed.  
			{
				g2d.drawString(letters,0,down);
				down=down+fm.getHeight();
				letters=feed[spot];
				distanceInList++;
			}
			else
			{
				letters=feed[spot];//when there is no more space for more lines of text
				down=20;
				distanceInList++;
				return;
			}
		}
		done=true;
	}
}

This post has been edited by GreenCamper: 03 May 2008 - 02:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Programming newbie(in general) having trouble with teleprompter

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,139
  • Joined: 06-March 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 03:07 PM

miss your FileChararray class to test your code
Was This Post Helpful? 0
  • +
  • -

#3 GreenCamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 03:59 PM

just slap any document into the program.

Oh, and char array is misleading, it returns a string array.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class FileChararray
{
	public String[] entireText()throws IOException
	{
		String[] returnArray= new String[300000];
		Scanner input=new Scanner(new File("sample.dat"));
		int count = 0;
		while(input.hasNext())
		{
			returnArray[count]=input.next();
			count++;
		}
		String[] finalArray=new String[count-1];
		for(int spot=0;spot<finalArray.length-1;spot++)
			finalArray[spot]=returnArray[spot];
		return finalArray;
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,139
  • Joined: 06-March 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 04:08 PM

OK do you have to post as an attachment sample.dat or I can just do

class FileChararray {

	String[] str = {{"abc", "def", "ghi"}};

   String[] entireText() {
	   return str;
   }
}


Was This Post Helpful? 0
  • +
  • -

#5 GreenCamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 05:43 PM

Well its designed for large documents that have too much text to put in one screen and still have the text large enough to be able to read from a distance. Here is a sample document that has a translated copy of the Magna Carta. It was the first thing on my mind.

it is a .txt file.

Attached File(s)


This post has been edited by GreenCamper: 03 May 2008 - 05:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,139
  • Joined: 06-March 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 03 May 2008 - 06:27 PM

View PostGreenCamper, on 3 May, 2008 - 05:43 PM, said:

Well its designed for large documents that have too much text to put in one screen and still have the text large enough to be able to read from a distance. Here is a sample document that has a translated copy of the Magna Carta. It was the first thing on my mind.

it is a .txt file.

Ya but I mean, for test purpose......
do I need 5 000 words ?
Was This Post Helpful? 0
  • +
  • -

#7 GreenCamper  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-April 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 04 May 2008 - 12:11 AM

Well the documents the teleprompter will need to scan will be similar in size to the magna carta.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8022
  • View blog
  • Posts: 31,139
  • Joined: 06-March 08

Re: Programming newbie(in general) having trouble with teleprompter

Posted 04 May 2008 - 09:43 AM

import java.awt.Container;
import javax.swing.JFrame;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.lang.InterruptedException;
public class DebateLine extends JFrame implements MouseListener
{
	private DPanel canvas;
	public DebateLine()
	{
		super("Debate Software- Proof of Concept");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600,600);
		setExtendedState(this.getExtendedState()|JFrame.MAXIMIZED_BOTH);//I just copied this code off the net, it maximizes the window
		Container con = this.getContentPane();
		canvas = new DPanel();
		con.add(canvas);
		setVisible(true);
	}



you have a
   canvas.addMouseListener(this);



missing after you created the DPanel

	public void readLines()
	{
		canvas.repaint();
		while(!canvas.getWhetherDone())
		{
			while(!clicked)
			{
				try{
					Thread.sleep(10);
				}catch(InterruptedException e)
					{return;}
			}
			canvas.repaint();
			canvas.show();
			
		}
		
	}



Your first call to repaint() will call the paint() method of DPanel
whatever you do you in your paint() method you will ended by setting done to true at the end of the method
so your
	while(!canvas.getWhetherDone())


will be true only one time (may be 2 if your system is really busy)

I also put a println before your call to drawString() the code never pass there

I also suggest that you rename your paintComponent() simply paint()
if you do so also change the call super.paintComponent(g); by super.paint(g);

A few println() in your paint() should help you trace the problem

Now I had problem to follow the logic in your paint() method.
You have to understand that when you call super.paint(g); the first think it will do is to repaint your background (and erase everything you had drawString() before). So everytime your paint() is called to have to redraw all the text you want displayed.

Why don't you just use a JTextArea() enable wordWrap on it and just .append() to it your word one by one. You wont have to overload paint() it will be automatically do for you and you won't have to worry if the user resizes the window.

Or make a big String of your array of words then display in the textArea from one index to another.

This post has been edited by pbl: 04 May 2008 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1