CONTENTS:
I. WORKSPACE
II. PROJECT
III. JAVA PACKAGE
IV. CLASS
V. ECLIPSE SDK SOURCE EDITOR
VI. ECLIPSE DEBUGGER
VII. DEBUGGING EXAMPLE PROGRAM ANALYSIS
VIII. DEBUGGING EXAMPLE PROGRAM SOURCE CODE
IX. REFERENCES
YOU WILL LEARN HOW TO:
1. Create a Java Project.
2. Create a Java Package.
3. Create a Java Class.
4. Use the Eclipse Source Editor.
5. Use the Eclipse Debugger.
I.
WORKSPACE
Start Eclipse.
From the Java Eclipse SDK left click File and select Switch
Workspace. This option shows you the path
to your workspace location.
On my computer my Java workspace location is as follows:
C:\Eclipse Workspace
II.
PROJECT
The Java project contains source code and related files for building a
java program.
Perform the following steps to create a project using the Eclipse New
Project Wizard:
A.
From the
Java Eclipse SDK click File.
B.
From File select New.
C.
From New select Project.
D.
From the New
Java Project wizard, at “Project name” type in your project name then accept
the other defaults on this window by pressing Next. The example program Project name is frakal.
E.
From the New
Java Project wizard next window accept the default options by pressing Finish.
Eclipse will create your project and
return you to the Java - Eclipse SDK.
A Java Package contains related class files and allows you to use
short-cut class names.
Perform the following steps to create a package:
A.
From the
Java Eclipse SDK Package tab window select the appropriate project folder
where you want the Package created, right click the project folder and select New.
B.
From New select Package to launch the Eclipse New Java Package Wizard.
C.
From the New
Java Package Wizard, at package “Name” type the package name. The example program package name is frakal.
D.
From the New
Java Package Wizard click Finish.
The package will be created and you will be returned to the Java - Eclipse
SDK.
A class is a blueprint for creating objects.
Perform the following steps to create a class:
A.
From the
Java Eclipse SDK Package tab window select the appropriate package in the src folder, right click the package, and select New.
B.
From New select Class to launch the Eclipse New Java Class Wizard.
C.
From the Eclipse
New Java Class Wizard, at “Name” type in a class name. The example program class name is frakal.
D.
From the Eclipse
New Java Class Wizard accept the default settings by selecting Finish. The class will be created and you will be
returned to the Java Eclipse SDK.
A.
Java Eclipse
SDK Editor Window Font
If you want to change the Java Eclipse SDK Editor Window font to make
the text easier to read perform the following steps:
1. From the Java Eclipse SDK Editor Window, left
click Window.
2. From Window left click Preferences.
3. From Preferences left click General.
4. From General left click Appearance.
5. From Appearance left click Colors and Fonts.
6. From Colors and Fonts left click Java Editor Text Font.
7. From Java Editor Text Font left click the Edit button.
8. From Java Editor Text Font select the font
you prefer, for example 12 point and bold to make it easier to see.
9. From Java Editor Text Font left click OK.
10. From
Java Editor Text Font left click Apply.
11. From
Java Editor Text Font left click OK and you will be returned to the Java
Eclipse SDK Editor Window.
You can repeat these steps at any time and adjust the font to whatever
size you need to make it easy to read the source code.
B.
Java Eclipse
SDK Editor Window Size
To give you more space, double left click on the tab at the top left of
the Editor Window where your class appears, and the Editor Window will expand to
full screen to give you more screen work area room.
C.
Editor Line
Numbers
Perform the following steps if you would like to have source code line
numbers:
1. From the Java Eclipse SDK Editor Window left
click Window.
2. From Window left click Preferences.
3. From Preferences left click General.
4. From General left click Editors.
5. From Editors left click Text Editors.
6. From Text Editors left click the box that
says Show Line Numbers.
7. From Text Editors left click Apply.
8. From Text Editors left click OK and
you will be returned to the Java Eclipse SDK Editor Window.
At this point your Java Eclipse SDK Editor Window should contain something
similar to the following based on the names you selected:
package frakal;
public class frakal {
}
Notice the first line is the “package statement” ending with a
semicolon.
The third line is the “class statement.” The body of the class is placed between the two
left and right curly braces ({ }).
When you are debugging lots of line numbers, from the Debug
perspective, you can left click Navigate, and left click “Go to Line ” to
obtain the "Go to Line" pop up window.
From the "Go to Line" pop up window enter the line number you want to go
to, left click OK and you will go to that line number.
Copy the example program. Then paste the example program into the body of the frakal class
you created on the blank line between the two left and right curly braces ({ }).
package frakal;
public class frakal
{
}
VI.
ECLIPSE DEBUGGER
A.
Break Point
From the Java Eclipse SDK Editor Window double left click in the grey shaded
margin to the left of a line number and you will see the Debugger Break Point
circle icon appear. The icon is a toggle
which can be turned on and off by double left clicking.
A break point suspends the thread execution of the source code, before the
line containing the break point is executed.
B.
Debug
After you set your break point run debug.
You will discover there are several ways to enter Debug and of course you can use whatever method you prefer. For example, from the Java Eclipse SDK Editor Window you can enter Debug by doing one of the following:
1.
Left click
Window, then left click Open Perspective, and then left click Debug.
2.
Left click
Debug on the tool bar at the far right of the top of your screen.
3.
Press F11.
C.
Java Eclipse
SDK Debug Perspective
The Java Eclipse SDK Debug Perspective has several tab windows, each of
which has buttons. Hovering your mouse
pointer over a button will reveal to you the purpose of that button.
1.
Debug shows
the call stack and status of all current threads, including any threads that
have already run to completion, and the line of code the debugger is currently
executing.
The following buttons for moving around within the program are on the far right of the title bar of the Debug view tab:
a. Step Into,
F5, is used to Step Into a method and stop at the first line of executable code
inside that method.
Normally you only want to step into methods in your own classes and not
into the third-party packages or standard Java packages. In order to accomplish this left click
Window, left click Preferences, left click Java, left click Debug, left click
Step Filtering, left click Select All, left click Apply, and left click OK.
b. Step Over,
F6, executes the current line of code and stops at the next line.
c. Step Return, F7, executes everything within a method entered by using Step Into
and then returns to the next line following the line which contained the method
we previously stepped into.
d. Resume,
F8, will resume execution of the program until it meets the next breakpoint.
Another way of moving around within the program is to right click on
the line you want the program to run to, then select Run To Line or use
Ctrl+r. The program will run to the line
you selected and stop.
2.
Java Editor
(Source Code Window)
The Java Editor shows the Java source code. When Debug executes the program pauses at the
first executable line on or after the breakpoint.
3.
Console
Console shows the programs console output.
4.
Tasks
The Tasks view displays the list of to-do items, or tasks that you have
added manually.
5.
Variables
Variables shows the current variables and their values.
6.
Breakpoints
Breakpoints lists all the breakpoints.
You can either disable or enable the breakpoints by checking or
un-checking the check mark next to each label.
7.
Outline
Outline lists an outline of the current source file.
8.
LogCat
The LogCat messages give you the full breakdown of what caused the
error, including the line number.
If you X out of (close) a perspective tab window and you want to see it
again, left click Window, then left click Show View, then left click the
perspective tab window you closed and Eclipse will show it again.
For example, when you first enter the debug perspective, you will not
be able to view the perspective tab window Expressions tab until you
left click Window, then left click Show View, then left click Expressions. You can use the Expressions tab to have the
Debugger evaluate and keep track of expressions.
Another tab window you will not be able to initially view is the Display
tab. To view the Display tab left
click Window, then left click Show View, then left click Display. The Display View displays the result of
evaluating an expression in the context of the current stack frame.
When you want to exit the debugger, refer to the top of the Java
Eclipse SDK Debug tab window tool bar, and left click the red button. Then left click Window, left click Open
Perspective, and left click Java to return to the Java Eclipse SDK Editor
Window.
VII.
DEBUGGING EXAMPLE PROGRAM ANALYSIS
The example program is a "fractal." I think fractals are fantastic. I
deliberately included this fractal as an example program in order to give you a
more challenging opportunity to practice debugging. Complex ideas involve lots of user created
methods and lots of open source utility methods. I will cover enough in this section of the
tutorial to get you started. If you have
not previously looked at open source utility methods this will be an eye opener
for you. If you have problems post them
as comments and I will reply to your comments.
A. Break Points
Go to the main method and set a break point at the following line:
new fraktal(args);
The break point should be on line 124. After you have the break point set, from the main tool bar left click Debug and the program will run and pause at the line of
code where you set the break point. The
debugger has paused the program at the break point before executing “new
fractal(args);”.
B. Step Into
From the Break Point, left click Step Into and you will enter the fractal(args) method
for debugging. When you left click Step
Into the debugger will Step Into a method and stop at the first line of
executable code inside that method. In
this case it has stopped at the following line of code:
super("Mandelbrot set
fractal");
C. Step Return
Left click Step Return and you will return to the next line following
the line which contained the method we previously stepped into. Our main method only contains one
method. Therefore, when you left click
Step Return the program creates the “Mandelbrot Set Fractal” and returns to the
right curly brace which ends the body of the main method as shown below.
}
I hope you like the fractal, I think it is very pretty. Close the “Mandelbrot Set Fractal” and left
click the red terminate button in the Debug view title bar.
From the Java editor source code window set a second break point at the
first for statement below the main method on the following line.
for (int i = 0; i < 50; i++)
Then from the top tool bar left click Run, then left click Debug and
the program will run and pause at the line of code where you set the first
break point.
From the Break Point, left click Step Into and you will enter the
fractal(args) method for debugging.
D. Step Over
Now that we are in the fractal(args) method we can walk through the lines of
code in the method by left clicking Step Over.
Each time we left click Step Over, the debugger executes the current
line of code and stops at the next line as shown below.
setSize(800,
800);
E. Resume
When we left click Resume or press F8 the Eclipse Debugger will resume
execution of the program until it meets the next breakpoint. Therefore, when we left click Resume we
arrive at our second break point.
Left clicking Step Over places us inside the for loop.
The for loop has 50 iterations.
If we want to continue using Step Over and we prefer not left clicking
Step Over 50 times, we can go to the Variables tab, find the variable i which
increments the for loop, and change the value of i in the Value column. Then we can continue left clicking Step Over.
F. Watch
In the for loop there are variables whose value changes you might want
to watch. To do so left click on the
variable, then right click and select Watch.
The variable you selected for watching will appear in the Expressions
tab window where you can watch them change as you Step Over statements.
Programs that run right the first time are no fun. Happiness is using your debugger! May you always be happy and have programs with lots of bugs to debug!
VIII. JAVA ECLIPSE DEBUGGER EXAMPLE PROGRAM SOURCE CODE
/*
Java Eclipse Debugger Tutorial
*/
/*
Java Eclipse Debugger Tutorial
*/
package
fraktal;
import
javax.swing.*;
/*
JPanel
*/
/*
JPanel
*/
import
java.awt.*;
/*
Graphics
*/
/*
Graphics
*/
import
java.awt.image.*;
import
java.awt.event.*;
public class fraktal extends JFrame
implements MouseListener
{
BufferedImage fractal;
WritableRaster raster;
Calculator calc;
int mouse_x, mouse_y;
double x;
double y;
double width;
double height;
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited (MouseEvent e) { }
public void mousePressed(MouseEvent e)
{
mouse_x = e.getX( );
mouse_y = e.getY( );
}
public void mouseReleased(MouseEvent e)
{
double tmpx =
translateX(mouse_x);
double tmpy = translateY(mouse_y);
double tmp = translateX(e.getX( ));
x = tmpx;
y = tmpy;
width = Math.abs(tmp - tmpx);
height = 0.75 * width;
while (!calc.isDone( ))
{
try {
Thread.sleep(1000);
} catch (InterruptedException ex) { }
} catch (InterruptedException ex) { }
}
calc = new Calculator(800,
800, raster, this);
calc.setInsets(x, y, width,
height);
calc.start( );
}
public double translateX(int ix)
{
return x + (((double) ix) /
800) * width;
}
}
public double translateY(int iy)
{
return y + (((double) iy) /
800) * height;
}
public fraktal (String args[ ])
{
super("Mandelbrot Set
Fractal");
setSize(800, 800);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
Component graphics = new Component( )
{
public void
paint(Graphics g)
{
g.drawImage(fractal,
0, 0, null);
}
};
graphics.addMouseListener(this);
getContentPane( ).add(graphics);
fractal = new
BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
raster = fractal.getRaster( );
System.out.println("Raster
top left corner: " + raster.getMinX( ) + "," + raster.getMinY( ));
System.out.println("Raster
size: " + raster.getWidth( ) + "," + raster.getHeight( ));
setVisible(true);
calc = new Calculator(800,
800, raster, this);
x = -1.5;
y = -1;
width = 2;
height = 0.75 * width;
if (args.length == 0)
{
calc.setInsets(-1.5,
-1, 2, 0.75 * 2);
}
if (args.length == 3)
{
x =
Double.parseDouble(args[0]);
y = Double.parseDouble(args[1]);
width = Double.parseDouble(args[2]);
height = 0.75 *
width;
calc.setInsets(x,
y, width, height);
}
System.out.println("Rectangle
calculation: ("+x+","+y+") to (" + (x + width) +
"," + (y + height)+")");
calc.start( );
}
/*
Main Method
*/
Main Method
*/
public static void main(String args[ ])
{
new fraktal(args);
}
}
class
Calculator extends Thread
{
WritableRaster raster;
int sizex;
int sizey;
double startx;
double starty;
double width;
double height;
double z;
double zi;
int[ ] black;
int[ ] red;
Object[ ] colors;
JFrame parent;
boolean done;
public Calculator(int sx, int sy,
WritableRaster r, JFrame p)
{
parent = p;
raster = r;
sizex = sx;
sizey = sy;
z = 0;
zi = 0;
black = new int[3];
black[0] = 0;
black[1] = 0;
black[2] = 0;
red = new int[3];
red[0] = 255;
red[1] = 0;
red[2] = 0;
done = true;
colors = new Object[200];
int c = 255;
for (int i = 0; i < 50;
i++)
{
int[ ] color = new
int[3];
color[0] = 0;
color[1] = 255 -
c;
color[2] = c;
colors[i] = color;
c -= 5;
}
c = 255;
for (int i = 50; i < 100;
i++)
{
int[ ] color = new
int[3];
color[0] = 255 - c;
color[0] = 255 - c;
color[1] = 255;
color[2] = 0;
colors[i] = color;
c -= 5;
}
}
c = 255;
for (int i = 100; i < 150;
i++)
i++)
{
int[ ] color = new
int[3];
color[0] = c;
color[0] = c;
color[1] = c;
color[2] = 255 -
c;
colors[i] = color;
c -= 5;
}
c = 255;
for (int i = 150 ; i < 200
; i++)
{
int[ ] color = new
int[3];
color[0] = 255 - c;
color[0] = 255 - c;
color[1] = 0;
color[2] = 255;
colors[i] = color;
c -= 5;
}
}
public void setInsets(double sx, double
sy, double w, double h)
{
startx = sx;
starty = sy;
width = w;
height = h;
}
public static double comp_mult_real(double
a, double b, double c, double d)
{
return (a * c) - (b * d);
}
public static double comp_mult_imag(double
a, double b, double c, double d)
{
return (a * d) + (b * c);
}
public int mandelbrotTest(double a, double
bi)
{
double atmp;
double btmp;
int number = 0;
double z = 0;
double zi = 0;
while ( (number != 200)
&& (comp_magnitude(z, zi) < 2))
{
number++;
atmp =
comp_mult_real(z, zi, z, zi);
btmp =
comp_mult_imag(z, zi, z, zi);
z = atmp;
zi = btmp;
z += a;
zi += bi;
}
if (number == 200)
{
return -1;
}
}
else
{
return number;
}
}
}
public boolean isDone( )
{
return done;
}
}
public static double comp_magnitude(double
a, double b)
{
return Math.sqrt( a * a + b *
b);
}
public void run( )
{
double dx = width / sizex;
double dy = height / sizey;
double z = startx;
double zi = starty;
done = false;
System.out.println("Mandelbrot
set fractal calculation is in progress.");
for (int x = 0; x < sizex;
x++)
{
zi = starty;
int it;
for (int y = 0; y
< sizey; y++)
{
if
((it = mandelbrotTest(z, zi)) != -1)
{
/*
Within Mandelbrot set.
*/
raster.setPixel(x,
y, (int[ ]) colors[it]);
}
else
{
/*
Not within Mandelbrot set.
*/
raster.setPixel(x,
y, black);
}
zi +=
dy;
}
if ( (x%5) == 0)
{
parent.repaint( );
}
z += dx;
}
done = true;
System.out.println("Mandelbrot
set fractal calculation has completed!");
}
}
EXAMPLE OUTPUT:
Raster top left corner: 0,0
Raster size: 800,800
Rectangle calculation: (-1.5,-1.0) to (0.5,0.5)
Mandelbrot set fractal calculation is in progress.
Mandelbrot set fractal calculation has completed!
EXAMPLE OUTPUT:
Raster top left corner: 0,0
Raster size: 800,800
Rectangle calculation: (-1.5,-1.0) to (0.5,0.5)
Mandelbrot set fractal calculation is in progress.
Mandelbrot set fractal calculation has completed!
IX.
REFERENCE
Java How To Program Eighth Edition by Paul Deitel and Harvey Deitel (Upper Saddle River , N.J. :
Pearson Education, Inc., 2010).
YOU HAVE LEARNED HOW TO:
1. Create a Java Project.
2. Create a Java Package.
3. Create a Java Class.
4. Use the Eclipse Source Editor.
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment