Notes
![]() ![]() Notes - notes.io |
import java.awt.Graphics;
import javax.swing.JPanel;
public class Shapes extends JPanel {
private final int choice; // user's choice of which shape to draw
// constructor sets the user's choice
public Shapes(int userChoice) {
choice = userChoice;
}
// draws a cascade of shapes starting from the top-left corner
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 10; i++) {
// pick the shape based on the user's choice
switch (choice) {
case 1 -> // draw rectangles
g.drawRect(10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10);
case 2 -> // draw ovals
g.drawOval(10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10);
}
}
}
}
1.2.shape test
import javax.swing.JFrame; // handle the display
import javax.swing.JOptionPane;
public class ShapesTest
{
public static void main(String[] args)
{
// obtain user's choice
String input = JOptionPane.showInputDialog(
"Enter 1 to draw rectanglesn" +
"Enter 2 to draw ovals");
int choice = Integer.parseInt(input); // convert input to int
// create the panel with the user's input
Shapes panel = new Shapes(choice);
JFrame application = new JFrame(); // creates a new JFrame
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300, 300);
application.setVisible(true);
}
}
2.draw panel
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel1 extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
g.drawLine(0, 0, width, height);
g.drawLine(0, height, width, 0);
}
}
2.1. draw panel test
import javax.swing.JFrame;
public class DrawPanel1Test {
public static void main(String[] args) {
DrawPanel1 panel = new DrawPanel1();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(500, 500);
application.setVisible(true);
}
}
3.draw smiely
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawSmiley extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(10, 10, 200, 200);
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
g.fillOval(50, 110, 120, 60);
g.setColor(Color.BLACK);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
}
}
3.1- smiely test
import javax.swing.JFrame;
public class DrawSmileyTest
{
public static void main(String[] args)
{
DrawSmiley panel = new DrawSmiley();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(300 ,300);
application.setVisible(true);
}
}
4.dialoge
import javax.swing.JOptionPane;
public class Dialog {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Welcome to Java");
}
}
5.
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team