473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4394
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

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

Similar topics

3
by: Erik Lechak | last post by:
Hello All, I am creating a visual programming environment for python (similar to Matlab's simulink, but for python). For several reasons I have decided not to go with OGL. I am writing a wxOGL...
11
by: Max M | last post by:
I am writing a "find-free-time" function for a calendar. There are a lot of time spans with start end times, some overlapping, some not. To find the free time spans, I first need to convert the...
7
by: Rolf Wester | last post by:
Hi, I have a Python console application that is intended to be used interactively and I have to add plotting capabilities (multiple XY plots and if possible 2D-surface plots). I'm loocking for a...
4
by: Mark Tranchant | last post by:
I'm looking for some advice here. Using a browser that supports fixed positioning (not IE), take a look at: http://tranchant.plus.com/guitar/chord-tutorial/ The page consists of a fixed...
4
by: Holger Marzen | last post by:
Say, we have uptimes from several servers: Server up_from up_to ------ ------- ------- s1 0:00 8:00 s1 10:00 20:00 s1 22:00 24:00 (would better be...
4
by: Charlie Brown | last post by:
I have a form with 2 custom controls that can be dragged around by a user. How can I check if they overlap each other without performing some kind of Collision detection on them? Is there...
7
by: ddecoste | last post by:
I have a need to add a visual representation to some data in Access. I need to draw a matix of squares inside another square. I have all the data that I need in a record in Access. The data...
5
by: jo5867472 | last post by:
hi to all I am just a beginner of python. I want to know how pixels are plotted in python. I am not intending to use PIL because I don't need to manipulate images. So is there a simple module for...
2
archonmagnus
by: archonmagnus | last post by:
Hello all, I've been experimenting with developing an orbital analysis program. Being a visually oriented person, I'd like to translate my (x, y) coordinate pairs to an pixel image array so I can...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.