• Post Reply Bookmark Topic Watch Topic
  • New Topic

graphics in JBuilder

 
steve gunning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does anyone know how to diplay graphical shapes (rectangle, ellipse, lines) in JBuilder?
thanks,
steve
 
Nura Horne
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean draw them directly into the UI Designer then you can't otherwise you do it exactly like normal java.
1. create a frame class
2. create a class called drawingPanel or whatever you want to call it that extends JPanel (I made it an inner class to the frame class in example but don't have to)
3. override the paint method in the drawing panel
4. insert the panel into your frame.
Here is some full code that you can paste in and run, I've used Graphics2D to provide drawing capabilities, but you could use the original Graphics class, see the Sun Java Tutorial for more info. http://java.sun.com/docs/books/tutorial/index.html
// CODE START
<CODE>
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class DrawingTestFrame extends JFrame
{
JPanel drawPanel = new myDrawingPanel();
public DrawingTestFrame()
{
setSize(400, 400);
getContentPane().add(drawPanel);
setVisible(true);
}
private class myDrawingPanel extends JPanel
{
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.red);
g2.fill(new Rectangle2D.Double(300,50,50,50));
g2.setColor(Color.yellow);
g2.fill(new Ellipse2D.Double(50,100,300,200));
}
}

public static void main(String[] args)
{
DrawingTestFrame drawingTestFrame = new DrawingTestFrame();
}
}
</CODE>
// CODE END


[This message has been edited by Arun Horne (edited July 27, 2001).]
 
steve gunning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Arun - i wsa hoping you could draw straight on but i guess i'll have to do it the long way. By the way, do you know how to display swing features such as JTextField and JButton alongside the shapes in the gui?
thanks,
steve.
 
Nura Horne
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create instances of any swing component u like and add it to the panel, and then draw some grpahics onto the panel as well. However the fact that you want to do this suggests that you may want two panels, one for drawing, one for components, but it depends on how yu want th egui of your app. Just ensure you call the paint method of the panel to make the components get painted before you do any graphics:
<code>
public void paint(Graphics g)
{
super.paint();
// now do drawing here
}
</code>
 
steve gunning
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks arun,
not sure how to go about what you're suggesting - i added the code you included but the compiler didnt like it.
what i'm trying to achieve is going to look something like the link below, should i use different panels? http://www.cs.aston.ac.uk/~gunninsp/galatean_model.gif
kind regards,
steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!