473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem in drawing a rectangle in swings...

38
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 MouseMotionListener 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.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
//import javax.swing.SwingUtilities;


public class Swing_Create_Frame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Frame in Java Swing");
frame.setBounds(400,400, 400,450);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.add(new MyPanel());
frame.pack();
}
}
class MyPanel extends JPanel {

public MyPanel() {
setBorder(BorderFactory.createLineBorder(Color.red ));
}

public Dimension getPreferredSize() {
return new Dimension(400,400);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw Text
g.drawString("This is my custom Panel!",10,20);
}

MyListener myListener = new MyListener();
void addMouseListener(myListener);

addMouseMotionListener(myListener);

public class MyListener extends MouseInputAdapter {
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
currentRect = new Rectangle(x, y, 0, 0);
updateDrawableRect(getWidth(), getHeight());
repaint();
}

public void mouseDragged(MouseEvent e) {
updateSize(e);
}

public void mouseReleased(MouseEvent e) {
updateSize(e);
}

void updateSize(MouseEvent e) {
int x = e.getX();
int y = e.getY();
currentRect.setSize(x - currentRect.x,
y - currentRect.y);
updateDrawableRect(getWidth(), getHeight());
Rectangle totalRepaint = rectToDraw.union(previouseRectDrawn);
repaint(totalRepaint.x, totalRepaint.y,
totalRepaint.width, totalRepaint.height);
}
}
}
}

Errors i'm getting are
Multiple markers at this line
- MyListener cannot be resolved to a type
- Watchpoint:MyPanel [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", VariableDeclaratorId 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,
Nov 5 '07 #1
3 3918
r035198x
13,262 8TB
1.) Use code tags when posting code
2.) Read Sun's event handling tutorial here.
Nov 5 '07 #2
sumuka
38
1.) Use code tags when posting code
2.) Read Sun's event handling tutorial here.

I read it but i'm not able to figure to my mistake.Plz anybody guide me ....
Nov 5 '07 #3
r035198x
13,262 8TB
I read it but i'm not able to figure to my mistake.Plz anybody guide me ....
What do you want to achive with this line then?

void addMouseListener(myListener);
If you look at the examples from the link I posted, you'll see that you don't put the return type (in this case void) when registering the listener. You are supposed to be calling a method not defining one.
Nov 5 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Billy Jacobs | last post by:
I have created a DataGridColumnDatePicker Component so that I can put a datetimepicker control in my datagrid. It almost works. When I put my mouse in the cell it changes to a datetimepicker...
3
by: Jeroen Ceuppens | last post by:
Hi, When i do this in my programma, in the contstructor of the form: g.DrawImage(i,new Rectangle(0,0,i.Width,i.Height),0,0,i.Width,i.Height,GraphicsUnit.Pixel); it doens't paint it ;( but if...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
4
by: steve bull | last post by:
I have created a UserControl and added it to my toolbox. It appears in the toolbox with the icon I created for it. The problem is that when I drag the control onto a form no code is generated. If I...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
4
by: Carlo, MCP | last post by:
Hello I found an abnormal behaviour on LinearGradientBrush (or FillRectangle). This code Public Class PanelPanel Inherits System.Windows.Forms.Panel Protected Overrides Sub OnPaint(ByVal e...
1
by: a4w | last post by:
I am not sure if this is too-granular a question to post to this group; however, I did not see a Tablet-specific group. I am experiencing a problem with the Microsoft.Ink.Ink.HitTest(Point , float)...
11
by: Tool69 | last post by:
Hi, I've got a simple but difficult problem : Suppose I've got a Paper class, on wich I can draw i.e a rectangle, a circle or whatever. class Paper(...): def __init__(self, paperx, papery):...
4
by: RobinS | last post by:
I am drawing a rectangle on a picture that has already been drawn on the graphics area (a user control). It works something like this: //in the MouseDown event m_isDragging = true; m_oldX =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.