Skip to main content

Reactangle example in java


                    Swing Rectangle example



Rectangles
 

To draw rectangles, we use the drawRect() method. To fill rectangles with the current color, we use the fillRect() method.
 

package PRACTICEPROGRAM;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


class DrawPanel extends JPanel {
 

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

            g2d.setColor(new Color(212, 212, 212));
            g2d.drawRect(10, 15, 90, 60);
            g2d.drawRect(130, 15, 90, 60);
            g2d.drawRect(250, 15, 90, 60);
            g2d.drawRect(10, 105, 90, 60);
            g2d.drawRect(130, 105, 90, 60);
            g2d.drawRect(250, 105, 90, 60);
            g2d.drawRect(10, 195, 90, 60);
            g2d.drawRect(130, 195, 90, 60);
            g2d.drawRect(250, 195, 90, 60);

            g2d.setColor(new Color(125, 167, 116));
            g2d.fillRect(10, 15, 90, 60);

            g2d.setColor(new Color(42, 179, 231));
            g2d.fillRect(130, 15, 90, 60);

            g2d.setColor(new Color(70, 67, 123));
            g2d.fillRect(250, 15, 90, 60);

            g2d.setColor(new Color(130, 100, 84));
            g2d.fillRect(10, 105, 90, 60);

            g2d.setColor(new Color(252, 211, 61));
            g2d.fillRect(130, 105, 90, 60);

            g2d.setColor(new Color(241, 98, 69));
            g2d.fillRect(250, 105, 90, 60);

            g2d.setColor(new Color(217, 146, 54));
            g2d.fillRect(10, 195, 90, 60);

            g2d.setColor(new Color(63, 121, 186));
            g2d.fillRect(130, 195, 90, 60);

            g2d.setColor(new Color(31, 21, 1));
            g2d.fillRect(250, 195, 90, 60);
    }
}

public class RectanglesExample extends JFrame {

    public RectanglesExample() {
        initUI();
    }

    public final void initUI() {

        DrawPanel dpnl = new DrawPanel();
        add(dpnl);

        setSize(360, 300);
        setTitle("Rectangles");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                RectanglesExample ex = new RectanglesExample();
                ex.setVisible(true);
            }
        });
    }
}
 

In the example we draw nine colored rectangles.
g2d.setColor(new Color(212, 212, 212));
g2d.drawRect(10, 15, 90, 60);

We set the color of the outline of the rectangle to a soft gray color, so that it does not interfere with the fill color. To draw the outline of the rectangle, we use the drawRect() method. The first two parameters are the x and y values. The third and fourth are width and height.

g2d.fillRect(10, 15, 90, 60);


To fill the rectangle with a color, we use the fillRect() method.
 


Join us on Facebook: Click Here 

Comments

Popular posts from this blog

Menu bar example with applet viewer in java

      Menu bar +applet viewer example-3 import java.applet.*; import java.awt.*;  /*   <applet code="MenuDemo" height=200 width=200>   </applet> */ public class MenuDemo extends Applet {             public void init( )             {                         Menus m = new Menus("Menu Bar Demo");                                    m.resize(200,200);       m.show();             } } class Menus extends Frame {             Menus(String s)             {                         super(s);                         Me...

Java question

  Java ques interview Ques:-List any five features of Java?  Ans :-Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded Ques . write full form of jdbc & odbc ?  Ans :-  JDBC -java database connectivity.             ODBC - open database connectivity Ques:- what are types of packages ? Ans: 1. Pre-defined packages 2. User defined packages Ques :-Define class?  Ans:- A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object. Ques:- Denfie string bulider? Ans :- Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects. Ques :- What is Singleton class?  Ans:Singleton class control object creation, limiting the number to one but allowing the flexibility to create m...

MVC Example 1

                        Swing MVC Example in java Java Swing first programs In this chapter, we will program our first programs in Swing toolkit. The examples are going  to be very simple. We will cover some basic functionality. Our first example In our first example, we will show a basic window. import javax.swing.JFrame; public class Simple extends JFrame { public Simple() { setSize(300, 200); setTitle("Simple"); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Simple simple = new Simple(); simple.setVisible(true); } } While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer. import javax.swing.JFrame; Here we import the JFrame widget. It is a toplevel container, which is used for plac...