Draw nothing, feel badly.

data exists ideed. but nothing visible.

help needed, thanks

import java.awt.Color;

import java.awt.Graphics;

import java.awt.BasicStroke;

import java.awt.GradientPaint;

import java.awt.Rectangle;

import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

import java.awt.geom.RoundRectangle2D;

import java.awt.geom.Arc2D;

import java.awt.geom.Line2D;

import javax.swing.JPanel;

import java.io.*;

publicclass LinesRectsCirclesJPanelextends JPanel

{

// display various lines, rectangles and ovals

privateint x1, y1, x2, y2;// to plot lines from (x1,y1) to (x2,y2)

privateint xy[] =newint[206];

privateint x[] =newint[103];

privateint y[] =newint[103];

publicvoid paintComponent( Graphics g )

{

super.paintComponent( g );// call superclass's paint method

this.setBackground( Color.WHITE );

Graphics2D g2d = (Graphics2D)g;// cast g to Graphics 2D

ParseFile p =new ParseFile();

p.xyforStroke();

xy =p.getxy();

int nonzeroSize = xy[xy.length-1];

System.out.println("Test Plot Data");

for (int i = 0; i<nonzeroSize;i++)

{

System.out.printf("%d\n",xy[i]);

}

g2d.setColor(Color.RED);

g2d.setStroke(new BasicStroke(0.5f));

for (int a = 0; a><nonzeroSize;a++)

{

x[a] = xy[a];

y[a] = xy[nonzeroSize+a];

}

for (int i = 0; i><nonzeroSize-1;i++)

{

x1 = x[i];

y1 = y[i];

x2 = x[i+1];

y2 = y[i+1];

System.out.printf("x1=%d\n",x1);

System.out.printf("y1=%d\n",y1);

Line2D line =new Line2D.Double((double)x1,(double)y1,(double)x2,(double)y2);

g2d.draw(line);// nothing visible

}

}

// end method paintComponent

}// end class LinesRectsCirclesJPanel

>

[3979 byte] By [ardmorea] at [2007-11-27 8:53:35]
# 1

Erm, this isn't your only class is it?

Well, in case you still need help. You need to make a main class that extends JFrame. This would be the constructor for that class:

public MainClass() {

super("Main Class ~ Author");

Container cp = getContentPane();

cp.add(new LinesRectsCirclesJPanel());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(500, 500);

setVisible(true);

repaint();

}

Keep in mind you need to have LinesRectsCirclesJPanel as an inner class, unless you want to make it a variable and call "variable.repaint()"

Ahem, that would be something that you would need to have in order to see the graphics that you created in your LinesRectsCirclesJPanel class. Sorry if i'm just resurrecting this thread, and you really don't need help, or you DO have a main class. Cheers! ^.^

PaRlOaGna at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 2

it's not the only class,

the main class:

public class PlotMain

{

// execute application

private int x1, x2, y1, y2;

public static void main( String args[] )

{

// create frame for LinesRectsCirclesJPanel

JFrame frame =

new JFrame( "Stroke and Fill" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

LinesRectsCirclesJPanel linesRectsCirclesJPanel = new LinesRectsCirclesJPanel();

linesRectsCirclesJPanel.setBackground( Color.WHITE );

frame.add( linesRectsCirclesJPanel ); // add panel to frame

frame.setSize( 400, 210 ); // set frame size

frame.setVisible( true ); // display

} // end main

} // end class LinesRectsOvals

ardmorea at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 3
Did you call a repaint() anywhere?
PaRlOaGna at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 4

Here's a little template for displaying custom components.

import java.awt.*;

import javax.swing.*;

public class ComponentExample extends JComponent {

static void displayGUI() {

JComponent comp = new ComponentExample();

comp.setPreferredSize(new Dimension(600,400));

JFrame f = new JFrame("ComponentExample");

f.getContentPane().add(comp);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int w = getWidth();

int h = getHeight();

g.setColor(Color.WHITE);

g.fillRect(0, 0, w, h);

g.setColor(Color.GREEN);

g.drawLine(0, 0, w, h);

g.drawLine(0, h, w, 0);

}

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable(){

public void run() {

displayGUI();

}

});

}

}

