Hello,
I'm doing a project in java using swings ,im able to create a panel and frame but i have a problem in event handling.I have added the MouseMotionList ener but it's giving some error .Here i want to draw a rectangle when the user clicks and drags the mouse button and releases a rectangle has to be drawn.can somebody help me to debug the error ASAP.
My code goes like this:
import javax.swing.*;
import javax.swing.JPa nel;
import javax.swing.Bor derFactory;
import java.awt.Color;
import java.awt.Dimens ion;
import java.awt.Graphi cs;
//import javax.swing.Swi ngUtilities;
public class Swing_Create_Fr ame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame in Java Swing");
frame.setBounds (400,400, 400,450);
frame.setVisibl e(true);
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
frame.add(new MyPanel());
frame.pack();
}
}
class MyPanel extends JPanel {
public MyPanel() {
setBorder(Borde rFactory.create LineBorder(Colo r.red));
}
public Dimension getPreferredSiz e() {
return new Dimension(400,4 00);
}
public void paintComponent( Graphics g) {
super.paintComp onent(g);
// Draw Text
g.drawString("T his is my custom Panel!",10,20);
}
MyListener myListener = new MyListener();
void addMouseListene r(myListener);
addMouseMotionL istener(myListe ner);
public class MyListener extends MouseInputAdapt er {
public void mousePressed(Mo useEvent e) {
int x = e.getX();
int y = e.getY();
currentRect = new Rectangle(x, y, 0, 0);
updateDrawableR ect(getWidth(), getHeight());
repaint();
}
public void mouseDragged(Mo useEvent e) {
updateSize(e);
}
public void mouseReleased(M ouseEvent e) {
updateSize(e);
}
void updateSize(Mous eEvent e) {
int x = e.getX();
int y = e.getY();
currentRect.set Size(x - currentRect.x,
y - currentRect.y);
updateDrawableR ect(getWidth(), getHeight());
Rectangle totalRepaint = rectToDraw.unio n(previouseRect Drawn);
repaint(totalRe paint.x, totalRepaint.y,
totalRepaint.wi dth, totalRepaint.he ight);
}
}
}
}
Errors i'm getting are
Multiple markers at this line
- MyListener cannot be resolved to a type
- Watchpoint:MyPa nel [access and modification] - myListener
- MyListener cannot be resolved to a type
Multiple markers at this line
- Syntax error on token ")", { expected after this token
- myListener cannot be resolved to a type
- Syntax error on token "myListener ", VariableDeclara torId expected after this token
These are the errors i'm getting at line no 38 and 39 which i have underlined.
Can anybody tell me what should i do?
Thanks in anticipation,
I'm doing a project in java using swings ,im able to create a panel and frame but i have a problem in event handling.I have added the MouseMotionList ener but it's giving some error .Here i want to draw a rectangle when the user clicks and drags the mouse button and releases a rectangle has to be drawn.can somebody help me to debug the error ASAP.
My code goes like this:
import javax.swing.*;
import javax.swing.JPa nel;
import javax.swing.Bor derFactory;
import java.awt.Color;
import java.awt.Dimens ion;
import java.awt.Graphi cs;
//import javax.swing.Swi ngUtilities;
public class Swing_Create_Fr ame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame in Java Swing");
frame.setBounds (400,400, 400,450);
frame.setVisibl e(true);
frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
frame.add(new MyPanel());
frame.pack();
}
}
class MyPanel extends JPanel {
public MyPanel() {
setBorder(Borde rFactory.create LineBorder(Colo r.red));
}
public Dimension getPreferredSiz e() {
return new Dimension(400,4 00);
}
public void paintComponent( Graphics g) {
super.paintComp onent(g);
// Draw Text
g.drawString("T his is my custom Panel!",10,20);
}
MyListener myListener = new MyListener();
void addMouseListene r(myListener);
addMouseMotionL istener(myListe ner);
public class MyListener extends MouseInputAdapt er {
public void mousePressed(Mo useEvent e) {
int x = e.getX();
int y = e.getY();
currentRect = new Rectangle(x, y, 0, 0);
updateDrawableR ect(getWidth(), getHeight());
repaint();
}
public void mouseDragged(Mo useEvent e) {
updateSize(e);
}
public void mouseReleased(M ouseEvent e) {
updateSize(e);
}
void updateSize(Mous eEvent e) {
int x = e.getX();
int y = e.getY();
currentRect.set Size(x - currentRect.x,
y - currentRect.y);
updateDrawableR ect(getWidth(), getHeight());
Rectangle totalRepaint = rectToDraw.unio n(previouseRect Drawn);
repaint(totalRe paint.x, totalRepaint.y,
totalRepaint.wi dth, totalRepaint.he ight);
}
}
}
}
Errors i'm getting are
Multiple markers at this line
- MyListener cannot be resolved to a type
- Watchpoint:MyPa nel [access and modification] - myListener
- MyListener cannot be resolved to a type
Multiple markers at this line
- Syntax error on token ")", { expected after this token
- myListener cannot be resolved to a type
- Syntax error on token "myListener ", VariableDeclara torId expected after this token
These are the errors i'm getting at line no 38 and 39 which i have underlined.
Can anybody tell me what should i do?
Thanks in anticipation,
Comment