- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make a canvas in Java Swing?
To make a canvas with Java Swing, use the Graphics2D class −
public void paint(Graphics g) { Graphics2D graphic2d = (Graphics2D) g; graphic2d.setColor(Color.BLUE); graphic2d.fillRect(100, 50, 60, 80); }
Above, we have created a rectangle and also added color to it.
The following is an example to make a canvas in Java −
Example
package my; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo extends JPanel { @Override public void paint(Graphics g) { Graphics2D graphic2d = (Graphics2D) g; graphic2d.setColor(Color.BLUE); graphic2d.fillRect(100, 50, 60, 80); } public static void main(String[] args) { JFrame frame = new JFrame("Demo"); frame.add(new SwingDemo()); frame.setSize(550, 250); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
This will produce the following output −
- Related Articles
- How to make a Tkinter canvas rectangle transparent?
- How to maximize JFrame in Java Swing
- How to add a title to JTable in Java Swing?
- How to highlight a row in a table with Java Swing?
- How to change Button Border in Java Swing
- How to add JTable to Panel in Java Swing?
- How do I make a transparent canvas in HTML5?
- How to make a Button using the Tkinter Canvas widget?
- How to leave space with EmptyBorder in Java Swing
- How to display row count in Java Swing JList
- Java Program to append a row to a JTable in Java Swing
- How to add a new row to JTable with insertRow() in Java Swing
- How to set a different look and feels to swing components in Java?
- How to highlight multiple rows in a sequence in a table with Java Swing?
- How to set fullscreen mode for Java Swing Application?

Advertisements