a Problem with java 2D

Y

York

Hi everybody,
i'm new to Java 2D and I have a Problem drawing a geometric primitive
on a JPanel.
I used getGraphics() to get the Graphics-Object of my JPanel, casted
it to Graphics2D, then drew a line on it und used myJPanel.repaint()
to update the JPanel.

But somehow I can't see the line I'v drawn and i can't figure it out
why .

the following is my code (without import statements):
.......................................................................................................
public class NewJFrame extends JFrame {

private JPanel pane = null;

/** Creates new instance */
public NewJFrame() {
initComponents();

// draw a line on JPanel
Graphics2D canvas = (Graphics2D)pane.getGraphics();
canvas.setPaint(Color.BLUE);
canvas.draw(new Line2D.Float(1,1,111,111));
pane.repaint();
}

private void initComponents() {
Dimension min = new Dimension(300,300);
setMinimumSize(min);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pane = new JPanel();
setContentPane(pane);
pane.setBackground(new Color(255, 204, 255));
pane.setForeground(new Color(51, 51, 255));
pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
.......................................................................................................

Any help would be greatly appreicated, because I am deeply confused
and frustrated.
 
D

Daniel Pitts

York said:
Hi everybody,
i'm new to Java 2D and I have a Problem drawing a geometric primitive
on a JPanel.
I used getGraphics() to get the Graphics-Object of my JPanel, casted
it to Graphics2D, then drew a line on it und used myJPanel.repaint()
to update the JPanel.

But somehow I can't see the line I'v drawn and i can't figure it out
why .

the following is my code (without import statements):
.......................................................................................................
public class NewJFrame extends JFrame {

private JPanel pane = null;

/** Creates new instance */
public NewJFrame() {
initComponents();

// draw a line on JPanel
Graphics2D canvas = (Graphics2D)pane.getGraphics();
canvas.setPaint(Color.BLUE);
canvas.draw(new Line2D.Float(1,1,111,111));
pane.repaint();
}

private void initComponents() {
Dimension min = new Dimension(300,300);
setMinimumSize(min);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pane = new JPanel();
setContentPane(pane);
pane.setBackground(new Color(255, 204, 255));
pane.setForeground(new Color(51, 51, 255));
pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
.......................................................................................................

Any help would be greatly appreicated, because I am deeply confused
and frustrated.
Repaint tells the panel to call the paint method, which will generally
erase everything you've painted on useing getGraphics().

The appropriate way to draw graphics is to create your own JComponent
subclass which override void paintComponent(Graphics g), and in that
method use the graphics object thats passed in.

Generally, you can cast Graphics2d g2d = (Graphics2d)g; and use the
newer Java 2d API.
 
K

Knute Johnson

Daniel said:
York said:
Hi everybody,
i'm new to Java 2D and I have a Problem drawing a geometric primitive
on a JPanel.
I used getGraphics() to get the Graphics-Object of my JPanel, casted
it to Graphics2D, then drew a line on it und used myJPanel.repaint()
to update the JPanel.

But somehow I can't see the line I'v drawn and i can't figure it out
why .

the following is my code (without import statements):
.......................................................................................................

public class NewJFrame extends JFrame {

private JPanel pane = null;

/** Creates new instance */
public NewJFrame() {
initComponents();

// draw a line on JPanel
Graphics2D canvas = (Graphics2D)pane.getGraphics();
canvas.setPaint(Color.BLUE);
canvas.draw(new Line2D.Float(1,1,111,111));
pane.repaint();
}

private void initComponents() {
Dimension min = new Dimension(300,300);
setMinimumSize(min);

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
pane = new JPanel();
setContentPane(pane);
pane.setBackground(new Color(255, 204, 255));
pane.setForeground(new Color(51, 51, 255));
pack();
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
.......................................................................................................


Any help would be greatly appreicated, because I am deeply confused
and frustrated.
Repaint tells the panel to call the paint method, which will generally
erase everything you've painted on useing getGraphics().

The appropriate way to draw graphics is to create your own JComponent
subclass which override void paintComponent(Graphics g), and in that
method use the graphics object thats passed in.

Generally, you can cast Graphics2d g2d = (Graphics2d)g; and use the
newer Java 2d API.

And here is a simple example;

http://knutejohnson.com/imageio.html
 
A

Andrew Thompson

Daniel Pitts wrote:
(OP)
..hmm. That code was frustratingly close to being an SSCCE*.
The appropriate way to draw graphics is to create your own JComponent
subclass which override void paintComponent(Graphics g), and in that
method use the graphics object thats passed in.

..or to put that an SSCCE way.

<sscce>
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class NewJFrame extends JFrame {

private JPanel pane = null;

/** Creates new instance */
public NewJFrame() {
initComponents();
}

private void initComponents() {
Dimension min = new Dimension(300,300);
setMinimumSize(min);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
pane = new JPanel(){
public void paintComponent(Graphics g) {
super.paintComponent(g);
// draw a line on JPanel
Graphics2D canvas = (Graphics2D)g;
canvas.setPaint(Color.BLUE);
canvas.draw(new
Line2D.Float(
1,1,
getWidth()/2,
getHeight()/2));
}
};
setContentPane(pane);
pane.setBackground(new Color(255, 204, 255));
pane.setForeground(new Color(51, 51, 255));
pack();
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}
</sscce>

* <http://www.physci.org/codes/sscce.html>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 
C

Chris ( Val )

Chris ( Val ) wrote:
(OP)
the following is my code (without import statements):
.hmm. That code was frustratingly close to being an SSCCE*.
[snip]
Did you test your SSCCE?

Ok, next question :)

Is the code you pasted here, actually the code you tested?

The reason I ask is because your SSCCE does not compile for me.

Oop's, I meant there are 2 warnings, sorry :)
 
A

Andrew Thompson

Chris said:
[quoted text clipped - 7 lines]

Ok, next question :)

Is the code you pasted here, actually the code you tested?
Yep.

The reason I ask is because your SSCCE does not compile for me.

(sigh) I was hoping you could take my first answer as a hint.

I'll try another approach.

The code I posted was successfully locally compiled and
run. It shows a slightly different thing to what the OP
wants, since the line will change if the JFrame is resized,
but that is less boring than a static line, and I wanted to
demonstrate that it would be redrawn when resized.

I just copied the code I posted, back into my Java
environment and compiled it 'fresh', to check there
was no line-wrap between when I first wrote it in
my editor, to when it arrived back there (via. usenet).

It compiled cleanly again.

So, we might go 'round & round'* with you making some
(semi)cryptic comment and me replying with 'one word'
answers to try and draw more detail out, or you might help
the thread along by stating *exactly what it is about
compiling the code that causes a problem for you.*

If, for example, you are getting ...

D:\NewJFrame.java:24: cannot find symbol
symbol : variable BLUE
location: class java.awt.Color
canvas.setPaint(Color.BLUE);

..then upgrade from 1.3, for pities sake!

For anything else, please *state the problem* specifically.

* But no, not for long - I have a short attention span.
 
C

Chris ( Val )

Oop's [sic], I meant there are 2 warnings, sorry :)

and they are ...?

My apologies for not posting them.

Compiled using version: "1.6.0_02"

Here are the two warning I received:

C:\Java\classes\NewJFrame.java:19: warning: [serial] serializable
class <anonymous NewJFrame$1> has no definition of serialVersionUID
pane = new JPanel(){
^
C:\Java\classes\NewJFrame.java:5: warning: [serial] serializable class
NewJFrame has no definition of serialVersionUID
public class NewJFrame extends JFrame {
^
2 warnings

Also note the placement of the parenthesis, in the following piece of
code:

EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});

Do you see a problem here?
 
C

Chris ( Val )

[quoted text clipped - 7 lines]
Ok, next question :)
Is the code you pasted here, actually the code you tested?
Yep.

The reason I ask is because your SSCCE does not compile for me.

(sigh) I was hoping you could take my first answer as a hint.

No need to "sigh".

Surely you read my latter reply, right?

[snip]
I just copied the code I posted, back into my Java
environment and compiled it 'fresh', to check there
was no line-wrap between when I first wrote it in
my editor, to when it arrived back there (via. usenet).

It compiled cleanly again.

What version of compiler did you use?
I used: "1.6.0_02"

Here are the two warnings that were produced:

C:\Java\classes\NewJFrame.java:19: warning: [serial] serializable
class <anonymous NewJFrame$1> has no definition of serialVersionUID
pane = new JPanel(){
^
C:\Java\classes\NewJFrame.java:5: warning: [serial] serializable class
NewJFrame has no definition of serialVersionUID
public class NewJFrame extends JFrame {
^
2 warnings
So, we might go 'round & round'* with you making some
(semi)cryptic comment and me replying with 'one word'
answers to try and draw more detail out, or you might help
the thread along by stating *exactly what it is about
compiling the code that causes a problem for you.*

Sure, but note that "communication" works both ways, and
a solitary 'yep' does nothing to help the thread along.
 
L

Lew

Chris said:
Chris said:
The reason I ask is because your SSCCE does not compile for me.
Oop's [sic], I meant there are 2 warnings, sorry :)
and they are ...?

My apologies for not posting them.

Compiled using version: "1.6.0_02"

Here are the two warning I received:

C:\Java\classes\NewJFrame.java:19: warning: [serial] serializable
class <anonymous NewJFrame$1> has no definition of serialVersionUID
pane = new JPanel(){
^
C:\Java\classes\NewJFrame.java:5: warning: [serial] serializable class
NewJFrame has no definition of serialVersionUID
public class NewJFrame extends JFrame {
^
2 warnings

It's a warning instead of an error because Serializable classes don't need an
explicit serialVersionUID; in its absence Java generates one for you. It's a
warning at all because leaving it at the default can cause you maintenance pain.

Have you read the Javadocs for java.io.Serializable? They discuss this member
variable there. Also, Joshua Bloch's seminal /Effective Java/ covers
Serialization in detail.

To implement Serializable is a solemn responsibility that a lot of people hack
through, then wonder why they have trouble later. It exposes details of
implementation, violating OO encapsulation, and locks in aspects of the class
design. Failing to account for that causes trouble. It can be horridly
inefficient, unless you take care. You can have trouble with modifications to
a class if it is enhanced (refactoring, additional features, ...). It's
actually a fair amount of work to make something Serializable correctly.
Also note the placement of the parenthesis, in the following piece of
code:

EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});

Do you see a problem here?

No. What is your point?
 
C

Chris ( Val )

Chris ( Val ) wrote:




Chris ( Val ) wrote:
The reason I ask is because your SSCCE does not compile for me.
Oop's [sic], I meant there are 2 warnings, sorry :)
and they are ...?
My apologies for not posting them.
Compiled using version: "1.6.0_02"
Here are the two warning I received:
C:\Java\classes\NewJFrame.java:19: warning: [serial] serializable
class <anonymous NewJFrame$1> has no definition of serialVersionUID
pane = new JPanel(){
^
C:\Java\classes\NewJFrame.java:5: warning: [serial] serializable class
NewJFrame has no definition of serialVersionUID
public class NewJFrame extends JFrame {
^
2 warnings

It's a warning instead of an error

Yes, that's what I said (corrected), in my follow up.
because Serializable classes don't need an
explicit serialVersionUID; in its absence Java generates one for you. It's a
warning at all because leaving it at the default can cause you maintenance pain.

I see, thank you.
Have you read the Javadocs for java.io.Serializable? They discuss this member
variable there. Also, Joshua Bloch's seminal /Effective Java/ covers
Serialization in detail.

No, I have not looked closely into the Serializable interface yet,
but I will in good time.
To implement Serializable is a solemn responsibility that a lot of people hack
through, then wonder why they have trouble later. It exposes details of
implementation, violating OO encapsulation, and locks in aspects of the class
design. Failing to account for that causes trouble. It can be horridly
inefficient, unless you take care. You can have trouble with modifications to
a class if it is enhanced (refactoring, additional features, ...). It's
actually a fair amount of work to make something Serializable correctly.

Thanks again, I will check it out in good time.
No. What is your point?

Well, my point is that it looks missplaced?
 
C

Chris ( Val )

Chris said:
I used [Java version] "1.6.0_02"

There is a security flaw in that version that was fixed in Java 6 update 3.

Thank you for the information.

I have the latest version on my other computer,
and I am installing it on this one as we speak.
 
C

Chris ( Val )

Chris ( Val ) wrote:
[snip]
Also note the placement of the parenthesis, in the following piece of
code:
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewJFrame().setVisible(true);
}
});
Do you see a problem here?

No. What is your point?

Perhaps the following is a little more obvious?

EventQueue.invokeLater( new Runnable()
{
public void run(){new NewJFrame().setVisible(true);}
}

);
^^^--------?
 
L

Lew

Chris said:
Perhaps the following is a little more obvious?

EventQueue.invokeLater( new Runnable()
{
public void run(){new NewJFrame().setVisible(true);}
}

);
^^^--------?

Matches the opening paren of the invokeLater method.
 
C

Chris ( Val )

Matches the opening paren of the invokeLater method.

Yes, I realise that.

What I find odd, is that there is a new scope
{ /* more stuff */ }, prior to the closing parenthesis.

IOW, is something like:

void Foo( new Bar() { System.out.println( "In the body of new
Bar" ); } );

....legal in Java?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top