Results 1 to 17 of 17
Thread: Draw shapes by pressing a button
- 03-13-2010, 12:46 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Draw shapes by pressing a button
I have the following code shown below, which will be able to draw a shape once clicking on a button. For example, if you press the "Draw Rectangle" button a rectangle should be drawn and so forth.
I have reached to the point where to determine the button pressed, but UNABLE to add the drawing functionality after that. How can I draw a shape after pressing a button?
Thanks.Java Code:import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class DisposeShapes{ JLabel label = new JLabel(); JButton addRectangle; JButton addSquare; JButton addCircle; Rectangle rectangle; Graphics2D g2; JPanel panel; public void paintComponent(Graphics g) { g2 = (Graphics2D)g; rectangle = new Rectangle(100,100,100,100); } public DisposeShapes() { JFrame frame = new JFrame("Dispose shapes"); panel = new JPanel(); addRectangle = new JButton("Add Rectangle"); addRectangle.setSize(50, 100); addRectangle.addActionListener(new ButtonListener()); addSquare = new JButton("Add Square"); addSquare.setSize(50,100); addSquare.addActionListener(new ButtonListener()); addCircle = new JButton("Add Circle"); addCircle.setSize(50,100); addCircle.addActionListener(new ButtonListener()); panel.setLayout(new FlowLayout()); panel.add(addRectangle); panel.add(addSquare); panel.add(addCircle); panel.add(label); frame.getContentPane().add(panel); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == addRectangle) { } else if (e.getSource() == addSquare) { } else if(e.getSource() == addCircle) { } } } public static void main(String[] args) { DisposeShapes disposeShapes = new DisposeShapes(); } }
-
First off, your drawing should be done in a paintComponent method override that is held in a class that extends JPanel. Next, this class could have a boolean variable that the paintComponent uses to decide if it will draw a rectangle or not. The button press simply sets the boolean variable and calls repaint on the drawing JPanel.
-
For e.g.,
Java Code:public class DisposeShapes { private boolean drawRectangle = false; private boolean drawCircle = false; JLabel label = new JLabel(); JButton addRectangle; JButton addSquare; JButton addCircle; Rectangle rectangle; Graphics2D g2; JPanel panel = new JPanel() { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); myPaint(g); // to off load this into the body of the class } }; // offloaded to here private void myPaint(Graphics g) { if (drawRectangle) { g.drawRect(10, 10, 100, 100); } if (drawCircle) { g.drawOval(10, 10, 100, 100); } //.... etc... } public DisposeShapes() { JFrame frame = new JFrame("Dispose shapes"); // panel = new JPanel(); // don't need this any more
- 03-13-2010, 02:45 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
Thank you so much for your thorough reply.
The code is now as follows, but still NOT working, and have an error in: super.paintComponent(g);
Thanks.Java Code:import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class DisposeShapes{ boolean drawRectangle = false; boolean drawCircle = false; boolean drawSquare = false; JLabel label = new JLabel(); JButton addRectangle; JButton addSquare; JButton addCircle; Rectangle rectangle; Graphics2D g2; JPanel panel = new JPanel(); public void paintComponent(Graphics g) { //super.paintComponent(g); myPaint(g); } public void myPaint(Graphics g) { if (drawRectangle) { g.drawRect(10,10,100,100); } if (drawCircle) { g.drawOval(10,10, 100, 100); } if (drawSquare) { } } public DisposeShapes() { JFrame frame = new JFrame("Dispose shapes"); addRectangle = new JButton("Add Rectangle"); addRectangle.setSize(50, 100); addRectangle.addActionListener(new ButtonListener()); addSquare = new JButton("Add Square"); addSquare.setSize(50,100); addSquare.addActionListener(new ButtonListener()); addCircle = new JButton("Add Circle"); addCircle.setSize(50,100); addCircle.addActionListener(new ButtonListener()); panel.setLayout(new FlowLayout()); panel.add(addRectangle); panel.add(addSquare); panel.add(addCircle); panel.add(label); frame.getContentPane().add(panel); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == addRectangle) { drawRectangle = true; } else if (e.getSource() == addSquare) { } else if(e.getSource() == addCircle) { } } } public static void main(String[] args) { DisposeShapes disposeShapes = new DisposeShapes(); } }Last edited by SWEngineer; 03-13-2010 at 02:49 PM.
- 03-13-2010, 02:54 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 03-13-2010, 02:57 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
The error says:
The method paintComponent(Grpahics) is undefined for the type Object
-
Again as noted above, you need to have paintComponent within a class that extends JPanel. Please look more carefully at my example to see what I mean.
-
Cross-post: http://forums.sun.com/thread.jspa?messageID=10950549
OP, please read up on the proper etiquette for notifying us about cross-posts here: JavaRanch - Be Forthright When Cross Posting To Other Sites
Your cooperation on this would be greatly appreciated (and the converse is true!).
Best of luck.Last edited by Fubarable; 03-13-2010 at 03:31 PM.
- 03-13-2010, 09:42 PM #9
Another cross post:
http://www.coderanch.com/t/487035/Sw...ton-draw-shape
db
-
- 03-14-2010, 12:34 AM #11
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
I cross posted to get answers from different people.
What is the matter with that? I really don't know, especially that I will be walking with each answer I get...
-
- 03-16-2010, 02:40 PM #13
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
I will keep this post at this forum. And, will just follow up in this forum. Is that OK?
Can we complete our discussion? And, HERE ONLY?
Thanks.
- 03-16-2010, 08:20 PM #14
And how are the readers on the other forums supposed to know that you've abandoned the threads there?
db
- 03-16-2010, 10:39 PM #15
Member
- Join Date
- Jan 2010
- Posts
- 12
- Rep Power
- 0
I don't know. But, I will not make any reply there.
- 05-14-2010, 05:14 AM #16
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
I've read this post; though it is a cross-post, I still get benefit from the logic of the code. The code written above (+ correction from Fubarable) doesn't get error, but it draws nothing. But when modifying it, I still get a trouble.
I'm using NetBeans, and if I click button "bOval", it says: Error on java.lang.NullPointer on code that calls mypaint(g).
I've switched between Graphics and Graphics2D and do other alternatives, but it still get errors. Would you like to help me? THANK YOU.
And here's some of my code:
================================
public class ToolbarKanvas extends javax.swing.JFrame {
boolean pilihOval = false;
Graphics g;
JPanel KanvasDasar = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
myPaint(g);
}
};
/** Creates new form ToolbarKanvas */
public ToolbarKanvas()
{
initComponents();
this.getContentPane().add(KanvasDasar, BorderLayout.CENTER);
this.pack();
}
// untuk menggambar geometri
private void myPaint(Graphics g)
{
if (pilihOval)
{
g.drawOval(5,5,40,40);
}
}
//panggil u/ gambar.
private void bOvalMouseClicked(java.awt.event.MouseEvent evt)
{ this.pilihOval = true;
myPaint(g);
}
-
Yours is a new question by a different user and thus deserves its own thread. I recommend that you start your own thread, post a link in it to this one (or pseudo-link if the software still doesn't let newbies link), and use code tags when you post your formatted code so it will retain its formatting (see link in my signature). Much luck.
Locking old thread.
Also your class has no business with a Graphics class field as this is not how Graphics is used. Start a new thread and we can discuss this further.Last edited by Fubarable; 05-14-2010 at 05:33 AM.
Similar Threads
-
Keylistenr doesn't work after pressing any button from main aplication
By darkkis in forum New To JavaReplies: 6Last Post: 12-23-2009, 09:10 AM -
Efficient Looping to Draw Shapes
By clam in forum New To JavaReplies: 18Last Post: 10-31-2009, 04:05 AM -
Focusing the JTextField and so on while pressing Tab
By britto_bicsjohn in forum AWT / SwingReplies: 1Last Post: 08-28-2009, 08:24 AM -
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...
By l245c4l in forum Advanced JavaReplies: 2Last Post: 04-12-2009, 10:39 AM -
Displaying text in a JTextField after pressing a button
By RLRExtra in forum New To JavaReplies: 5Last Post: 01-17-2008, 09:01 PM


LinkBack URL
About LinkBacks


Bookmarks