Plotting non overlapping adjacent rectangles

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cowboyrocks2009
    New Member
    • Mar 2009
    • 17

    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:-
    Code:
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.geom.Arc2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.GeneralPath;
    import java.awt.geom.Line2D;
    import java.awt.geom.Rectangle2D;
    import java.awt.geom.RoundRectangle2D;
    
    import javax.swing.JApplet;
    import javax.swing.JFrame;
    
    public class RectApplet extends JApplet {
    
      public void init() {
        setBackground(Color.white);
        setForeground(Color.white);
      }
    
      public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    
        g2.setPaint(Color.gray);
        int x = 40;
        int y = 50;
        g2.setPaint(Color.red);
        g2.fill(new Rectangle2D.Double(x, y, 200, 50));
        // Trying to create a second rectangle which should be adjacent to the first one but its overlapping unfortunately
        g3.setPaint(Color.green);
        g3.fill(new Rectangle2D.Double(x, y, 400, 50));
    
        g2.setPaint(Color.blue);
        g2.drawString("My Rectangle Plotter", x, 150);
    
      }
    
      public static void main(String s[]) {
        JFrame f = new JFrame("");
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        JApplet applet = new RectApplet();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(1000, 200));
        f.show();
      }
    }
    Thanks in advance
    Cowboy
    Last edited by Nepomuk; Apr 29 '09, 01:18 PM. Reason: Please use [code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Set the color instead of the paint, i.e. g2.setColor( ... ) instead of g2.setPaint( ... ).

    kind regards,

    Jos

    Comment

    • cowboyrocks2009
      New Member
      • Mar 2009
      • 17

      #3
      Thanks JosAH for ur help I really appreciate but it won't solve my problem :) lol.

      Thx
      cowboy

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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()

        Comment

        Working...