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!
jacq carballo
Ranch Hand
Joined: Feb 10, 2002
Posts: 42
posted
0
try putting your graphics in a Canvas. Put the canvas in the JScrollPane. and then add the scroll pane into the contentpane.
Canvas is a heavyweight AWT component and JScrollPane is a lightweight Swing component... a big no no!
You'll have to either extend JPanel to paint your image on it, or just load your image in an ImageIcon and add it to a JLabel.
I'm not sure how you are creating your graphics output... if you were more specific perhaps I could help you more with this...
Are you using the Graphics context to draw onto a Component like JPanel? Are you generating Images?
-Nate
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
jacq carballo
Ranch Hand
Joined: Feb 10, 2002
Posts: 42
posted
0
when do you consider a component to be lightweight or heavyweight? (just want to know and learn) tia
Roy Ben Ami
Ranch Hand
Joined: Jan 13, 2002
Posts: 732
posted
0
light weight and heavy weight are just fancy names given to to components to distinguish between SWING componenets (which are reffred to lightweight) and AWT componenets (which are reffred to as heavy weight).
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Not to make this too complicated, why not simply place your graphic in a JLabel that is in a ScrollPane?
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.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. } }
Pip Vo
Greenhorn
Joined: Jul 18, 2001
Posts: 12
posted
0
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); } }
To add your applet to a JScrollPane in a JFrame you would do something like this...
Of course, for everything to work out all right, you'll need to implement a getPreferredSize() method on your applet to return the size (as a Dimension object) that it should be... since you are already doing some calculations to do the drawing this shouldn't be too hard...
-Nate
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.