Solved

How to set line width using Graphics.drawRect?

Posted on 2000-04-06
4
900 Views
Last Modified: 2008-02-01
im using Graphics.drawRect to draw some rects. how can i adjust the line width ?

Thanks,
  P
0
Comment
Question by:pipe
4 Comments
 

Expert Comment

by:Daman
Comment Utility
after
void drawRect(int top, int left, int width, int height)
call
fillRect(int top, int left, int width, int height

eg,
if you want to have rect(100*50) of width 5

call

c1.drawRect(10, 10, 100, 50)
c2.fillRect(10, 10, 100, 50)
c1.fillRect(15, 15, 90, 40)

(where c1 and c2 are "black" and "red" color)


This will work for you.
0
 
LVL 4

Expert Comment

by:jerch
Comment Utility
Hi pipe...
Do you mean the thickness of the line? I don't think you can set it.  You can do this instead.

        int thickNess = 4;
        for (int i = 0; i < thickNess; i++)
            g.drawRect(x + i, y + i, width - 2 * i, height - 2 * i);

regards..
Jerson
0
 
LVL 2

Accepted Solution

by:
stalefish earned 100 total points
Comment Utility
Actually, if you use Graphics2D, you can adjust the thickness

public void paint(Graphics g){
  BasicStroke stroke = new BasicStroke(5); //5 pixels wide
  Graphics2D g2 = (Graphics2D) g;
  g2.setStroke(stroke);
  g2.drawRect(w,x,y,z);
  ...
}
0
 

Expert Comment

by:aniket040700
Comment Utility
the possible option is using drawRect() for straight lines only.

public void paint(Graphics g)
{
   g.drawRect(int a, int b, widht w, height h);
}

a and b are the co-ordinates

w is the length of the line

h is the thickness
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now