Results 1 to 5 of 5
- 09-24-2010, 10:26 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
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.
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).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 I'm stuck at this...
hopefully someone can get me on the right track, thx in advance :)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.?????? } } }
- 09-24-2010, 10:55 PM #2
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.
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.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.
-
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!
- 09-25-2010, 02:50 AM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
when you have followed the others advice check out Graphics2D's translate method.
http://download.oracle.com/javase/1....8int,%20int%29
- 09-25-2010, 09:03 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Use stop button to stop moving (stop timers) on JPanel
By mneskovic in forum New To JavaReplies: 3Last Post: 07-23-2010, 12:50 PM -
Drawing on Jpanel
By nonabhai in forum AWT / SwingReplies: 0Last Post: 03-13-2010, 04:46 AM -
Drawing with JPanel
By m00nchile in forum New To JavaReplies: 2Last Post: 02-18-2010, 08:12 PM -
Drawing moving object
By dotabyss in forum Java 2DReplies: 1Last Post: 02-16-2010, 11:20 PM -
Moving Images in JPanel
By killpoppop in forum AWT / SwingReplies: 7Last Post: 03-08-2009, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks