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.
}
}