BigDaddyLoveHandlesa at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 5
> ParseFile p = new ParseFile();Are you doing file I/O in your paintComponent method?
BigDaddyLoveHandlesa at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 6
> > ParseFile p = new ParseFile();> > Are you doing file I/O in your paintComponent method?no, I didn't. ParseFile doen't have paintComponrntmethod.
ardmorea at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 7
> > Are you doing file I/O in your paintComponent method?> > no, I didn't. > ParseFile doen't > have paintComponrntmethod.Does this line of code read from a file:> ParseFile p = new ParseFile();
BigDaddyLoveHandlesa at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 8
yes, it is.and I didn't call repaint().Where should I put it?
ardmorea at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 9
As noted above, you are putting stuff in the paintComponent method that don't belong there. All file handling and non-painting routines should be done in non paintcomponent methods with results being placed in class variables that paintComponent can check.
petes1234a at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 10

> yes, it is.

> and I didn't call repaint().

Aside: I never mentioned repaint, although I don't think you need to explicitly invoke it. I would try the code without it.

> Where should I put it?

My point here, by the way, is that paint methods like paintComponent get called

repeatedly, and more often than you think. You should only be coding the paint

operation in them, not other things like reading from files. Why read from the

file over and over again?

Suggestion: you can read the data in the component's constructor, or define

a separate method like readData, and don't forget to call it!

BigDaddyLoveHandlesa at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 11
You understand that you have little control of when paintComponent will be called. You can have it called yourself with a repaint method, but java can call it once, twice, 100 times as necessary if the component needs repainting.
petes1234a at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 12

Well, what exactly are you planning on putting inside this program? A Graphics intensive program that is usually changing? in which case you should probably just have a seperate inner-class extending Thread. Then inside the run method of this class. call a repaint then wait about 30 MILLIseconds. which gives you a good... 30 fps. But the wait method isn't always correct so it might vary a little bit. BUT If you're going to be painting seldomly and at the users command, like if your recalculating a formula of something like that. Just call repaint() basically whenever you want :P hope this helps

Message was edited by:

PaRlOaGn

PaRlOaGna at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 13
but I do have a read data file ParseFile although I haven't posted it.my purpose is not overwritting the drawing each time.I am going to plot shapes at diferent places on a frame with different reading data.
ardmorea at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 14
I mean to draw static compositions
ardmorea at 2007-7-12 21:11:18 > top of Java-index,Java Essentials,New To Java...
# 15

> but I do have a read data file ParseFile although I

> haven't posted it.

> my purpose is not overwritting the drawing each

> time.

> I am going to plot shapes at diferent places on a

> frame with different reading data.

I don't understand what you are writing.

1. Do you have to read from several data files or one data file?

2. For each file (or the one file), do you have to read from it repeatedly,

because its content is changing over time?

BigDaddyLoveHandlesa at 2007-7-21 22:49:10 > top of Java-index,Java Essentials,New To Java...
# 16
I read only from one file yes,its content is changing over time
ardmorea at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 17
Yes, but what is your program going to do? I understand you want to draw shapes but, what does the file read have to do with it?
PaRlOaGna at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 18
> I read only from one file > yes,its content is changing over timeIs your program required to detect these changes and automatically update the data it holds in memory? A simpler solution would be a "refresh" button.
BigDaddyLoveHandlesa at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 19

well, I still have a confusion,

I can seperate the data processing with the paint.

but I think it is just making code readable and clear.

I get data xy[] fron reading a file,

then extract x,y from xy,then x1,x2,y1,y2.

why not painting. Line2D line = new Line2D.Double((double)x1,(double)y1,(double)x2,(double)y2);

g2d.draw(line);

thanks

ardmorea at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 20

my data is like

section 1

20.000020.0000 moveto

592.000020.0000 lineto /

592.0000 772.0000 lineto /

20.0000 772.0000 lineto /

20.000020.0000 lineto /

section 2

22.000022.0000 moveto

590.000022.0000 lineto /

590.0000 770.0000 lineto /

22.0000 770.0000 lineto /

22.000022.0000 lineto /

...

section 11111

...

so each time I read one section data and return and paint.

ardmorea at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 21

> why not painting.

It's impossible to say, given what you have posted. I suggest you start with a simpler program that works, and then slowly add features. For example, start with some hard coded values and get that working before adding file I/O.

One way to achieve this, and that is also a better design, is to take the file I/O

out of your custom component. It's job is to render the data, not do file I/O.

Instead, it can have a method:

setData(... the data...)

(By the way, this method should probably call repaint, since

it changes the state of the component.)

First, call this method with some hard coded data, just one or two numbers,

the least amount of data to get up and running. When that is debugged, write

another test that passes in more data. When that works, write the code that

reads from a file and test that, then join those parts together. Step by step.

BigDaddyLoveHandlesa at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...
# 22
Thanks for your suggestions.I am doing that.
ardmorea at 2007-7-21 22:49:11 > top of Java-index,Java Essentials,New To Java...