Pip Vo

Greenhorn
+ Follow
since Jul 18, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
(keep public parts private until JForum day)
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt
Moderation Tools

Recent posts by Pip Vo

Hi Ranchers
I'm still stuck on this. Got the following code
and think it's pretty close to what I need,
but how do I integrate it into my existing JFrame
(if at all?). I've read about scroll panes in
Core Java and know how to add JPEGS and GIFS to the window ... but need to put my own program output inside (please see previous post for code ... sorry it isn't formatted. I need to learn about the UBB(?) code). Thank you!
class scrollpaneimage extends JApplet
{
scrollpaneimage()
{
Container contentPane = getContentPane();
JLabel jlabel = new JLabel(new ImageIcon("RectangleV3"));
JScrollPane jscrollpane = new JScrollPane(jlabel);
contentPane.add(jscrollpane);
}
}
15 years ago
Hi,
Thanks for all the advice. I should've included
my code for generating graphics. Still puzzled on how to proceed. From my book, I think
I should draw on the JPanel and then add it to the
content pane of my JFrame, but not sure *how* to do this. Thanks !
CODE FOR GRAPHICS:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.StringTokenizer;
// This is like the FontDemo applet in volume 1, except that it
// uses Java 2D APIS to define and render the graphics and text.
public class RectangleV3 extends JApplet
{
final static int maxCharHeight = 14;
final static int minFontSize = 12;
final static Color bg = Color.yellow;
final static Color fg = Color.red;
final static Color red = Color.red;
final static Color white = Color.white;
final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER);
final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke
(1.0f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0f,dash1,0.0f);
static double data1[] = new double[6000]; // MAX INDEX NEEDS AUTOMATION.
static String name1[] = new String[6000]; // MAX INDEX NEEDS AUTOMATION.
static String temp1[] = new String[6000]; // MAX INDEX NEEDS AUTOMATION.
static char array1[] = new char[25];
static int h = 0;
static int count = 0;
static int count2 = 0;
static int i = 0;
static int j = 0;
Dimension totalSize;
FontMetrics fontMetrics;
public void init()
{
// Initialize drawing colors.
setBackground(Color.white);
setForeground(Color.red);
}
public void drawChars(char[] data, int offset, int length, int x, int y)
{
System.out.println (data);
}
FontMetrics pickFont(Graphics2D g2, String longString, int xSpace)
{
boolean fontFits = false;
Font font = g2.getFont();
FontMetrics fontMetrics = g2.getFontMetrics();
int size = font.getSize();
String name = font.getName();
int style = font.getStyle();
while (!fontFits)
{
if ((fontMetrics.getHeight() <= maxCharHeight)
&&(fontMetrics.stringWidth(longString) <= xSpace))
{
fontFits = true;
}
else
{
if (size <= minFontSize)
{
fontFits = true;
}
else
{
g2.setFont(font = new Font(name,style,--size));
fontMetrics = g2.getFontMetrics();
}
}
}
return fontMetrics;
}
public void paint(Graphics g)
{
// g.drawChars (array1, 10, 10, 10, 10);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Dimension d = getSize();
int gridWidth = d.width/6;
int gridHeight = d.height/2;
fontMetrics = pickFont(g2, "Filled and Stroked GeneralPath", gridWidth);
Color fg3D = Color.lightGray;
g2.setPaint(fg3D);
g2.draw3DRect(0, 0, d.width-1, d.height-1, true);
g2.draw3DRect(3, 3, d.width-7, d.height-7, false);
g2.setPaint(fg);
int x = 5;
int y = 7;
int rectWidth = gridWidth-2*x;
int stringY = gridHeight-3-fontMetrics.getDescent();
int rectHeight = stringY-fontMetrics.getMaxAscent()-y-2;
int i = 0;
// int line = 1; // NEEDS AUTOMATION
// int line = 2; // NEEDS AUTOMATION
// int line = 3; // NEEDS AUTOMATION
int line = 1921; // NEEDS AUTOMATION

g2.setStroke(wideStroke);
g2.setPaint(Color.blue);
for (i = 0; i <= line; i++)
{
// System.out.println("(x,y,w,h): "+data1[0] +" " +data1[1] +" " +100 +" "+data1[2]);
// System.out.println("(x,y,w,h): "+data1[3] +" " +data1[4] +" " +100 +" "+data1[5]);
// System.out.println("(x,y,w,h): "+data1[6] +" " +data1[7] +" " +100 +" "+data1[8]);
// System.out.println("(x,y,w,h): "+data1[9] +" " +data1[10] +" " +100 +" "+data1[11]);
// ACTUAL DATA
g2.setPaint(Color.yellow);
g2.fill(new Rectangle2D.Double(data1[3*i],data1[3*i+1],100,data1[3*i+2]));
g2.setPaint(Color.black);
g2.drawString((String)name1[2*i], (int)data1[3*i], (int)data1[3*i+1]);
g2.drawString((String)name1[2*i+1], (int)data1[3*i], (int)data1[3*i+1]+8);

System.out.println ("X: " +data1[3*i]);
System.out.println ("Y: " +data1[3*i+1]);
System.out.println ("HEIGHT: " +data1[3*i+2]);
System.out.println ("NAME1: " +name1[2*i]);
System.out.println ("NAME2: " +name1[2*i+1]);

// g2.drawString("FROM TEST DATA", 150, 200);
// g2.draw(new Rectangle2D.Double(0,150,50,50));
// g2.draw(new Rectangle2D.Double(50,200,50,50));
// g2.draw(new Rectangle2D.Double(0,250,50,50));
// g2.draw(new Rectangle2D.Double(50,275,50,50));
}
// fill and stroke GeneralPath
int x3Points[] = {x, x+rectWidth, x, x+rectWidth};
int y3Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,x3Points.length);
filledPolygon.moveTo(x3Points[0], y3Points[0]);
for (int index = 1; index < x3Points.length; index++)
{
filledPolygon.lineTo(x3Points[index], y3Points[index]);
};
}
public static void get_data1()
{
int count1 = 0;
int count2 = 0;
int j = 0;
String line;
// GET COORDINATES. THIS TRY BLOCK WAS INSERTED BY NU VO.
try
{
BufferedReader br = new BufferedReader(new FileReader("tile1.txt"));

while ((line = br.readLine()) != null)
{
System.out.println("CURRENT LINE: " +line);
StringTokenizer st = new StringTokenizer(line);
try
{
while (st.hasMoreTokens())
{
// System.out.println("J: " +j);
temp1[j] = st.nextToken();
// System.out.println("TEMP: " +temp1[j]);
data1[count1] = Double.parseDouble(temp1[j]);
// System.out.println("DATA: " +data1[count1]);
count1++;
j++;
}
}
catch(NumberFormatException e)
{System.out.println(e);
name1[count2] = String.valueOf(temp1[j]);
System.out.println("FILL NAME: " +name1[count2]);count2++;
name1[count2] = st.nextToken(); count2++;}
} // end while
} // end try
catch(IOException e) {System.out.println("ERROR");}
}
public static void main(String s[])
{
JFrame f = new JFrame("RectangleV3");
// JScrollBar j = new JScrollBar();
// JScrollPane scrollPane = new JScrollPane(f);
// contentPane.add(scrollPane, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JApplet applet = new RectangleV3();
f.getContentPane().add("Center", applet);
applet.init();
f.pack();
f.setSize(new Dimension(800,800));
f.show();
get_data1(); // This gets rectangle coordinates.
}
}
15 years ago
Hi Ranchers,
I need advice on how to place my graphics
output into a scrollable window. Right now,
it's displayed in the content pane of my
JFrame and getting cut off b/c there's no scroll
bar.
I got Sun's tutorial demo program:
http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/ScrollDemo.java
but it display a JPEG, and I want to display
with my graphics output. Any advice on how to
do this or where to read up on it? Sun's page
doesn't have the answer and neither does my
Core Java book. Thanks!
15 years ago
Dave:
Thanks. I changed my file to 123.0, 456.0, etc.
but still get wrong results (ERROR: NOT DOUBLE). This is driving me crazy.
15 years ago
Thank you. I decided to use a different converter:
name1[count2] = String.valueOf(data1[count1])
so my code is now:
import java.io.*;
import java.util.StringTokenizer;
public class catV2
{
static String temp1[] = new String[100];
static String name1[] = new String[100];
static double data1[] = new double[100];
static int count1 = 0;
static int count2 = 0;
static int i = 0; static int j = 0;
static int k = 0;
public static void main (String args[])
{
String line;

try
{
BufferedReader br = new BufferedReader
(new FileReader(args[i]));
while ((line = br.readLine()) != null)
{
System.out.println("CURRENT LINE: " +line);
StringTokenizer st = new StringTokenizer(line);
try
{
while (st.hasMoreTokens())
{
data1[count1] = Double.parseDouble(st.nextToken());
System.out.println("DATA: " +data1[count1]);
count1++;
j++;
} // end while
} // end inner try
catch (NumberFormatException e)
{System.out.println("ERROR: NOT DOUBLE");
//name1[count2] = double.toString(data1[count1]);
name1[count2] = String.valueOf(data1[count1]);
System.out.println("NAME: " +name1[count2]); count2++;}
count2++;
k++;
//}
} // end while
} // end try
catch(IOException e)
{System.err.println("ERROR: " +e);}

}
}
This compiles fine, but gives me weird results.
My sample file content:
123 ABC111
456 DEF222
789 GHI333
GR8 111111
Results:
reisling>>javac catV2.java
reisling>>java catV2 text.txt
CURRENT LINE: 123 ABC111
DATA: 123.0
ERROR: NOT DOUBLE
NAME: 0.0
CURRENT LINE: 456 DEF222
DATA: 456.0
ERROR: NOT DOUBLE
NAME: 0.0
CURRENT LINE: 789 GHI333
DATA: 789.0
ERROR: NOT DOUBLE
NAME: 0.0
CURRENT LINE: GR8 111111
ERROR: NOT DOUBLE
NAME: 0.0
Why is 123 not a double? Please help. Thank you.
15 years ago
Hi Ranchers:
Am having problems with data type determination
and conversion.
I want to put all non double tokens into my name1 array and all doubles into data1 array.
CODE:

I get the following error message:
cannot resolve symbol
symbol : method doubleToString (double,int,int)
I got this method from Sun's page, but there's
not a whole lot written about it. Maybe I need an
example of it being used. Would appreciate
any advice. Thank you!
Pip
Edited by Dave to put code in code tags
[ February 12, 2002: Message edited by: Dave Vick ]
15 years ago
Cindy,
Thanks for the reply.
I need to find out what tokens are doubles
and put them in the data1 array. Anything else
(like G7889) goes into the name1 array.
I've used StreamTokenizer in an earlier version,
and something like G7889 gets split up into G and 7889 which I don't want. So am not sure about
using again and trying to put StringTokenizer
inside it.
Pip
15 years ago
Does anyone know how to find the data type of
st.nextToken in the code below? I can't use
streamTokenizer's TT_WORD or TT_NUMBER b/c need
to parse words like G7790 that have both #s and
letters and streamTokenizer gets confused. Need something specific to stringTokenizer. Searched Sun's page w/ no luck. TIA

[ January 29, 2002: Message edited by: Cindy Glass ]
15 years ago
Thanks for the reply. My error messages:
tokenizer2.java:21: cannot resolve symbol
symbol : constructor StringTokenizer (java.io.FileReader)
location: class java.util.StringTokenizer
StringTokenizer st = new StringTokenizer(filereader);
This code came from Sun's API webpage ... so am
surprised it's not working.
15 years ago
Dear Ranchers:
I need to parse strings (but don't
care if they're letters or #s). I was using
StreamTokenizer b4, but a post suggested
StringTokenizer so switched to that and still
get errors. Please advise on the code below. Thanx
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.io.*;
import java.util.StringTokenizer;
public class tokenizer2
{
public static void get_data1()
{
try
{
FileReader filereader = new FileReader("file.txt");
StringTokenizer st = new StringTokenizer(filereader);

System.out.println("Following parameters will be used in RECTANGLE object:");
while (st.hasMoreTokens())
{
println(st.nextToken());
}
filereader.close();
} // end try
catch(IOException e) {System.out.println("ERROR!");}
} // end get_data1()

public static void main(String s[])
{
get_data1();
}
}
15 years ago
Hi,
Apologies if this is a repeat. Netscape crashed. My program below
parses #s from a file (this part works fine) and then passes these #s to an imported graphics class (drawArc) to draw an arc.
I keep getting this error: streamtokenizer2.java:38: illegal start of expression public void paint (Graphics g)
I know I'm not setting this line up properly, but can't find detailed enough answers in Sun documentation or Black Book.
Tried moving the graphics chunk to different places and using different versions (drawArc vs. Arc2D.Double) Arc2D.Double class
works fine on its own. Any advice? Thanks!

[This message has been edited by Cindy Glass (edited September 28, 2001).]
16 years ago
I am getting this error message:
java StackApp
Exception in thread "main" java.lang.NoClassDefFoundError: StackApp
From previous messages on the forum, I figure I have to set
my classpath name to the directory my files are in.
Does anyone know how to do this in UNIX? Do I need to add
it to my .cshrc file? Please advise.
Thank you!
16 years ago