Drawing methods:
Many applications require drawing. For example, if you were developing an application that carried out the graphical display of data, say in a pie chart, then you would need to draw a circle and some lines and also fill in areas with some colour.
There is a special class known as Graphics that enables drawing to take place. The Graphics class is an abstract class that provides a graphical context for drawing, which the local Java system translates to the actual graphics device that is being used when the graphics operations occur.
The drawing process is achieved by means of the programmer providing a concrete implementation of a painting method, taking a Graphics object as its argument, and carrying out drawing actions on that object, which will be displayed on a screen. If you look in the Java class library you will find that the class Component, which forms the basis of all the GUI objects, has a paint method in it. Hence all the elements of the Swing toolkit inherit this method.
The paint method is invoked whenever a GUI object is created or some external action that requires redrawing (such as a window being moved) occurs. It is usually called indirectly by the program through such methods as repaint and setVisible. An example of the use of paint is shown below:
import javax.swing. *;
import java.awt.Graphics;
public class SimpleGraphic extends JFrame
{
/ * Here we override the method paint to create what we want to appear on screen. */
public void paint (Graphics g)
{
super.paint(g);
g.drawRect(50, 50, 100, 75);
}
public SimpleGraphic (String title)
{
super(title); setSize(300, 300);
addWindowListener(new CloseAndQuit( ));
}
}
public class SimpleGraphicTest
{
public static void main (String [] args)
{
SimpleGraphic world = new SimpleGraphic("Simple Graphic");
world.setVisible(true);
}
}
In this example, as shown in Figure, a rectangle is drawn at an offset (50, 50) from the origin, with a width of 100 and height of 75. The measurements are expressed in pixels, which stands for ‘picture elements'. A picture element represents a single dot in a displayed image.
Figure A SimpleGraphic frame showing a rectangle
We have shown the use of the paint method here for illustration, however in general it is recommended that you write your drawing code in a paintComponent method in a JPanel subclass (or another subclass of JComponent) and then add this component to your top-level container, such as a JFrame (or one of its subclasses).
The origin on an application is at the topmost left-hand position in the window. The x-direction then increases across the screen to the right and the y-direction increases down the screen. There are a number of methods available that allow us to draw closed figures and lines. A selection of these methods are described in Table.
Table Methods to draw figures and lines
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Drawing methods homework and assignments? Live Drawing methods experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Drawing methods homework help, java assignment help and Drawing methods projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours