Results 1 to 5 of 5
  1. #1
    CAD
    CAD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Smile Cant figure it out (Moving a drawing in JPanel via a button).

    hi guys :)

    This is what I already made, and works as it should work.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyFrame extends JFrame implements ActionListener {
      private JButton b1;
      private JPanel p;
        private JTextField tf;
    
      public MyFrame() {
        setLayout(new FlowLayout());
        p = new JPanel(); add(p);
            p.setPreferredSize(new Dimension(500, 300));
            p.setBackground(Color.WHITE);
        tf = new JTextField(20); add(tf);
            b1 = new JButton("Press me"); add(b1);
        b1.addActionListener(this);
            
            setSize(540, 400); 
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
          }
    
      public void actionPerformed(ActionEvent e) {
    
            String s = tf.getText();
            if (s.equals("")) {
                s = "Java";
            }
            Graphics g = p.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 500, 100);
            g.setColor(Color.BLACK);
                int x = 10, y = 20;
            for ( int i = 1 ; i <= 10 ; i = i + 1) {
    
              g.drawString(s, x, y);
                    x = x + 7* s.length() + 15;    
                    y = y + 7* s.length() + 5;
              }    
     
          }  
    }
    So.....that just shows you a drawing of 'text' going down, but next issue is, that i want to add several buttons (i'll start with just one), and by pressing the button (lets call it "To the right"), the entire drawing (thats already on JPanel), moves X amount of pixels to the right. (have to the same for down, and diagonally down).

    So I'm stuck at this...

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyFrame extends JFrame implements ActionListener {
      private JButton b1, b2;
      private JPanel p;
        private JTextField tf;
    
      public MyFrame() {
        setLayout(new FlowLayout());
        p = new JPanel(); add(p);
            p.setPreferredSize(new Dimension(500, 300));
            p.setBackground(Color.WHITE);
        tf = new JTextField(20); add(tf);
            b1 = new JButton("Press me"); add(b1);
        b1.addActionListener(this);
        b2 = new JButton("To the right"); add(b2);
        b2.addActionListener(this);
        
            setSize(540, 400); 
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
          }
    
      public void actionPerformed(ActionEvent e) {
          if (event.getSource() == b1) {
            String s = tf.getText();
            if (s.equals("")) {
                s = "Java";
            }
            Graphics g = p.getGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 500, 100);
            g.setColor(Color.BLACK);
                int x = 10, y = 20;
            for ( int i = 1 ; i <= 10 ; i = i + 1) {
    
              g.drawString(s, x, y);
                    x = x + 7* s.length() + 15;    
                    y = y + 7* s.length() + 5;
            }    
          }  
            else if (event.getSource() == b2) {
                Graphics g = p.getGraphics();
                b2.??????
                }
       
          } 
     
    }
    hopefully someone can get me on the right track, thx in advance :)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,514
    Rep Power
    25

    Default

    Most drawing in panels is done by extending the panel and overriding its paintComponent method. Action listeners are used to detect user desires, do some calculations about where to draw the shape, set the values and then call repaint() which calls the paintComponent method which then follows the instructions set by the listener.
    that i want to add several buttons (i'll start with just one), and by pressing the button (lets call it "To the right"), the entire drawing (thats already on JPanel), moves X amount of pixels to the right.
    So the listener for the button, would compute the new position for the drawing and call repaint which would call paintComponent and the drawing is made at the new location.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Agree with Norm. If you haven't done so, please have a look at the Sun tutorials on Swing Graphics and 2D Graphics and they'll show you examples of drawing in a JPanel's paintComponent method as Norm suggests.

    Luck!

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    when you have followed the others advice check out Graphics2D's translate method.

    http://download.oracle.com/javase/1....8int,%20int%29

  5. #5
    CAD
    CAD is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Thx, gonna check it out this afternoon and let u guys know if I made it all work :)

Similar Threads

  1. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  2. Drawing on Jpanel
    By nonabhai in forum AWT / Swing
    Replies: 0
    Last Post: 03-13-2010, 04:46 AM
  3. Drawing with JPanel
    By m00nchile in forum New To Java
    Replies: 2
    Last Post: 02-18-2010, 08:12 PM
  4. Drawing moving object
    By dotabyss in forum Java 2D
    Replies: 1
    Last Post: 02-16-2010, 11:20 PM
  5. Moving Images in JPanel
    By killpoppop in forum AWT / Swing
    Replies: 7
    Last Post: 03-08-2009, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •