I am just learning
Java and I found this site while searching for help. I am so glad I found you. I am taking a course in college to learn Java. My instructor has given us an assignment to write an
applet and to change the code of one of our projects to work with the applet. The first applet is suppose to pass any arguments. It sounded really simple but I find myself completely lost. Can you offer any suggestions that would help?
Here is the applet:
<applet code="BallProject.class" width = "800" height = "400">
</applet>
Here is the code:
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.event.*;
import javax.swing.*;
public class BallProject extends JApplet
{
public void init()
{
double topX; //top corner value of X
double topY; //top corner value of Y
Color c1; //color YELLLOW
Color c2; //color BLACK
topX = 100;
topY = 100;
c1 = Color.YELLOW; //instance of color yellow
c2 = Color.BLACK; //instance of color black
addMouseMotionListener(new MouseMotionHandler());
addMouseListener(new MouseHandler());
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(0, 2));
}
}
/**
A frame that contains a panel with drawings
*/
class BallFrame {
public BallFrame()
{
// add panel to frame
//BallPanel panel = new BallPanel();
//BallPanel panel2 = new BallPanel(75, 75, Color.BLUE, Color.RED);
panel.setBackground(Color.WHITE);
contentPane.add(panel);
contentPane.add(panel2);
}
}
class BallPanel extends JPanel
{
double width = 200; //width of frame of ball
double height = 200; //height of frame of ball
double deltaX;
double deltaY;
boolean c_flag = true; //double click flag
boolean p_flag; //pointer in ball flag
Ellipse2D circle; //ball
//four args constructor
//public BallPanel(int left, int top, Color color1, Color color2)
//{
// topX = left;
// topY = top;
// c1 = color1;
// c2 = color2;
// addMouseMotionListener(new MouseMotionHandler());
// addMouseListener(new MouseHandler());
//}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
//draw a circle
circle = new Ellipse2D.Double(topX, topY, width, height);
g2.setPaint(c1);
g2.fill(circle);
//create the message
String message = "Click and Drag the Circle!";
Font f = new Font("Serif", Font.BOLD, 24);
g2.setFont(f);
FontRenderContext context = g2.getFontRenderContext();
Rectangle2D bounds = f.getStringBounds(message, context);
// draw the message
FontMetrics fm = g2.getFontMetrics(f); //gets size of font
int stringLength = fm.stringWidth(message); //computes size of message
int panelWidth = this.getWidth(); //gets the width of the ball panel
int begX = (panelWidth - stringLength) / 2; //computes first X where message should start
g2.setPaint(Color.BLUE);
g2.drawString(message, begX, 352);
if (p_flag) {
if (c_flag)
{
g2.setPaint(c1);
g2.fill(circle);
}
else {
g2.setPaint(c2);
g2.fill(circle);
}
}
repaint();
}
private class MouseMotionHandler implements MouseMotionListener {
public void mouseMoved (MouseEvent event) {
}
public void mouseDragged(MouseEvent event) {
if (p_flag) {
int currentX = event.getX();
int currentY = event.getY();
topX = currentX - deltaX; //new topX
topY = currentY - deltaY; //new topX
}
repaint();
}
}
private class MouseHandler extends MouseAdapter {
public void mouseClicked(MouseEvent event) {
Point n = new Point(event.getPoint());
if (event.getClickCount() == 2){//did a double click occur??
if(circle.contains(event.getX(), event.getY())){//if you double clicked inside of the circle
c_flag = !c_flag; //reverse the color flag to use the other color
repaint(); //make a call to repaint to visualize the change in color
}
}
}
public void mousePressed(MouseEvent event)
{
//determine if mouse is in the ball
if(circle.contains(event.getX(), event.getY()))
p_flag = true;
//get values for X & Y where mouse is at
int currentX = event.getX();
int currentY = event.getY();
//calculate the movement of the mouse
deltaX = currentX - topX; //find the relative distance between the top
deltaY = currentY - topY; //corner and the place where the mouse was pressed.
topX = currentX - deltaX; //new topX
topY = currentY - deltaY; //new topX
}
public void mouseReleased(MouseEvent event) {
if(!circle.contains(event.getX(), event.getY()))
p_flag = false;
}
}
}