Using Java! My Paint Application Develop a paint application which includes func
ID: 3633586 • Letter: U
Question
Using Java!
My Paint Application
Develop a paint application which includes functionality for drawing different shapes. These diagrams can be drawn using different color and fill options. The application should have menus as well as toolbar to select different shapes. A sample application window is as shown in the diagram:
Following points need to be noted for developing the application:
1. A toolbar (JToolBar) should contain a number of buttons each representing a shape to be drawn. Once a shape is selected by mouse, the button is shown pressed. The shapes to be drawn are Circles, Ovals, Rectangles, Squares and Lines. Buttons in the toolbar should have appropriate icons and tooltips.Bonus: You can add more shapes like arc, polygon etc.
2. A shape can also be selected from the menu “Current Shape”. The menu has JRadioButtonMenuItems so that only one is selected at any given time. It is important that the toolbar selection and menu item selection is synchronized.
3. A settings menu has a menu item called “Drawing Settings”. Once clicked, it opens a window to select the drawing settings like “Color”, “Style: Normal” or “Style: Filled”.Bonus: You can add more drawing settings like round edge or corner edges for rectangles and square etc.
4. A status bar (JLabel) at the bottom of the application shows some status messages like the current selected shape, the mouse coordinates etc. as shown in sample figures.Bonus: If you draw a shape all the previous shapes maybe stored in an array and redrawn every time else every time you draw a new shape, the previous shape will be erased.
Explanation / Answer
Introduction
Applet is a program to run on the browser and it is embedded on the web page. This program is not system level program but it is a network level program. The Applet class is a super class of any applet. Applet viewer is used to view or test the applet whether the applet is running properly or not.
In this program we will see how to draw the different types of shapes like line, circle and rectangle. There are different types of methods for the Graphics class of the java.awt.*; package have been used to draw the appropriate shape. Explanation of the methods used in the program is given just ahead :
Graphics.drawLine() :
The drawLine() method has been used in the program to draw the line in the applet. Here is the syntax for the drawLine() method :
drawLine(int X_from_coordinate, int Y_from_coordinate, int X_to_coordinate, int Y_to_coordinate);
Graphics.drawString() :
The drawSring() method draws the given string as the parameter. Here is the syntax of the drawString() method :
drawString(String string, int X_coordinate, int Y_coordinate);
Graphics.drawOval() :
The drawOval() method draws the circle. Here is the syntax of the drawOval() method :
g.drawOval(int X_coordinate, int Y_coordinate, int Wdth, int height);
Graphics.drawRect() :
The drawRect() method draws the rectangle. Here is the syntax of the drawRect() method :
g.drawRect(int X_coordinate, int Y_coordinate, int Wdth, int height)
Here is the java code of the program :.
import java.applet.*;
import java.awt.*;
public class CircleLine extends Applet{
int x=300,y=100,r=50;
public void paint(Graphics g){
g.drawLine(3,300,200,10);
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.drawString("Circle",275,100);
g.drawRect(400,50,200,100);
g.drawString("Rectangel",450,100);
}
}
Here is the HTML code of the program:
============================================================================
/*
Applet will paint special shapes and use colors and fonts
Only new methods are explained
*/
import java.awt.*;
import java.applet.*;
public class DrawExample extends Applet
{
// Specify variables that will be needed everywhere, anytime here
// The font variable
Font bigFont;
// The colors you will use
Color redColor;
Color weirdColor;
Color bgColor;
public void init()
{
// Here we will define the varibles further
// Will use Arial as type, 16 as size and bold as style
// Italic and Plain are also available
bigFont = new Font("Arial",Font.BOLD,16);
// Standard colors can be named like this
redColor = Color.red;
// lesser known colors can be made with R(ed)G(reen)B(lue).
weirdColor = new Color(60,60,122);
bgColor = Color.blue;
// this will set the backgroundcolor of the applet
setBackground(bgColor);
}
public void stop()
{
}
// now lets draw things on screen
public void paint(Graphics g)
{
// tell g to use your font
g.setFont(bigFont);
g.drawString("Shapes and Colors",80,20);
// Now we tell g to change the color
g.setColor(redColor);
// This will draw a rectangle (xco,yco,xwidth,height);
g.drawRect(100,100,100,100);
// This will fill a rectangle
g.fillRect(110,110,80,80);
// change colors again
g.setColor(weirdColor);
// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
// ovals are also possible this way.
g.fillArc(120,120,60,60,0,360);
g.setColor(Color.yellow);
// Draw a line (int x1, int y1, int x2, int y2)
g.drawLine(140,140,160,160);
// reset the color to the standard color for the next time the applets paints
// an applet is repainted when a part was'nt visible anymore
// happens most often because of browser minimizing or scrolling.
g.setColor(Color.black);
}
}
// that's some basic drawing.
// next is drawing images on screen
// go to imageExample.java
===========================================================================
Java applets are programs written in the object-oriented programming language Java. Applets tend to perform one basic task, and do so in a contained environment like a web browser. You will often see applets displaying graphical content, such as photos or geometrical shapes. Drawing rectangles and circles with Java involves working with the "Graphics" class, which contains a host of tools for creating and managing visual content. Developing such graphical content opens a new realm of artistic expression not possible with text-based programs. It also promotes rich communication with your program's users.
Related Searches:
Java Applet HelpJava Cache Example
Difficulty: Moderate
Instructions
Things You'll Need
Integrated Development Environment (IDE) for Java
1
Download a free IDE (see Resources) to enable rapid, error-free development of your graphical applet.
2
Press "File > New" on your IDE's toolbar to create a new Java project. Choose the option for creating a plain "Java application," as opposed to a "class library" or other project type.
3
Name the project "ShapeApplet," and choose the option to create a main class for this project. Name the main class "ShapeApplet.Main." Type these names carefully because Java is case sensitive.
4
Select all the program code that the IDE creates in the file "Main.java" and press the "Delete" key. Paste in the following code:
/////////////////////////////
package ShapeApplet;
import java.applet.*;
import java.awt.*;
public class Main extends Applet{
public void init(){
}
public void paint(Graphics g){
g.drawLine(0,0, 75, 75);
}
}
/////////////////////////////
5
Select your IDE's option for running the project: choose either "Run File" or "Run" under the "Run" menu.
6
Notice the output from the program: a small window appears and displays a thin, black line extending from its upper-left corner toward its lower-right. Click the window's "X" button to close it.
7
Change the shape of the program's graphic by replacing the statement "g.drawline..." with this statement:
g.drawRect(10, 10, 150, 200);
Re-run the program and notice the resulting rectangle.
8
Create a filled rectangle by closing the program's window and then replacing the "drawRect" text with the text "fillRect." Re-run the program.
9
Change the rectangle's color by typing the following statement before the "g.drawRect" statement:
g.setColor(Color.red);
(Note: you can use other common colors in place of red. But be sure to type your color in lower-case letters.)
10
Create a circle by replacing the "g.drawRect" statement with:
g.fillOval(10, 10, 200, 200);
11
Draw an arbitrary polygon by replacing the complete "paint" method with the following:
public void paint(Graphics g){
int []xpoints = {0, 80, 150, 200, 350};
int []ypoints = {0, 3, 50, 100, 150};
Polygon poly = new Polygon(xpoints, ypoints, xpoints.length);
g.fillPolygon(poly);
}
Run the program as described in Step 5.
Ruby Challenges, Win Cashjoin.CloudSpokes.com
Compete in Ruby Dev contests to develop your skills, win cash
BASIC for Windowswww.libertybasic.com
Liberty BASIC gives you a power toolkit for Windows programming!
Create a Free Slideshowtripwow.tripadvisor.com/slideshow
Create free animated slideshows from your travel photos in minutes!
Embedded Dashboardswww.intellicus.com
Interactive dashboards & advanced visualization to embed in your app
Ads by Google
References
Sun Microsystems: Java API: the Paint Method
Sun Microsystems: Java API: the Fill Polygon Method
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.