Paint method  
Author Message
jjcchicago





PostPosted: 2004-1-21 7:33:00 Top

java-programmer, Paint method I'm still new to Java, so this may be a pretty pathetic question.
Anyway, I have a paint method in an applet that paints something and
then when a certain button is pushed it needs to draw more (rectangles
and strings to be precise). However, the paint method is above, and
the scope of 'g' does not extend into the actionPerformed method. Can
'Graphics g' be declared outside the paint method, and if not is there
a way to make it work, while rewriting as little as possible?
 
Mark Haase





PostPosted: 2004-1-21 8:16:00 Top

java-programmer >> Paint method In article <email***@***.com>,
email***@***.com (windozer) wrote:

> I'm still new to Java, so this may be a pretty pathetic question.
> Anyway, I have a paint method in an applet that paints something and
> then when a certain button is pushed it needs to draw more (rectangles
> and strings to be precise). However, the paint method is above, and
> the scope of 'g' does not extend into the actionPerformed method. Can
> 'Graphics g' be declared outside the paint method, and if not is there
> a way to make it work, while rewriting as little as possible?

What you should do in actionPerformed() is set a flag so that the next
time the applet is redrawn it knows to draw some more stuff. You can
call repaint() to signal that you want the applet redrawn at the next
possible opportunity.

If you're looking to do as little work as possible, it is possible to
get a reference to the current graphics context. All Components have a
getGraphics() accessor..you just need to get a reference to an
appropriate Component.

--
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
 
Glodalec





PostPosted: 2004-4-1 21:17:00 Top

java-programmer >> Paint method Hi !

I am trying to graphically access contentpane within the frame, so I
wrote my own paint(Graphics g) method, which calls super.paint... and
then I do my stuff.

Now I have a question. Suppose I do some graphics which takes several
minutes to finish, or either it reads specified graphic file and puts
it to a Graphic object. When I move window, I don't want to reread file
(it can also be deleted in the same time).
I can somehow control my graphics using some boolean flag, but also have
several problems.
The question is, should I put my graphic in some image object, and
display it, if nothing in my graphic changes, so only image object is
drawn each time paint method is called, or should I write also Mouse &
Frame listeners to control all stuff.

Any hint would be appreciated.

 
 
Thomas Weidenfeller





PostPosted: 2004-4-1 21:28:00 Top

java-programmer >> Paint method Glodalec wrote:
> The question is, should I put my graphic in some image object, and
> display it

Yes.

/Thomas
 
 
JessyCute





PostPosted: 2006-5-4 18:45:00 Top

java-programmer >> Paint method I try to draw the line like this on the screen, but when some menu show
up my line is not draw correctly. Anyone know how to solve it thankyou.

Follow this code. I tried paint() and paintComponent() but it doesn't
work both.

Code:



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class TestPaint extends JFrame{

public TestPaint(){
JMenuBar bar = new JMenuBar();
JMenu testM = new JMenu("TEST");
testM.add(new JMenuItem("test1-child"));
testM.add(new JMenuItem("test1-child"));

JMenu test1M = new JMenu("TEST1");
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));

JMenu test2M = new JMenu("TEST2");
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));

JMenu test3M = new JMenu("TEST2");
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));

bar.add(testM);
bar.add(test1M);
bar.add(test2M);
bar.add(test3M);

setJMenuBar(bar);

getContentPane().add(new TestPanel(), BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
show();
}

class TestPanel extends JPanel{

/*public void paint(Graphics g) {
super.paint(g);
System.out.println("paint");
int x2 = ( int ) g.getClipBounds().getWidth();
int y = 100;
g.setColor( Color.RED );
g.drawLine( 0, y, x2, y );
}*/

protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paintComponent");
int x2 = ( int ) g.getClipBounds().getWidth();
int y = 100;
g.setColor( Color.RED );
g.drawLine( 0, y, x2, y );
}
}

public static void main(String[] args) { new TestPaint(); }
}

 
 
JessyCute





PostPosted: 2006-5-4 18:45:00 Top

java-programmer >> Paint method I try to draw the line like this on the screen, but when some menu show
up my line is not draw correctly. Anyone know how to solve it thankyou.

Follow this code. I tried paint() and paintComponent() but it doesn't
work both.

Code:



import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

public class TestPaint extends JFrame{

public TestPaint(){
JMenuBar bar = new JMenuBar();
JMenu testM = new JMenu("TEST");
testM.add(new JMenuItem("test1-child"));
testM.add(new JMenuItem("test1-child"));

JMenu test1M = new JMenu("TEST1");
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));
test1M.add(new JMenuItem("test1-child"));

JMenu test2M = new JMenu("TEST2");
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));
test2M.add(new JMenuItem("test1-child"));

JMenu test3M = new JMenu("TEST2");
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));
test3M.add(new JMenuItem("test1-child"));

bar.add(testM);
bar.add(test1M);
bar.add(test2M);
bar.add(test3M);

setJMenuBar(bar);

getContentPane().add(new TestPanel(), BorderLayout.CENTER);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
show();
}

class TestPanel extends JPanel{

/*public void paint(Graphics g) {
super.paint(g);
System.out.println("paint");
int x2 = ( int ) g.getClipBounds().getWidth();
int y = 100;
g.setColor( Color.RED );
g.drawLine( 0, y, x2, y );
}*/

protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("paintComponent");
int x2 = ( int ) g.getClipBounds().getWidth();
int y = 100;
g.setColor( Color.RED );
g.drawLine( 0, y, x2, y );
}
}

public static void main(String[] args) { new TestPaint(); }
}

 
 
Bart Cremers





PostPosted: 2006-5-4 19:00:00 Top

java-programmer >> Paint method When using the clipbounds to paint you'll have to make sure you take
the x position of the clip rectangle also into account:

int x1 = g.getClipBounds().x;
int x2 = x1 + g.getClipBounds().width;
int y = 100;
g.setColor(Color.RED);
g.drawLine(x1, y, x2, y);

Regards,

Bart

 
 
Thomas Fritsch





PostPosted: 2006-5-4 20:16:00 Top

java-programmer >> Paint method JessyCute wrote:
> I try to draw the line like this on the screen, but when some menu show
> up my line is not draw correctly. Anyone know how to solve it thankyou.
>
> Follow this code. I tried paint() and paintComponent() but it doesn't
> work both.
>

> class TestPanel extends JPanel{
>
> protected void paintComponent(Graphics g) {
> super.paintComponent(g);
> System.out.println("paintComponent");
> int x2 = ( int ) g.getClipBounds().getWidth();
Using
g.getClipBounds().getWidth()
in the line above is wrong. You should use
this.getWidth()
instead of that. Then the red line will be drawn correctly, also after
being obscured by one of menus.
> int y = 100;
> g.setColor( Color.RED );
> g.drawLine( 0, y, x2, y );
> }
> }

--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
 
 
Bart Rider





PostPosted: 2006-5-4 21:36:00 Top

java-programmer >> Paint method JessyCute wrote:
> I try to draw the line like this on the screen, but when some menu show
> up my line is not draw correctly. Anyone know how to solve it thankyou.
>
> Follow this code. I tried paint() and paintComponent() but it doesn't
> work both.
>
> Code:
>
>
>
> import java.awt.BorderLayout;
> import java.awt.Color;
> import java.awt.Graphics;
>
> import javax.swing.JFrame;
> import javax.swing.JMenu;
> import javax.swing.JMenuBar;
> import javax.swing.JMenuItem;
> import javax.swing.JPanel;
>
> public class TestPaint extends JFrame{
>
> public TestPaint(){
> JMenuBar bar = new JMenuBar();
> JMenu testM = new JMenu("TEST");
> testM.add(new JMenuItem("test1-child"));
> testM.add(new JMenuItem("test1-child"));
>
> JMenu test1M = new JMenu("TEST1");
> test1M.add(new JMenuItem("test1-child"));
> test1M.add(new JMenuItem("test1-child"));
> test1M.add(new JMenuItem("test1-child"));
> test1M.add(new JMenuItem("test1-child"));
> test1M.add(new JMenuItem("test1-child"));
>
> JMenu test2M = new JMenu("TEST2");
> test2M.add(new JMenuItem("test1-child"));
> test2M.add(new JMenuItem("test1-child"));
> test2M.add(new JMenuItem("test1-child"));
> test2M.add(new JMenuItem("test1-child"));
> test2M.add(new JMenuItem("test1-child"));
>
> JMenu test3M = new JMenu("TEST2");
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
> test3M.add(new JMenuItem("test1-child"));
>
> bar.add(testM);
> bar.add(test1M);
> bar.add(test2M);
> bar.add(test3M);
>
> setJMenuBar(bar);
>
> getContentPane().add(new TestPanel(), BorderLayout.CENTER);
>
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> setSize(400,400);
> show();
> }
>
> class TestPanel extends JPanel{
>
> /*public void paint(Graphics g) {
> super.paint(g);
> System.out.println("paint");
> int x2 = ( int ) g.getClipBounds().getWidth();
> int y = 100;
> g.setColor( Color.RED );
> g.drawLine( 0, y, x2, y );
> }*/
>
> protected void paintComponent(Graphics g) {
> super.paintComponent(g);
> System.out.println("paintComponent");
> int x2 = ( int ) g.getClipBounds().getWidth();
> int y = 100;
> g.setColor( Color.RED );
> g.drawLine( 0, y, x2, y );
> }
> }
>
> public static void main(String[] args) { new TestPaint(); }
> }
>

Use x2=getWidth(), so you draw from zero x coordinate to the
maximum (you may want to give credit to any border zone). If
you use the clip bounderies, the section to redraw might not
nessecarily start with x coordinate zero, but that was pointed
out by Bart Cremers before. :)

Just a hint to add:
dont use show any more, instead use setVisible(true) and before
the call to setVisible insert a pack() statement (together with
a new method in TestPanel called getMinimumSize)
 
 
RedGrittyBrick





PostPosted: 2006-5-5 4:30:00 Top

java-programmer >> Paint method JessyCute wrote:
> I try to draw the line like this on the screen, but when some menu show
> up my line is not draw correctly. Anyone know how to solve it thankyou.
>
> Follow this code. I tried paint() and paintComponent() but it doesn't
> work both.
>
<snip>

You *multiposted* this question to several Java newsgroups. That wastes
other people's time. See http://smjg.port5.com/faqs/usenet/xpost.html
 
 
JessyCute





PostPosted: 2006-5-5 10:34:00 Top

java-programmer >> Paint method Thanks. I am sorry.

 
 
JessyCute





PostPosted: 2006-5-5 10:43:00 Top

java-programmer >> Paint method Thanks. leouser.
But my problem is not relate to the line width or any it's position. If
you tried to press on the third or fouth menu, and move the mouse out
of the menu then click on the frame.

Let's notice. The red line will be cut.

Normal case: line show like this.

----------------------------------------------------

When I tried to do the menu drop down: the line will show like this.

---------- ------------ ---------

Some position was gone in this line.

 
 
JessyCute





PostPosted: 2006-5-5 11:04:00 Top

java-programmer >> Paint method Thank you. Now I know what is the problem.
The problem came from getClipboard() method.

Thank you very much.

 
 
Vova Reznik





PostPosted: 2006-5-5 21:44:00 Top

java-programmer >> Paint method JessyCute wrote:
> Thank you. Now I know what is the problem.
> The problem came from getClipboard() method.
>
> Thank you very much.
>

The problem didn't come form getClipBoard method
The problem came from unwillingness to read documentation.

int x2 = ( int ) g.getClipBounds().getWidth();
int y = 100;
g.setColor( Color.RED );
g.drawLine( 0, y, x2, y );

12345678901234567890123456789

|_______this_is_a_line_______|
|__a_clip__|

clipWidth = 10
clipX = 11

You want to draw line (repaint small portion)
from clipX to clipX + clipWidth
_is_a_line
but you command to draw line
from 0 to clipWidth

|_______thi

int x2 = g.getClipBounds().x;
g.drawLine( x2, y, x2 + g.getClipBounds().width, y );