change applet to frame

Hello everibody!

I have a program which works with MouseEvents.
Unfortunately it uses an JApplet, but I need a JFrame, and when I try to change it , gives me error messages.
What can I do?
Here are the codes:

[code]
package mousedemo;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

//////////////////////////////////////////////////////////////// class MouseTest
public class MouseTest extends JFrame implements MouseListener{
JTextArea _loggingTA = new JTextArea(10, 40);
MouseGraphicsComponent _display = new MouseGraphicsComponent();

//============================================================== constructor
public MouseTest() {
JButton clearBtn = new JButton("Clear");
JLabel commentsLbl = new JLabel("Experiment with combinations of " +
"mouse buttons and modifier keys.");

//... Add listeners.
_display.addMouseListener(this);
clearBtn.addActionListener(new ClearAction());

//... Do layout.
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(clearBtn);
topPanel.add(commentsLbl);

JPanel content = new JPanel();
content.setLayout(new BorderLayout(5, 5));
content.add(topPanel, BorderLayout.NORTH);
content.add(_display, BorderLayout.WEST);
content.add(new JScrollPane(_loggingTA), BorderLayout.CENTER);

this.setContentPane(content);
}

//============================================================== method main
public static void main(String[] args) {
JFrame window = new JFrame("Mouse Demo");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new MouseTest());
window.pack(); // Do the layout.
window.setLocationRelativeTo(null); // Center it.
window.setVisible(true);
}

//========================================================== mouse listeners
// I've violated the normal formatting rules because these all belong
// together and all do the same thing. Normally, some of them would do
// something, and others would just ignore the event (no statements
// in the method body).
//
// For ActionEvents, it's a very bad idea to have the outer class
// implement ActionListener because typically there are many things that
// might cause these events: buttons, menu items, radio buttons, checkboxes,
// etc, so there would need to be lots of separate listeners.
//
// Mouse events are different. First, they are usually handled
// within a component which listens to its own events, as in the
// other class here. Handling them outside the component, as in this class,
// is unusual, and it's unlikely that more than one component's mouse
// events would be handled outside. If there was a need to separate the
// mouse events from several components, it would be easy to just put the
// listeners in an inner class that implements MouseListener.
// That might, or might not, be a cleaner way to organize the code.
public void mouseClicked(MouseEvent e) { log(e); }
public void mouseEntered(MouseEvent e) { log(e); }
public void mouseExited(MouseEvent e) { log(e); }
public void mousePressed(MouseEvent e) { log(e); }
public void mouseReleased(MouseEvent e){ log(e); }

//====================================================================== log
// Utility method to handle common logging action by all listeners.
private void log(MouseEvent e) {
_loggingTA.append(e.paramString() + '
');
}

//////////////////////////////////////////////////////////////// ClearAction
class ClearAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_loggingTA.setText(""); // Clear text in logging area.
_display.clear(); // Clear graphics area.
}
}
}
[/code]

And the other code:

[code]
package mousedemo;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/////////////////////////////////////////////////// class MouseGraphicsComponent
class MouseGraphicsComponent extends JComponent implements MouseMotionListener {

//======================================================= instance variables
//... The appropriate listener sets the instance variables,
// then calls repaint(), which schedules a call to paintComponent,
// which uses these variable values.
private int _lastMovedX = 0; // x coord of mouse move
private int _lastMovedY = 0; // y coord of mouse move

//... List of points for line constructed by dragging.
ArrayList _points = new ArrayList(1000);

//============================================================== constructor
public MouseGraphicsComponent() {
this.setPreferredSize(new Dimension(200, 200));
this.addMouseMotionListener(this); // Listen to mouse moves, drags.
}

//================================================== override paintComponent
@Override public void paintComponent(Graphics g) {
//... Paint background.
g.setColor(Color.YELLOW);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);

//... Draw coordinates of the mouse as it moves.
String coords = "x=" + _lastMovedX + ", y=" + _lastMovedY;
g.drawString(coords, _lastMovedX, _lastMovedY);

//... Draw lines between each of the points (if any).
if (_points.size() > 1) {
Point previousPoint = _points.get(0);
for (int i = 1; i < _points.size(); i++) {
Point thisPoint = _points.get(i);
g.drawLine(previousPoint.x, previousPoint.y
, thisPoint.x, thisPoint.y);
previousPoint = thisPoint;
}
}
}

//====================================================== listener mouseMoved
// This sets the x, y coordinate that are used by paintComponent.
// Could also save as a Point object, but ints are sometimes simpler.
public void mouseMoved(MouseEvent e) {
//log(e);
_lastMovedX = e.getX();
_lastMovedY = e.getY();
this.repaint(); // Show the change. Indirectly calls paintComponent.
}

//==================================================== listener mouseDragged
// Save each point we dragged over to show as a "line" in paintComponent.
// Saved as a Point object instead of ints because ArrayList needs object.
public void mouseDragged(MouseEvent e) {
_points.add(e.getPoint()); // Save the point for drawing.
this.repaint(); // Show the change. Indirectly calls paintComponent.
}

//==================================================================== clear
// Clear the points that dragging recorded.
public void clear() {
_points.clear();
this.repaint(); // Show the change. Indirectly calls paintComponent.
}
}
[/code]

Comments

  • : Hello everibody!
    :
    : I have a program which works with MouseEvents.
    : Unfortunately it uses an JApplet, but I need a JFrame, and when I try to change it , gives me error messages.
    : What can I do?
    : Here are the codes:
    :
    : [code]
    : package mousedemo;
    :
    : import java.awt.*;
    : import java.awt.event.*;
    : import javax.swing.*;
    :
    : //////////////////////////////////////////////////////////////// class MouseTest
    : public class MouseTest extends JFrame implements MouseListener{
    : JTextArea _loggingTA = new JTextArea(10, 40);
    : MouseGraphicsComponent _display = new MouseGraphicsComponent();
    :
    : //============================================================== constructor
    : public MouseTest() {
    : JButton clearBtn = new JButton("Clear");
    : JLabel commentsLbl = new JLabel("Experiment with combinations of " +
    : "mouse buttons and modifier keys.");
    :
    : //... Add listeners.
    : _display.addMouseListener(this);
    : clearBtn.addActionListener(new ClearAction());
    :
    : //... Do layout.
    : JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    : topPanel.add(clearBtn);
    : topPanel.add(commentsLbl);
    :
    : JPanel content = new JPanel();
    : content.setLayout(new BorderLayout(5, 5));
    : content.add(topPanel, BorderLayout.NORTH);
    : content.add(_display, BorderLayout.WEST);
    : content.add(new JScrollPane(_loggingTA), BorderLayout.CENTER);
    :
    : this.setContentPane(content);
    : }
    :
    : //============================================================== method main
    : public static void main(String[] args) {
    : JFrame window = new JFrame("Mouse Demo");
    : window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : window.setContentPane(new MouseTest());
    : window.pack(); // Do the layout.
    : window.setLocationRelativeTo(null); // Center it.
    : window.setVisible(true);
    : }
    :
    : //========================================================== mouse listeners
    : // I've violated the normal formatting rules because these all belong
    : // together and all do the same thing. Normally, some of them would do
    : // something, and others would just ignore the event (no statements
    : // in the method body).
    : //
    : // For ActionEvents, it's a very bad idea to have the outer class
    : // implement ActionListener because typically there are many things that
    : // might cause these events: buttons, menu items, radio buttons, checkboxes,
    : // etc, so there would need to be lots of separate listeners.
    : //
    : // Mouse events are different. First, they are usually handled
    : // within a component which listens to its own events, as in the
    : // other class here. Handling them outside the component, as in this class,
    : // is unusual, and it's unlikely that more than one component's mouse
    : // events would be handled outside. If there was a need to separate the
    : // mouse events from several components, it would be easy to just put the
    : // listeners in an inner class that implements MouseListener.
    : // That might, or might not, be a cleaner way to organize the code.
    : public void mouseClicked(MouseEvent e) { log(e); }
    : public void mouseEntered(MouseEvent e) { log(e); }
    : public void mouseExited(MouseEvent e) { log(e); }
    : public void mousePressed(MouseEvent e) { log(e); }
    : public void mouseReleased(MouseEvent e){ log(e); }
    :
    : //====================================================================== log
    : // Utility method to handle common logging action by all listeners.
    : private void log(MouseEvent e) {
    : _loggingTA.append(e.paramString() + '
    ');
    : }
    :
    : //////////////////////////////////////////////////////////////// ClearAction
    : class ClearAction implements ActionListener {
    : public void actionPerformed(ActionEvent e) {
    : _loggingTA.setText(""); // Clear text in logging area.
    : _display.clear(); // Clear graphics area.
    : }
    : }
    : }
    : [/code]
    :
    : And the other code:
    :
    : [code]
    : package mousedemo;
    :
    : import java.awt.*;
    : import java.awt.event.*;
    : import java.util.ArrayList;
    : import javax.swing.*;
    :
    : /////////////////////////////////////////////////// class MouseGraphicsComponent
    : class MouseGraphicsComponent extends JComponent implements MouseMotionListener {
    :
    : //======================================================= instance variables
    : //... The appropriate listener sets the instance variables,
    : // then calls repaint(), which schedules a call to paintComponent,
    : // which uses these variable values.
    : private int _lastMovedX = 0; // x coord of mouse move
    : private int _lastMovedY = 0; // y coord of mouse move
    :
    : //... List of points for line constructed by dragging.
    : ArrayList _points = new ArrayList(1000);
    :
    : //============================================================== constructor
    : public MouseGraphicsComponent() {
    : this.setPreferredSize(new Dimension(200, 200));
    : this.addMouseMotionListener(this); // Listen to mouse moves, drags.
    : }
    :
    : //================================================== override paintComponent
    : @Override public void paintComponent(Graphics g) {
    : //... Paint background.
    : g.setColor(Color.YELLOW);
    : g.fillRect(0, 0, this.getWidth(), this.getHeight());
    : g.setColor(Color.BLACK);
    :
    : //... Draw coordinates of the mouse as it moves.
    : String coords = "x=" + _lastMovedX + ", y=" + _lastMovedY;
    : g.drawString(coords, _lastMovedX, _lastMovedY);
    :
    : //... Draw lines between each of the points (if any).
    : if (_points.size() > 1) {
    : Point previousPoint = _points.get(0);
    : for (int i = 1; i < _points.size(); i++) {
    : Point thisPoint = _points.get(i);
    : g.drawLine(previousPoint.x, previousPoint.y
    : , thisPoint.x, thisPoint.y);
    : previousPoint = thisPoint;
    : }
    : }
    : }
    :
    : //====================================================== listener mouseMoved
    : // This sets the x, y coordinate that are used by paintComponent.
    : // Could also save as a Point object, but ints are sometimes simpler.
    : public void mouseMoved(MouseEvent e) {
    : //log(e);
    : _lastMovedX = e.getX();
    : _lastMovedY = e.getY();
    : this.repaint(); // Show the change. Indirectly calls paintComponent.
    : }
    :
    : //==================================================== listener mouseDragged
    : // Save each point we dragged over to show as a "line" in paintComponent.
    : // Saved as a Point object instead of ints because ArrayList needs object.
    : public void mouseDragged(MouseEvent e) {
    : _points.add(e.getPoint()); // Save the point for drawing.
    : this.repaint(); // Show the change. Indirectly calls paintComponent.
    : }
    :
    : //==================================================================== clear
    : // Clear the points that dragging recorded.
    : public void clear() {
    : _points.clear();
    : this.repaint(); // Show the change. Indirectly calls paintComponent.
    : }
    : }
    : [/code]
    :
    What kind of errors, run-time errors or compilation errors? What are the error messages: invalid type-cast, unknown identifier, out of bounds, etc, etc.?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion