views:

158

answers:

4

Hello Folks, well this is my code:

import javax.swing.*;
import javax.*;
import java.awt.*;
import java.awt.Color;
import java.awt.Graphics.*;
import java.awt.event.*;
import javax.swing.UIManager;
public class SimpleGUI extends JFrame{
               public SimpleGUI(){
                       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

               }





           public void go(){
                  Drawpanel = new Mypanel();
               JFrame frame = new JFrame("Chasing Line");

               frame.getContentPane().add(BorderLayout.CENTER, Drawpanel);
               frame.setSize(300,300);
               frame.setVisible(true);


                  Drawpanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {


                            public void mouseMoved(java.awt.event.MouseEvent evt) {
                                      DrawpanelMouseMoved(evt);
                              }
                 }); 




           }

                  public void DrawpanelMouseMoved(java.awt.event.MouseEvent evt) {

                                   xpos=evt.getX();
                                   ypos=evt.getY();
                                System.out.println("Coordinates : X :"+ xpos+"Y: "+ypos);
                             Drawpanel.paintImage(xpos,ypos);



                  } 

        class Mypanel extends JPanel{


                        public void paintImage(int xpost,int ypost){
                                           Graphics d = getGraphics();
                                            d.setColor(Color.black);
                                           d.drawLine(xpost, 0, xpost, this.getHeight());
                                           d.setColor(Color.red);
                                           d.drawLine(0, ypost, this.getWidth(),ypost);
                                           this.validate();




                        }

        } // end the inner class 


               public static void main(String[] args){
                               try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");


                                    } catch(Exception e) {

                                        System.err.println("Look and feel not set");

                                    }

                            SimpleGUI win = new SimpleGUI();

                            win.go();


               }

                      Mypanel Drawpanel;
                      private int xpos=0;
                      private int ypos=0;

}  // close SimpleGUI class

The problem is how can i delete the old lines?, i mea,make only the current x and y lines appear on the screen, make the intersection between both lines "follow" the mouse pointer.

thanks for any reply.

+1  A: 

The problem is how can i delete the old lines?, i mea,make only the current x and y lines appear on the screen, make the intersection between both lines "follow" the mouse pointer.

  • Save all your lines that you want to keep in a LinkedList or similar.
  • Start painting with drawing the background again, this will clear your old lines.
  • Paint all your lines in your LinkedList.

Or if you don't want to save any lines:

  1. Draw the background again, this removes the old lines.
  2. Draw your lines.

You can draw the background again with:

clearRect(int x, int y, int width, int height)
Jonas
thank you for you answer, but i hava another issue, if i button widgets(button,, text box, whatever) all get deleted, how can i conserve the widget?
Jeyjey
@Jeyjey: What are you trying to do? Add your components to other panels, not the one you are painting on.
Jonas
A: 

you'll need to do d.clear(); just after the Graphics d = getGraphics(); line, this will clear the existing graphics and what you draw on it after that will be all that's shown.

oedo
thank you for you answer, but i hava another issue, if i button widgets(button,, text box, whatever) all get deleted, how can i conserve the widget?
Jeyjey
not sure what you mean there? you probably want the thing you're drawing on to be a separate swing widget with buttons around/above it?
oedo
A: 

You should never use code that invokes getGraphics(). Custom painting is done by overriding the paintComponent() method.

Custom Painting Approaches shows 2 different approaches for doing some basic painting.

I'm not sure I understand your question about the "widgets", but the examples have buttons that are not affected by the painting.

camickr
A: 

this is the update code

My code:

import javax.swing.; import javax.; import java.awt.; import java.awt.Color; import java.awt.Graphics.; import java.awt.event.*; import javax.swing.UIManager; public class SimpleGUI extends JFrame{ public SimpleGUI(){ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

           }





       public void go(){
              Drawpanel = new Mypanel();
           JFrame frame = new JFrame("Chasing Line");
           JButton mybutton1 = new JButton("Please");
           JButton mybutton2 = new JButton("Help");
           JButton mybutton3 = new JButton("Me!!");
           Drawpanel.add(mybutton1); 
           Drawpanel.add(mybutton2);
           Drawpanel.add(mybutton3);

           frame.getContentPane().add(BorderLayout.CENTER, Drawpanel);
           frame.setSize(300,300);
           frame.setVisible(true);


              Drawpanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {


                        public void mouseMoved(java.awt.event.MouseEvent evt) {
                                  DrawpanelMouseMoved(evt);
                          }
             }); 




       }

              public void DrawpanelMouseMoved(java.awt.event.MouseEvent evt) {

                               xpos=evt.getX();
                               ypos=evt.getY();
                            System.out.println("Coordinates : X :"+ xpos+"Y: "+ypos);
                         Drawpanel.paintImage(xpos,ypos);



              } 

    class Mypanel extends JPanel{


                    public void paintImage(int xpost,int ypost){
                                       Graphics d = getGraphics();
                                        d.clearRect(0,0, this.getWidth(), this.getHeight());
                                        d.setColor(Color.black);
                                       d.drawLine(xpost, 0, xpost, this.getHeight());
                                       d.setColor(Color.red);
                                       d.drawLine(0, ypost, this.getWidth(),ypost);
                                       this.validate();




                    }

    } // end the inner class 


           public static void main(String[] args){
                           try {
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");


                                } catch(Exception e) {

                                    System.err.println("Look and feel not set");

                                }

                        SimpleGUI win = new SimpleGUI();

                        win.go();


           }

                  Mypanel Drawpanel;
                  private int xpos=0;
                  private int ypos=0;

} // close SimpleGUI class

the question is how can i keep those 3 buttons with out alterating their state?

thank for any reply!

Jeyjey
Exactly what are you trying to do?
Jonas