Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Help coderanch get a
new server
by contributing to the
fundraiser
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Swing / AWT / SWT
Messagebox with Mouseevent
shalini gnana
Ranch Hand
Posts: 189
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I tried to add messagebox when the mouse is released..But is not showing..I don't know where do i went wrong?
package MyApplet; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.text.DateFormat; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import javax.swing.*; import java.awt.*; import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.BorderFactory; import javax.swing.border.Border; public class test1 extends JApplet { JFrame frame; private static final int BALL_DIAMETER = 40; // Diameter of ball //--- instance variables /** Ball coords. Changed by mouse listeners. Used by paintComponent. */ private int _ballX = 450; // x coord - set from drag private int _ballY = 50; // y coord - set from drag /** Position in ball of mouse press to make dragging look better. */ private int _dragFromX = 0; // pressed this far inside ball's private int _dragFromY = 0; // bounding box. /** true means mouse was pressed in ball and still in panel.*/ private boolean _canDrag = false; JLabel label; String s= "aswedfrtyhgqwsadrftlopqasnhfgrchildnode1"; int width = s.length(); int border = 2; int margin = 10; int rectx = margin+border,recty =margin+border+63; int rectwidth = 8, rectheight = 6; int imgborder = 10; int imgline1 = 300; int labellocx = rectx+imgborder+13; int labellocy = recty+imgborder-1; int circlex = rectx+rectwidth; int circley = recty+rectheight; int x =rectx+rectwidth+10; int y = recty+rectheight+10; int x1 = x+17; int y1 = y+20; int titlebarx= 300; int titlebary = 40; boolean title = true; String date[] = {"0","4","8","12","16","20","24"}; String day[] = {"Mon","Tue","Wed","Thu","Fri","Sat"}; int width1 = 180; // int f2= x+168; int n =180; // int dayx = 385; long From ; long To ; ArrayList dateList; int rect1x= 300; int rect1y = 100; int rect1width= 50; int rect1heigth = 50; boolean candrag = false; int PrevX; int PrevY; public void init() { Container cont = getContentPane(); cont.setLayout(new BorderLayout()); // to see the scrollpane, the scrollpane has to be smaller // then the component held within its viewport setPreferredSize(new Dimension(300, 300)); JScrollPane scroll = new JScrollPane(); getContentPane().add(scroll, BorderLayout.CENTER); scroll.getViewport().add(new Imagepanel()); } private class Imagepanel extends JPanel implements MouseListener,MouseMotionListener { JScrollPane scroll = new JScrollPane(); Rectangle rect = new Rectangle(rectx,recty,rectwidth,rectheight); Rectangle rect1 = new Rectangle(rect1x,rect1y,rect1width,rect1heigth); Ellipse2D.Double circle = new Ellipse2D.Double(_ballX,_ballY,BALL_DIAMETER,BALL_DIAMETER); Ellipse2D.Double circle1 = new Ellipse2D.Double(x+10,y+10,4,4); boolean selected = false; boolean selected1 = false; int w ; Imagepanel() { // setting the component to be largenr than the scrollpane // just so we'll see scrollbars. setPreferredSize(new Dimension(32710,32710)); scroll = new JScrollPane(); add(scroll); addMouseListener(this); addMouseMotionListener(this); setBorder(BorderFactory.createLineBorder(Color.BLACK, border)); } protected void paintComponent(Graphics g) { int n = 168; int x = 300; int x1=300; int x2=x; int daydx=(width1/2); int dayx =(daydx+x); int f2= x+n; From = new java.util.GregorianCalendar(2007,01,01).getTime().getTime(); To = new java.util.GregorianCalendar(2007,06,30).getTime().getTime(); double Difference = To-From; long days = Math.round((Difference/(1000*60*60*24))); int noday = (int)days; Date fromDate = new java.util.GregorianCalendar(2007,01,01).getTime(); Date toDate = new java.util.GregorianCalendar(2007,06,30).getTime(); super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Ariel",Font.PLAIN,11); Font font1 = new Font("Ariel",Font.PLAIN,9); g2.fillOval(_ballX, _ballY, BALL_DIAMETER-20, BALL_DIAMETER-20); g2.setBackground(Color.GRAY); //drawing the tilte bar g2.setBackground(Color.WHITE); g2.drawLine(imgline1,0,imgline1,getHeight()); g2.setPaint(new Color(0,128,192)); g2.fillRect(imgborder,imgborder,290,45); //g2.drawLine(margin,margin,titlebarx,titlebary); //drawing the root node and labeling g2.draw(rect); g2.setPaint(Color.WHITE); g2.drawLine(margin,margin,titlebarx,titlebary); g2.drawString("RESOURCE",40,40); g2.drawString("TIME",200,25); g2.setPaint(Color.green); g2.drawString("sadasd",344,22); g2.draw(circle); rect.setLocation(rectx,recty); rect.setSize(rectwidth,rectheight); g2.setPaint(Color.BLACK); g2.drawString(" aswedfrtyhgqwsadrftlopqasnhfgrchildnode1",rectx+rectwidth+05,recty+rectheight); for (int i = 0;i<noday;i++) { Calendar c = Calendar.getInstance(); // current date c.add(Calendar.DATE, i); // add one day SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy"); // use the pattern: day_of_month String str = sdf.format(c.getTime()); // fromat the date to string System.out.println(str); // print it at the console for (int j=0;j<6;j++) { g2.setFont(font1); g2.drawLine(x1,40,x1,35); g2.drawString(date[j],x1,35); // g2.drawString(day[j],dayx,20); g2.drawString("24",f2,35); f2+=width1; x1+=30; // dayx=+170; } g2.drawRect(x,10,width1,30); x+=width1; g2.drawString(str,dayx,20); dayx+=180; } for(int k=0;k<n;k++) { for(int l=0;l<6;l++) { Calendar c = Calendar.getInstance(); // current datem c.add(Calendar.DAY_OF_WEEK, k); // add one day SimpleDateFormat sdf = new SimpleDateFormat("EEE"); // use the pattern: day_of_month String str = sdf.format(c.getTime()); // fromat the date to string System.out.println(str); // print it at the console g2.drawString(str,dayx,5); // g2.drawString(day[l],dayx,20); dayx+=180; } } //dividing the drawing panel //drawing for the click event if(selected) { Color color = Color.orange; g2.fill(circle); g2.draw(circle); // g2.drawRoundRect(x+30,y+15,200,12,5,5); g2.drawString("aswedfrtyhgqwsadrftlopqasnhfgrchildnode1",x+15,y+5); g2.drawRect(300,y,100,15); } if(selected1) { g2.setFont(new Font("Arial",Font.PLAIN,12)); g2.fill(circle1); g2.draw(circle1); g2.drawString(" aswedfrtyhgqwsadrftlopqmasnhfgrchildnode1,",x1+15,y1+5); } if(_canDrag) { g2.drawString(_ballX+", "+_ballY, _ballX, _ballY); g2.drawString(PrevY+", "+PrevX,_ballX, _ballY); } } public void update(Graphics g) { paint(g); } public void mouseClicked(MouseEvent e) { //imagePaneMouseClicked(e); } private void imagePaneMouseClicked(MouseEvent e) { Point p = e.getPoint(); if (rect.contains(p)) { if (!selected) { selected = true; repaint(); } } if(circle.contains(p)) { if(!selected1) { selected1 = true; repaint(); } } if(rect.contains(p)) { if(e.getClickCount() == 2) { selected = false; repaint(); } } } public void mousePressed(MouseEvent e) { int x = e.getX(); // Save the x coord of the click int y = e.getY(); // Save the y coord of the click if (x >= _ballX && x <= (_ballX + BALL_DIAMETER) && y >= _ballY && y <= (_ballY + BALL_DIAMETER)) { _canDrag = true; _dragFromX = x - _ballX; // how far from left _dragFromY = y - _ballY; // how far from top } else { _canDrag = false; } this.repaint(); } public void mouseReleased(MouseEvent e) { if(candrag){ frame = new JFrame(); try { JOptionPane d = new JOptionPane(); d.showInternalMessageDialog(frame,"message","dfgfg",JOptionPane.CANCEL_OPTION); frame.setVisible(true); } finally { frame.dispose(); } } this.repaint(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { candrag = false; } public void mouseDragged(MouseEvent e) { PrevX = _ballX; PrevY = _ballY; if (_canDrag) { // True only if button was pressed inside ball. //--- Ball pos from mouse and original click displacement _ballX = e.getX() - _dragFromX; _ballY = e.getY() - _dragFromY; //--- Don't move the ball off the screen sides _ballX = Math.max(_ballX, 0); _ballX = Math.min(_ballX, getWidth() - BALL_DIAMETER); //--- Don't move the ball off top or bottom _ballY = Math.max(_ballY, 0); _ballY = Math.min(_ballY, getHeight() - BALL_DIAMETER); } } public void mouseMoved(MouseEvent e) { } } }
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Popup menu not responding with mouse event
mouse event for an array of object
My scrollbars are not displaying
AWT Background Problem
Dispalying random dates
More...