-
Java help please
hey I need help with getting my program to read from a file and store the information and then going on to the next line and doing the same.
The file contains information to draw a shape, for example:
rectangle 20 20 70 70 red
oval 30 50 60 70 blue
etc..
where the numbers are coordinates on an applet to draw the starting corner points.
I need it read the first line and store the variables so that i can then draw the shapes when i run it as an applet.
This isn't a complicated program but i'm having difficulty reading from a file and using the info respectively to draw the appropriate shapes. I need it to read from any similar file too so it needs to be able to do that.
Its basically a shape drawer program.
My code so far...(bad i know but i'm learning)
import java.io.*;
import java.awt.*;
import java.awt.Graphics;
import javax.swing.JApplet;
import java.util.*;
public class Program_2 extends JApplet
{
public void init()
{
Scanner inFile = new Scanner ( new FileReader("notsosmileyface.txt"));
{
String shape = inFile.next();
int x = inFile.nextInt();
int y = inFile.nextInt();
int width = inFile.nextInt();
int height = inFile.nextInt();
int secx = inFile.nextInt();
int secy = inFile.nextInt();
String color = inFile.next();
}
}
public void paint( Graphics g )
{
try
{
readInputFile(g); //unsure how to adjust this to read the file.
int numLines = null;
while (( numLines = inFile.readLine()) != null);
if (color.equals(black))
{
g.setColor(Color.black);
}
else if (color.equals(blue))
{
g.setColor(Color.blue);
}
else if (color.equals(cyan))
{
g.setColor(Color.cyan);
}
else if (color.equals(darkGray))
{
g.setColor(Color.darkGray);
}
else if (color.eqauls(gray))
{
g.setColor(Color.gray);
}
else if (color.equals(green))
{
g.setColor(Color.green);
}
else if (color.equals(lightGray))
{
g.setColor(Color.lightGray);
}
else if (color.equals(magenta))
{
g.setColor(Color.magenta);
}
else if (color.equals(orange))
{
g.setColor(Color.orange);
}
else if (color.equals(pink))
{
g.setColor(Color.pink);
}
else if (color.equals(red))
{
g.setColor(Color.red);
}
else if (color.equals(white))
{
g.setColor(Color.white);
}
else
{
g.setColor(Color.yellow);
}
if ( shape.equals(rectangle))
{
g.fillRec(x,y,width,height);
}
else if (shape.equals(oval))
{
g.fillOval(x,y,width,height);
}
else if (shape.equals(arc))
{
g.fillArc(x,y,width,height,secx,secy);
}
else
{
g.drawLine(x,y,width,height);
}
}
}
catch (FileNotFoundException fnf )
{
System.out.println("That file was not found.");
}
public void readInputFile( Graphics g ) throws FileNotFoundException
{
inFile.close();
}
}
Any helpful tips from various people would be greatly appreciated.
I may be making this harder then it needs to be but it is difficult to read the file and use the information to draw for me. thanks.
-
Hello,
If you can read a line you may split it with StringTokenizer and convert the String-s
to Integer-s. This is just a solution when you don't have time for debug problem.
-
hey, mostly what i need help in is that part exactly, reading each line and being able to use it. I need the program to be abel to read from any similar file and i am having trouble figuring out where the file reader should be and how i should initialize the applet and run the paint method in the applet with the information from the file.
Similar Threads
-
Replies: 2
Last Post: 06-14-2006, 03:16 PM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By Anonymous in forum Java
Replies: 0
Last Post: 01-31-2002, 11:08 PM
-
By JJ in forum Enterprise
Replies: 1
Last Post: 07-06-2000, 04:50 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks