• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having touble adding graphics to JPanel

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For more on drawing possibilities see Trail: 2D Graphics.
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Nick LiMuti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic