Draw Circle Jframe Specific Location
- Details
- Written by
- Terminal Updated on 10 Baronial 2019 | Print Email
In Coffee, to draw a rectangle (outlines) onto the current graphics context, we tin use the post-obit methods provided by the Graphics/Graphics2D class:
- drawRect(int x, int y, int width, int height)
- draw3DRect(int x, int y, int width, int height, boolean raised)
- draw(Rectangle2D)
- drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
With:
- (x, y) is the upper left corner of the rectangle.
- width and height: specify dimension of the rectangle.
- raised: specifies whether the rectangle edges are raised or sunk when cartoon a 3D rectangle.
- Rectangle2D is the base grade of Rectangle (for integer coordinates) Rectangle2D.Double (for double coordinates) and Rectangle2D.Float (for float coordinates).
- arcWidth and arcHeight: specify the horizontal and vertical diameters of the arcs at the four corners when drawing a rectangle with rounded corners.
To demonstrate the examples, nosotros create the following Swing program:
/** * This program demonstrates how to rectangles using Graphics2D object. * @author www.codejava.internet * */ public course RectanglesDrawingExample extends JFrame { public RectanglesDrawingExample() { super("Rectangles Drawing Demo"); getContentPane().setBackground(Color.WHITE); setSize(480, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } void drawRectangles(Graphics g) { Graphics2D g2d = (Graphics2D) g; // code to depict rectangles goes here... } public void paint(Graphics m) { super.paint(g); drawRectangles(m); } public static void primary(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new RectanglesDrawingExample().setVisible(true); } }); } } The code examples volition go into the drawRectangles() method which obtains graphics context of the JFrame window:
void drawRectangles(Graphics yard) { Graphics2D g2d = (Graphics2D) g; // lawmaking to depict rectangles goes here... } Now, let'due south see the examples in details.
1. Cartoon Rectangles in integer coordinates
Using the drawRect() method:
g2d.drawRect(30, 50, 420, 120);
Using the draw(Rectangle) method:
g2d.describe(new Rectangle(30, 50, 420, 120));
Result:
two. Cartoon Rectangles in double coordinates
To describe a rectangle in double coordinates, create a new instance of the Rectangle2D.Double class. For example:
double ten = 29.5d; double y = 48.8d; double width = 413.2d; double top = 118.6d; Rectangle2D.Double rect = new Rectangle2D.Double(ten, y, width, height); g2d.draw(rect);
Or like this for short:
g2d.draw(new Rectangle2D.Double(29.5d, 48.8d, 413.2d, 118.6d));
3. Drawing Rectangles in bladder coordinates
Similar to double coordinates, nosotros use the Rectangle2D.Bladder class to draw rectangles in float coordinates. For example:
float ten = 29.5f; float y = 48.8f; float width = 413.2f; float height = 118.6f; Rectangle2D.Bladder rect = new Rectangle2D.Float(x, y, width, meridian); g2d.draw(rect);
Or similar this for short:
g2d.draw(new Rectangle2D.Float(29.5f, 48.8f, 413.2f, 118.6f));
four. Drawing Rectangles with Rounded-Corners
To draw a rectangle with iv rounded corners, use the drawRoundRect() method and laissez passer values for the horizontal diameter (arcWidth) and vertical diameter (arcHeight) of the corners. Here'south an case:
int ten = 30; int y = 50; int width = 420; int height = 120; int arcWidth = 20; int arcHeight = twenty; g2d.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
Upshot:
The following example plays around with diverse values for the arcWidth and arcHeight:
g2d.drawRoundRect(x, y, width, meridian, arcWidth, arcHeight); g2d.drawRoundRect(x + 10, y + ten, width - 20, pinnacle - twenty, arcWidth + 10, arcHeight + 10); g2d.drawRoundRect(ten + 20, y + 20, width - 40, tiptop - forty, arcWidth + 20, arcHeight + ten); g2d.drawRoundRect(x + 30, y + 30, width - 60, height - threescore, arcWidth + 40, arcHeight + 20);
Result:
five. Cartoon 3D Rectangles
The draw3DRect() method draws a rectangle with edges announced to be raised or sunk, specified past the raised boolean flag. Here'southward an example:
int x = thirty; int y = 50; int width = 420; int meridian = 120; boolean raised = true; g2d.setColor(Colour.LIGHT_GRAY); g2d.draw3DRect(x, y, width, height, raised);
Issue (a really simple 3D effect):
The following example draws two rectangles - 1 with raised edges and one with sunk edges:
g2d.setColor(Color.LIGHT_GRAY); g2d.draw3DRect(30, 50, 200, 120, true); g2d.draw3DRect(250, 50, 200, 120, false);
Result:
Notation: The draw3DRect() method uses only the current color and ignores the current paint, such as line strokes.
6. Drawing Rectangles with Custom Strokes
Rather than using the default style (sparse and solid line, black color), we can brand some attractive decorations for the outlines of the rectangles using Strokes. For case:
Stroke stroke1 = new BasicStroke(6f); g2d.setColor(Color.BLACK); g2d.setStroke(stroke1); g2d.drawRect(30, 50, 420, 120);
That draws a rectangle with outlines in blue color and thickness of 6 pixels width. Here'southward the result:
To empathize more near using strokes, meet the tutorial: Drawing lines examples with Graphics2D. The following code snippet plays effectually with various strokes:
int x = 20; int y = forty; int width = 440; int height = 140; Stroke stroke1 = new BasicStroke(6f); g2d.setColor(Color.Blue); g2d.setStroke(stroke1); g2d.drawRect(10, y, width, tiptop); float[] dashingPattern1 = {2f, 2f}; Stroke stroke2 = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, i.0f, dashingPattern1, 2.0f); g2d.setColor(Color.Cerise); g2d.setStroke(stroke2); g2d.drawRect(x + twenty, y + twenty, width - 40, height - 40); bladder[] dashingPattern2 = {10f, 4f}; Stroke stroke3 = new BasicStroke(4f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dashingPattern2, 0.0f); g2d.setColor(Colour.GREEN); g2d.setStroke(stroke3); g2d.drawRect(x + 40, y + 40, width - fourscore, height - 80); float[] dashingPattern3 = {10f, 10f, 1f, 10f}; Stroke stroke4 = new BasicStroke(4f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, dashingPattern3, 0.0f); g2d.setColor(Color.Blueish); g2d.setStroke(stroke4); g2d.drawRect(x + 60, y + sixty, width - 120, height - 120); Result:
API References:
- drawRect() method Javadoc
- draw3DRect() method Javadoc
- depict(Shape) method Javadoc
- drawRoundRect() method Javadoc
- BasicStroke class Javadoc
- Rectangle2D grade Javadoc
- Rectangle class Javadoc
- Rectangle2D.Double class Javadoc
- Rectangle2D.Bladder course Javadoc
Other Coffee Graphics Tutorials:
- How to add watermark for images using Java
- How to resize images using Java
- How to convert image format using Coffee
- How to draw prototype with automatic scaling in Java
- How to capture screenshot programmatically in Java
- How to describe text vertically with Java Graphics2D
- How to Create Zoomable User Interface Coffee Programs with Piccolo2D Framework
- Drawing lines examples with Java Graphics2D
- Using JFreechart to draw line chart with CategoryDataset
- Using JFreechart to draw XY line nautical chart with XYDataset
Virtually the Writer:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java i.4 and has been falling in love with Java since then. Brand friend with him on Facebook and picket his Java videos you YouTube.
Add annotate
Source: https://www.codejava.net/java-se/graphics/drawing-rectangles-examples-with-graphics2d
0 Response to "Draw Circle Jframe Specific Location"
Publicar un comentario