2D Graphics in Java
2D Graphics can be achieved using Java programming with the help of a few advanced features of the Java 2 platform, which includes Java’s built-in functions for operations like image processing, advanced graphic designing options, geometric transformation, alpha compositing, etc. There are a number of packages provided under the java 2D package, such as awt, awt.image, awt.color, awt.font, awt.geom, awt.print and awt.image.renderable. Java is a goto option famous amongst the gaming developers community due to its high-quality graphical outcome, huge variety of geometrical designing options facilitates printing of the documents, and allows to maintain the performance of the developed product.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsJava 2D Rendering
Java 2D API supports a uniform rendering model across all the different devices: a display monitor or a printer. During the development of the program, rendering works, in the same way, irrespective of the end component, whether it is a printer or a display monitor. The package automatically detects and changes the Graphics context based on the end component. The Java 2D API consists of java.awt.Graphics2D which extends the Graphics class to provide support for enhanced graphics and rendering features.
Below are the features which the package provides:
- It supports the rendering of primitive geometric shapes and figures.
- It provides the option to fill the interior of any shape with any color or pattern specified in paint attributes using strokes.
- It lets you render the specified image.
- It lets you convert the text Strings into glyphs, filled with the colors specified in the paint attributes.
Example #1
Let’s have a look at the same Java program and see how it works.
Code:
import javax.swing.JFrame; import java.awt.*; // AWT package is responsible for creating GUI import javax.swing.*; // Java swing package is responsible to provide UI components // AWT class extents Jframe which is part of Swing package public class AWTGraphicsSampleProgram extends JFrame { /** * */ // Defining all the static variables private static final long serialVersionUID = 1L; public static final int SAMPLE_CANVAS_WIDTH = 500; public static final int SAMPLE_CANVAS_HEIGHT = 500; // The program enters from the main method public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new AWTGraphicsSampleProgram(); // this run method will create a new object and thus invoke the constructor method. } }); } //Here we are creating an instance of the drawing canvas inner class called DrawCanwas private DrawCanvas sampleCanvas; public AWTGraphicsSampleProgram() { sampleCanvas = new DrawCanvas(); sampleCanvas.setPreferredSize(new Dimension(SAMPLE_CANVAS_WIDTH, SAMPLE_CANVAS_HEIGHT)); Container containerPane = getContentPane(); containerPane.add(sampleCanvas); setDefaultCloseOperation(EXIT_ON_CLOSE); // setting up the default close mechanism pack(); setTitle("......"); // set the desired title of the JFrame setVisible(true); // setVisible method will be set the visibility of the Jframe to true } /** * here drawCanvas is the inner class of the Jpanel which is used for custom drawing */ private class DrawCanvas extends JPanel { /** * */ private static final long serialVersionUID = 1L; // Overriding paintComponent will let you to design your own painting @Override public void paintComponent(Graphics graphics) { super.paintComponent(graphics); setBackground(Color.BLACK); // setting the background color to black graphics.setColor(Color.GREEN); // setting up the color to green graphics.drawLine(30, 40, 100, 200); graphics.drawOval(150, 180, 10, 10); graphics.drawRect(200, 210, 20, 30); graphics.setColor(Color.magenta); graphics.fillOval(300, 310, 30, 50); graphics.fillRect(400, 350, 60, 50); graphics.setColor(Color.WHITE); graphics.setFont(new Font("Monospaced", Font.PLAIN, 12)); // setting up the font style and font size graphics.drawString("Java Graphics in 2D ...", 10, 20); } } }
Output:
Graphics class provides different methods for drawing different graphical objects. The most common methods are drawString(), drawImage() and fillXxx(). These methods can be broadly divided into two categories. The first type of Graphics method is, it provides draw and fills features that enable the Users to render the basic shapes, text, and images. The other type of method is for attribute settings which lets you change the effect of how the drawing appears in the console. Methods such as setColor and setFont lets you decide how the draw and fill renders. The graphics context is responsible for maintaining the state or attributes such as the current font’s current painting colour.
Example #2
Let’s see another example of what else we can achieve with Java 2D classes.
Code:
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class GeometricShapes extends JFrame { /** * */ private static final long serialVersionUID = 1L; @SuppressWarnings("deprecation") public GeometricShapes() { super( "Geometric shapes" ); setSize( 425, 160 ); show(); } public static void main( String args[] ) { GeometricShapes figure = new GeometricShapes(); figure.addWindowListener( new WindowAdapter() { public void windowclosing( WindowEvent e ) { System.exit( 0 ); } }); } public void paint( Graphics graphics ) { // Instantiating Graphics 2D class Graphics2D graphics2D = ( Graphics2D ) graphics; graphics2D.setPaint( new GradientPaint( 16, 30, Color.red, 45, 105, Color.green, true ) ); graphics2D.fill( new Ellipse2D.Double( 6, 31, 61, 105 ) ); graphics2D.setPaint( Color.black ); graphics2D.setStroke(new BasicStroke( 9.0f ) ); graphics2D.draw( new Rectangle2D.Double( 82, 32, 67, 102 ) ); // This will create a black colored rounded rectangle BufferedImage bufferedImage = new BufferedImage( 10, 10, BufferedImage.TYPE_INT_RGB ); Graphics2D design = bufferedImage.createGraphics(); design.setColor( Color.blue ); design.fillRect( 0, 0, 9, 9 ); design.setColor( Color.orange ); design.drawRect( 2, 2, 7, 7 ); design.setColor( Color.black ); design.fillRect( 2, 2, 4, 4 ); design.setColor( Color.pink ); design.fillRect( 5, 5, 2, 2 ); graphics2D.setPaint( new TexturePaint( bufferedImage, new Rectangle( 9, 9 ) ) ); graphics2D.fill( new RoundRectangle2D.Double( 156, 31, 76, 101, 51, 51 ) ); graphics2D.setPaint( Color.CYAN ); graphics2D.setStroke(new BasicStroke( 7.0f ) ); graphics2D.draw( new Arc2D.Double( 240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); // this will create line in red and black color graphics2D.setPaint( Color.red ); graphics2D.draw( new Line2D.Double( 400, 40, 350, 180 ) ); float dashesArray[] = { 20 }; graphics2D.setPaint( Color.black ); graphics2D.setStroke( new BasicStroke( 4, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, dashesArray, 0 ) ); graphics2D.draw( new Line2D.Double( 320, 30, 395, 150 ) ); } }
Output:
Example #3
Let’s apply 2D java in the following program.
Code:
import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; public class GeometricShapes2 extends JFrame { /** * */ private static final long <em>serialVersionUID</em> = 1L; public static void main( String args[] ) { GeometricShapes2 design = new GeometricShapes2(); design.addWindowListener(new WindowAdapter() { }); } @SuppressWarnings("deprecation") public GeometricShapes2() { super( "A circle made up of stars by joining them at certain position filled with random colors" ); setBackground( Color.<em>green</em> ); setSize( 450, 450 ); show(); } public void paint( Graphics graphics ) { int xCoordinates[] = { 57, 69, 111, 75, 85, 57, 29, 39, 3, 45 }; int yCoordinates[] = { 2, 38, 38, 56, 98, 74, 98, 56, 38, 38 }; Graphics2D graphics2D = ( Graphics2D ) graphics; GeneralPath starFigure = new GeneralPath(); starFigure.moveTo( xCoordinates[ 0 ], yCoordinates[ 0 ] ); for ( int j = 1; j < xCoordinates.length; j++ ) starFigure.lineTo( xCoordinates[ j ], yCoordinates[ j ] ); starFigure.closePath(); graphics2D.translate( 200, 200 ); for ( int i = 1; i <= 10; i++ ) { graphics2D.rotate( Math.<em>PI</em> / 9.0 ); graphics2D.setColor(new Color( ( int ) ( Math.<em>random</em>() * 128 ),( int ) ( Math.<em>random</em>() * 128 ), ( int ) ( Math.<em>random</em>() * 128 ) ) ); graphics2D.fill( starFigure ); } } }
Output:
Example #4
Applying color coding in the following program.
Code:
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class GeometricShapes3 extends Canvas { /** * */ private static final long serialVersionUID = 1L; Frame windowFrame; TextField sampleText; Font sampleFont; Color colorOfText; Color colorOfCircle; public static void main(String args[]) { GeometricShapes3 start; start = new GeometricShapes3(); } public GeometricShapes3() { this("Arial", Font.BOLD, 18, Color.gray, Color.red); } public GeometricShapes3(String ff, int fs, int fz, Color bg, Color fg) { setBackground(bg); colorOfCircle = Color.green.brighter(); colorOfText = fg; sampleFont = new Font(ff, fs, fz); sampleText = new TextField("eduCBA (Corporate Bridge Consultancy Pvt Ltd) "); windowFrame = new Frame("Demo"); windowFrame.add(sampleText, BorderLayout.NORTH); windowFrame.add(this, BorderLayout.CENTER); windowFrame.setSize(new Dimension(300,340)); windowFrame.setLocation(150,140); windowFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); sampleText.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { repaint(); } }); windowFrame.setVisible(true); } public void paint(Graphics graphics) { String sampleTxt = sampleText.getText(); if (sampleTxt.length() == 0) return; if (graphics instanceof Graphics2D) { Dimension dimension = getSize(); Point point = new Point(dimension.width / 2, dimension.height / 2); int radius = (int)(point.x * 0.84); graphics.setColor(colorOfCircle); graphics.drawArc(point.x - radius, point.y - radius, radius*2-1, radius*2-1, 0, 360); graphics.setColor(colorOfText); graphics.setFont(sampleFont); CircularText((Graphics2D)graphics, sampleTxt, point, radius, -Math.PI/2, 1.0); } else { System.out.println("Some Error Occurred"); } } static void CircularText(Graphics2D graphics, String sampleTxt, Point center, double radius, double length, double height) { double circleAngle = length; Point2D circle = new Point2D.Double(center.x, center.y); char chArray[] = sampleTxt.toCharArray(); FontMetrics fm = graphics.getFontMetrics(); AffineTransform formx, formy; formx = AffineTransform.getTranslateInstance(circle.getX(),circle.getY()); for(int i = 0; i < chArray.length; i++) { double cwid = (double)(getWidth(chArray[i],fm)); if (!(chArray[i] == ' ' || Character.isSpaceChar(chArray[i]))) { cwid = (double)(fm.charWidth(chArray[i])); formy = new AffineTransform(formx); formy.rotate(circleAngle, 0.0, 0.0); String chstr = new String(chArray, i, 1); graphics.setTransform(formy); graphics.drawString(chstr, (float)(-cwid/2), (float)(-radius)); } if (i < (chArray.length - 1)) { double adv = cwid/2.0 + fm.getLeading() + getWidth(chArray[i + 1],fm)/2.0; circleAngle += Math.sin(adv / radius); } } } static int getWidth(char charText, FontMetrics fontMetrics) { if (charText == ' ' || Character.isSpaceChar(charText)) { return fontMetrics.charWidth('n'); } else { return fontMetrics.charWidth(charText); } } } <strong> </strong><strong>Output:</strong>
Example #5
2D java for text graphics.
Code:
import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; public class FontsDemo extends Frame { /** * */ private static final long <em>serialVersionUID</em> = 1L; public static void main( String[] argv ) { FontsDemo myExample = new FontsDemo( "Text Graphics" ); } public FontsDemo( String title ) { super( title ); setSize( 450, 180 ); addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent we ) { dispose(); System.<em>exit</em>( 0 ); } } ); setVisible( true ); } public void paint( Graphics g ) { Graphics2D graphics = (Graphics2D) g; FontRenderContext frc = graphics.getFontRenderContext(); Font font = new Font( "Arial", Font.<em>HANGING_BASELINE</em> | Font.<em>BOLD</em>, 72 ); TextLayout tl = new TextLayout( "eduCBA", font, frc ); Shape myShape = tl.getOutline( AffineTransform.<em>getTranslateInstance</em>( 50, 100 ) ); Paint myPaint = loadTextureResource( "1.gif" ); graphics.setPaint( myPaint ); graphics.fill( myShape ); } public TexturePaint loadTextureResource( String absfilename ) { MediaTracker tracker = new MediaTracker( this ); Image imtexture = Toolkit.getDefaultToolkit().getImage( absfilename ); tracker.addImage( imtexture, 0 ); try { tracker.waitForID( 0 ); int width = imtexture.getWidth( this ); int height = imtexture.getHeight( this ); System.<em>out</em>.println( "width" + width + " height =" + height ); BufferedImage buffImg = new BufferedImage( width, height, BufferedImage.<em>TYPE_INT_ARGB</em> ); Graphics g = buffImg.getGraphics(); g.drawImage( imtexture, 0, 0, this ); return new TexturePaint( buffImg, new Rectangle2D.Double( 0, 0, width, height ) ); } catch( Exception e ) { System.<em>out</em>.println( "Exception on Image-Texture Loading" ); } return null; } }
Output:
Conclusion
Now that we have reached the end of the article, I hope you guys must have a fair idea of what you can achieve with the Java 2D graphics. Honestly, the capability of Java 2D classes are not limited to simple shapes and figures; it can be extended to design complex figures and geometrical shapes and mostly depends on how are you leveraging the existing classes and methods.
The above is the detailed content of 2D Graphics in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip
