aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Having touble adding graphics to JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Having touble adding graphics to JPanel" Watch "Having touble adding graphics to JPanel" New topic
Author

Having touble adding graphics to JPanel

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey guys im trying to add shapes to a jpanel, please help;
Eventually i want to add a JLabel exactly beside the Shape too.
So far i have this;

Everything works except when i try to add graphics? Can some one put me back on track please
Mark
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Guys even if you can give me the simplest example of just displaying a circle on a jtable it would be a great help as i cant find any easy tutorials that shows me how to do such a simple thing.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Right i managed to get a simple example going that will draw a simple shape to a jpanel, but in the main it calls Shape and draws everything in paint.
so i just get 3 shapes on top of each other. How would i be able to call in main say draw circle and have it draw only a circle.
Then maybe draw circle leave appropiate gap draw square on jpanel?
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Oh yea heres my code
[code[
package shape;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

public class Shape extends JFrame {

public void paint(Graphics g) {
super.paint(g);

g.drawRect(30, 30, 30, 30);

g.drawLine(0,0,50,50);

g.drawOval(20,20,20,20);
}
public static void main(String args[]) {
Shape f = new Shape();
f.setSize(200, 200);
f.show();

}
}
[/code]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
For more on drawing possibilities see Trail: 2D Graphics.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey,
Thanks for your help so far.
Ok ive gone a new direction.
I found an example and edited it, it displays a circle, square and line all one after another. Ill post code to shoe ye;


and also main to run the JPanel


So thats all good, but the thing is i cant seem to apply any BorderLayout to the shapes, i want to display them one after another on new lines.
eg Circle
Square
line
In that kind of order, opposed to left to right. Can someone tell me how this can be done thanks.
Mark
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
do you still need help?

or has the issue been resolved at sun's forum
http://forum.java.sun.com/thread.jspa?threadID=756647&tstart=0
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey,

Na im afraid im still stuck.
i created a class which creates shapes and stuff.
In my main class if i run the shapes are displayed, but if i add a label it does not appear when i try to use gridlayoutmanager, absolute positioning. it either display blank frame or just the shapes.
I tried layout manager but its not accurate enough im afraid.
I just need a way to declare a label in main and specify where to put it on a frame that contains the shapes i created in my class?
So i suppose is there a way to use the same frame created in my class in my main without creating a new one? Im not sure really what to do .
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
If you really want to use components and add them to a parent container which has graphics on it you can do it. Another option is to do all graphics. Here you would draw the label with the drawString method. You can get/create a Rectangle around the string and fill/draw it with color(s) for decoration. Here's a taste of each:
Nick LiMuti
Greenhorn

Joined: Nov 02, 2011
Posts: 2
I am having an issue that seems to be a bit similar to this, although I haven't read the above posts overly thoroughly. I can't get an image to display in a JFrame. If someone could take a look at my code and tell me what my mistakes are, that would be fantastic and i would be extremely grateful.


NOTE: This code does not give me any errors, it just fails to display the image in the JFrame. Also, I have used "10" as my int for most of it, so if this is an issue could someone please tell me how to fix it? As far as I have been able to decipher from the API, the ints here mainly control size and positioning.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Nick LiMuti wrote:I am having an issue that seems to be a bit similar to this, although I haven't read the above posts overly thoroughly.

Then I suggest you start by reading the above posts. The solution has definitely been discussed. In short, you should not use getGraphics().


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nick LiMuti
Greenhorn

Joined: Nov 02, 2011
Posts: 2
Rob Spoor wrote:
Nick LiMuti wrote:I am having an issue that seems to be a bit similar to this, although I haven't read the above posts overly thoroughly.

Then I suggest you start by reading the above posts. The solution has definitely been discussed. In short, you should not use getGraphics().


I said it was similar to this, not exactly the same. I do not want to draw a shape to the screen, I want to add an image to the JFrame. However, I have found another way to do this, although it would be great if someone could please tell me a different way, as this doesn't seem like the correct way to do it. Unless it is, in which case never mind. Also, if using an image is the same as using a Graphics object then I will definitely look further into it. My teacher said that it isn't though (although she may be incorrect).

Here's my new code:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

If you would have read this thread you would have found out that the proper solution was to create a JPanel subclass, override paintComponent, and paint the image inside that method. The image should be loaded outside of the paintComponent:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having touble adding graphics to JPanel