10 Replies - 971 Views - Last Post: 18 December 2008 - 12:48 PM Rate Topic: -----

#1 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Reading in Lines and using Them in applet

Posted 18 December 2008 - 11:28 AM

Trying to get the x Postion and Y postition and Diamater of the circle from the file Ornaments, and then make fillOval with the information from it.
//Exam 3 Program
//December 18, 2008

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ShabanExam3 extends Applet
{
	public static void main(String[] args)
  {
	try
		{
		ObjectInputStream inputStream =
			new ObjectInputStream(new FileInputStream("Ornaments.dat"));
		int ornamentsX [] = new int [10];
		int ornamentsY [] = new int [10];
		int ornamentsDiameter [] = new int [10];
		for(int count = 0; count < 10; count++)
	 	 {
	 	 	ornamentsX[count] = inputStream.read();
	 	 	ornamentsY[count] = inputStream.read();
	 	 	ornamentsDiameter[count] = inputStream.read();
		 }
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Cannot find file Ornaments.dat.");
			System.exit(0);
		}
		catch(IOException e)
		{
			System.out.println("Problems with input from Ornaments.dat.");
		}
  }

	public static final int WINDOW_WIDTH = 300;
	public static final int WINDOW_HEIGHT = 475;
	private Color randomColor;
	int[] xPoints = {150, 10, 290};
	int[] yPoints = {30, 400, 400};

	Scanner keyboard = new Scanner(System.in);
	public void init()
	{
	//set the size and background color of itself (the applet)
	this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	this.setBackground(Color.red);
	randomColor = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
	}

	public void paint(Graphics g)
	{
	  g.setColor(new Color(0, 0, 0));
	  g.fillRect(140, 400, 20, 40);
	  g.setColor(Color.green);
	  g.fillPolygon(xPoints, yPoints, 3);
	  for(int count = 0; count < 10; count++)
	  {
	  g.setColor(randomColor);
	  g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
	  }
	}
}

This post has been edited by xxwolfsrainxx: 18 December 2008 - 11:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading in Lines and using Them in applet

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,988
  • Joined: 20-September 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 11:33 AM

You can't use File in an applet unless it's signed or the security manager is relaxed in some other way. If you hold that file in the applet's codebase, you can do

ObjectInputStream in = new ObjectInputStream(getClass().getResourceAsStream("/Ornaments.dat"));


Was This Post Helpful? 0
  • +
  • -

#3 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 11:45 AM

Okay I got the reading to work but I get these errors.
H:\Desktop\Java\ShabanExam3.java:63: cannot find symbol
symbol : variable ornamentsX
location: class ShabanExam3
g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
^
H:\Desktop\Java\ShabanExam3.java:63: cannot find symbol
symbol : variable ornamentsY
location: class ShabanExam3
g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
^
H:\Desktop\Java\ShabanExam3.java:63: cannot find symbol
symbol : variable ornamentsDiameter
location: class ShabanExam3
g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
^
3 errors
//December 18, 2008

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ShabanExam3 extends Applet
{
	public static final int WINDOW_WIDTH = 300;
	public static final int WINDOW_HEIGHT = 475;
	private Color randomColor;
	int[] xPoints = {150, 10, 290};
	int[] yPoints = {30, 400, 400};
	Scanner keyboard = new Scanner(System.in);
	public void init()
	{
		//set the size and background color of itself (the applet)
		this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		this.setBackground(Color.red);
		randomColor = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
		
		try
			{
			ObjectInputStream inputStream =
				new ObjectInputStream(new FileInputStream("Ornaments.dat"));
			int ornamentsX [] = new int [10];
			int ornamentsY [] = new int [10];
			int ornamentsDiameter [] = new int [10];
			for(int count = 0; count < 10; count++)
		 	 {
		 	 	ornamentsX[count] = inputStream.read();
		 	 	ornamentsY[count] = inputStream.read();
		 	 	ornamentsDiameter[count] = inputStream.read();
			 }
			}
			catch(FileNotFoundException e)
			{
				System.out.println("Cannot find file Ornaments.dat.");
				System.exit(0);
			}
			catch(IOException e)
			{
				System.out.println("Problems with input from Ornaments.dat.");
	 		}
	}

	public void paint(Graphics g)
	{
	  g.setColor(new Color(0, 0, 0));
	  g.fillRect(140, 400, 20, 40);
	  g.setColor(Color.green);
	  g.fillPolygon(xPoints, yPoints, 3);
	  for(int count = 0; count < 10; count++)
	  {
	  g.setColor(randomColor);
	  g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
	  }
	}
}

This post has been edited by xxwolfsrainxx: 18 December 2008 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 11:57 AM

It is clear why you recieve these errors. These symbols have been defined in init() methods and cannot be referenced through method paint(). They are local variable not fields of the class. You can define them as fields of class.

----------------------------------------------
if it was helpful click the link below

This post has been edited by amir_ju: 18 December 2008 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 11:58 AM

View Postamir_ju, on 18 Dec, 2008 - 10:57 AM, said:

It is clear why you recieve these errors. These symbols have been defined in init() methods and cannot be referenced through method paint(). They are local variable not fields of the class. You can change them to fields of class.




----------------------------------------------
if it was helpful click the link below

Thanks. Could you explain to me though how I change them to fields of the clasS?
Was This Post Helpful? 0
  • +
  • -

#6 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:14 PM

You may modify your code like this:(syntax errors have gone, However I don't gurantee that it works correctly)

//Exam 3 Program
//December 18, 2008

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ShabanExam3 extends Applet
{
	public static void main(String[] args)
	{
		try
		{
			ObjectInputStream inputStream =
				new ObjectInputStream(new FileInputStream("Ornaments.dat"));
			for(int count = 0; count < 10; count++)
			{
				ornamentsX[count] = inputStream.read();
				ornamentsY[count] = inputStream.read();
				ornamentsDiameter[count] = inputStream.read();
			}
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Cannot find file Ornaments.dat.");
			System.exit(0);
		}
		catch(IOException e)
		{
			System.out.println("Problems with input from Ornaments.dat.");
		}
	}

	public static final int WINDOW_WIDTH = 300;
	public static final int WINDOW_HEIGHT = 475;
	private static int ornamentsX [] = new int [10];
	private static int ornamentsY [] = new int [10];
	private static int ornamentsDiameter [] = new int [10];
	private Color randomColor;
	int[] xPoints = {150, 10, 290};
	int[] yPoints = {30, 400, 400};

	Scanner keyboard = new Scanner(System.in);
	public void init()
	{
		//set the size and background color of itself (the applet)
		this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		this.setBackground(Color.red);
		randomColor = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
	}

	public void paint(Graphics g)
	{
		g.setColor(new Color(0, 0, 0));
		g.fillRect(140, 400, 20, 40);
		g.setColor(Color.green);
		g.fillPolygon(xPoints, yPoints, 3);
		for(int count = 0; count < 10; count++)
		{
			g.setColor(randomColor);
			g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);
		}
	}
}


----------------------------------------------
if it was helpful click the link below


Sorry another syntax error is at line
g.fillOval(ornamentsX, ornamentsY, ornamentsDiameter);


fillOval is not applicable for (int[], int[], int[])
Was This Post Helpful? 0
  • +
  • -

#7 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:15 PM

Okay Amir that works but then the ornaments don't show. Is it becuase the data isn't being read?
Was This Post Helpful? 0
  • +
  • -

#8 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:28 PM

I detected some errors:
you should use readInt() rather than read()
you should note that fillOval works like this fillOval(x, y, width, height).
I've corrected them here. But I steel don't know what are you trying to display.
//Exam 3 Program
//December 18, 2008

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Scanner;

public class ShabanExam3 extends Applet
{
	public static void main(String[] args)
	{
		try
		{
			ObjectInputStream inputStream =
				new ObjectInputStream(new FileInputStream("Ornaments.dat"));
			for(int count = 0; count < 10; count++)
			{
				ornamentsX[count] = inputStream.readInt();
				ornamentsY[count] = inputStream.readInt();
				ornamentsDiameter[count] = inputStream.readInt();
			}
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Cannot find file Ornaments.dat.");
			System.exit(0);
		}
		catch(IOException e)
		{
			System.out.println("Problems with input from Ornaments.dat.");
		}
	}

	public static final int WINDOW_WIDTH = 300;
	public static final int WINDOW_HEIGHT = 475;
	private static int ornamentsX [] = new int [10];
	private static int ornamentsY [] = new int [10];
	private static int ornamentsDiameter [] = new int [10];
	private Color randomColor;
	int[] xPoints = {150, 10, 290};
	int[] yPoints = {30, 400, 400};

	Scanner keyboard = new Scanner(System.in);
	public void init()
	{
		//set the size and background color of itself (the applet)
		this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		this.setBackground(Color.red);
		randomColor = new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
	}

	public void paint(Graphics g)
	{
		g.setColor(new Color(0, 0, 0));
		g.fillRect(140, 400, 20, 40);
		g.setColor(Color.green);
		g.fillPolygon(xPoints, yPoints, 3);
		for(int count = 0; count < 10; count++)
		{
			g.setColor(randomColor);
			g.fillOval(ornamentsX[count], ornamentsY[count], ornamentsDiameter[count], ornamentsDiameter[count]);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:31 PM

Nvm the data is being read it just doesn't read that data into the for loop where it creates the ornaments

Trying to display ovals based on the ints from the dat file.
Was This Post Helpful? 0
  • +
  • -

#10 amir_ju  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 82
  • Joined: 20-June 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:42 PM

How did you create the ornaments.dat? Did you typed it yourself?
Was This Post Helpful? 0
  • +
  • -

#11 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Reading in Lines and using Them in applet

Posted 18 December 2008 - 12:48 PM

Okay I got it to work. Thanks you guys for your great assistance :D. Always give good advice. ^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1