469,341 Members | 6,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,341 developers. It's quick & easy.

Plotting non overlapping adjacent rectangles

Hi,

I am trying to write a Java program to plot rectangles with different colors side by side non overlapping but unfortunately I am unable to do that as of now.
Suppose I want to create 3 rectangles:-
100 - 200
200 - 300
300 - 400

I want to have 3 rectangles non overlapping and adjacent to each other with 3 different colors. I am new to Java so don't know howto do that ?!
I would appreciate any help in this regard.

Here is my code:-
Expand|Select|Wrap|Line Numbers
  1. import java.awt.BasicStroke;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.awt.FontMetrics;
  6. import java.awt.GradientPaint;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.RenderingHints;
  10. import java.awt.event.WindowAdapter;
  11. import java.awt.event.WindowEvent;
  12. import java.awt.geom.Arc2D;
  13. import java.awt.geom.Ellipse2D;
  14. import java.awt.geom.GeneralPath;
  15. import java.awt.geom.Line2D;
  16. import java.awt.geom.Rectangle2D;
  17. import java.awt.geom.RoundRectangle2D;
  18.  
  19. import javax.swing.JApplet;
  20. import javax.swing.JFrame;
  21.  
  22. public class RectApplet extends JApplet {
  23.  
  24.   public void init() {
  25.     setBackground(Color.white);
  26.     setForeground(Color.white);
  27.   }
  28.  
  29.   public void paint(Graphics g) {
  30.     Graphics2D g2 = (Graphics2D) g;
  31.     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  32.         RenderingHints.VALUE_ANTIALIAS_ON);
  33.  
  34.     g2.setPaint(Color.gray);
  35.     int x = 40;
  36.     int y = 50;
  37.     g2.setPaint(Color.red);
  38.     g2.fill(new Rectangle2D.Double(x, y, 200, 50));
  39.     // Trying to create a second rectangle which should be adjacent to the first one but its overlapping unfortunately
  40.     g3.setPaint(Color.green);
  41.     g3.fill(new Rectangle2D.Double(x, y, 400, 50));
  42.  
  43.     g2.setPaint(Color.blue);
  44.     g2.drawString("My Rectangle Plotter", x, 150);
  45.  
  46.   }
  47.  
  48.   public static void main(String s[]) {
  49.     JFrame f = new JFrame("");
  50.     f.addWindowListener(new WindowAdapter() {
  51.       public void windowClosing(WindowEvent e) {
  52.         System.exit(0);
  53.       }
  54.     });
  55.     JApplet applet = new RectApplet();
  56.     f.getContentPane().add("Center", applet);
  57.     applet.init();
  58.     f.pack();
  59.     f.setSize(new Dimension(1000, 200));
  60.     f.show();
  61.   }
  62. }
  63.  
Thanks in advance
Cowboy
Apr 29 '09 #1
3 3818
JosAH
11,448 Expert 8TB
Set the color instead of the paint, i.e. g2.setColor( ... ) instead of g2.setPaint( ... ).

kind regards,

Jos
Apr 29 '09 #2
Thanks JosAH for ur help I really appreciate but it won't solve my problem :) lol.

Thx
cowboy
Apr 29 '09 #3
r035198x
13,262 8TB
After making the change suggested, take note of the following

1.) You need to understand the co-ordinate system. All your rectangles are being drawn with the same value of x and y. That means they will overlap since their top corners coincide and they are being drawn in the same orientation. Better change the x value (increase it say by 100) before drawing the second rectagle.
2.) Do you want to make an applet or a Java application. Decide which you want and don't mix the two.
3.) Do not use deprecated methods. Use setVisible(true) instead of show()
Apr 30 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Erik Lechak | last post: by
11 posts views Thread by Max M | last post: by
7 posts views Thread by Rolf Wester | last post: by
4 posts views Thread by Mark Tranchant | last post: by
4 posts views Thread by Holger Marzen | last post: by
4 posts views Thread by Charlie Brown | last post: by
5 posts views Thread by jo5867472 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.