PDA

View Full Version : Trying to draw on Java JPanel


MrShed
04-27-2005, 03:39 PM
Hi guys,

I'm trying to draw using Graphics2D on a Java JPanel. Actually its a ImagePanel - a class ive extended from a JPanel, which just loads a jpeg into the panel. This part works fine....but then I want to draw on top of the JPanel, and its not showing what im drawing. Here is the code for the paint in the jpanel(if anyone needs me to post other code let me know):

protected void paintComponent(Graphics g)
{
System.out.println("in paintstuff");
super.paintComponent(g);
if(text)
{
System.out.println("Here");
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.drawString(textString, lastX, lastY);
text = false;
}


if (image == null) return;

switch (style)
{
case TILED:
drawTiled(g);
break;

case SCALED:
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
break;

case ACTUAL:
drawActual(g);
break;
}


}

Its definitely getting into the if(text) clause, and ive tried writing directly to the g Graphics, but that doesnt work either. "image" is just a BufferedImage storing the JPEG and whatever else I want to have drawn(in theory!).

Any help would be appreciated

suryad
04-27-2005, 07:55 PM
Have not worked much with the graphics2d library. I dont know if this would help any but have you looked at the demo/jfc directory in the jdk itself? There could be some useful code there.

I was trying to render a splashscreen for the heck of it in my program...and while I finally got the image to display...used the awt classes btw...MediaTracker and so on cause I found an example on the web...the thing is if I add another panel to the splashscreen frame everything else disappears. My splash image is a jpeg file and I really would like to know if I can create a frame. Then create a panel. Then load the image into the panel. Then create another panel and place it at the bottom of the panel with the image...and put some text there...you know a typical splash screen.

MrShed
04-27-2005, 08:24 PM
Same here...I havent used it much, which is why i think im having the trouble. Havent looked at the example code, will do, but doubt it will help as i think its because im using the bufferedimage in conjunction with the jpanel thats causing the issue, and doubt that will be there. Thanks for the help tho