8 Replies - 4398 Views - Last Post: 30 November 2009 - 08:09 PM Rate Topic: -----

#1 dukky  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 111
  • Joined: 28-November 09

Drawing In A JFrame Using A Button

Post icon  Posted 29 November 2009 - 03:37 PM

Okay what i want to do here is make a frame with a panel at the bottom portion of the frame and it will have three buttons on the panel. Whenever one of the buttons is pressed i want the action of the button to draw something in the top portion of the JFrame. my problem is I don't know how to draw onto the JFrame within the button's action. Can someone give me an example of what i need to do? if you need to see my code to know what i'm trying to do just let me know and i'll post it.. it's kind of on another computer right now so i don't feel like getting it if i don't need to.

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing In A JFrame Using A Button

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Drawing In A JFrame Using A Button

Posted 29 November 2009 - 03:53 PM

You could override the paintComponent method, and within that, test to see if a given flag is set to true, and if it is, then do the drawing. If not, then do nothing. When you push the button, the flag is set to true and repaint is called, so that it paints on the JFrame.

boolean flag = false;
public void paintComponent(Graphics g) {
	 if (flag) {
		 // Paint stuff
	 }
}

public void actionPerformed(ActionEvent e) {
	  // If it is the button then do this
	  if (it is the right button) {
		  flag = true;
		  repaint();
	  }
}


Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Drawing In A JFrame Using A Button

Posted 29 November 2009 - 09:27 PM

JFrame has BorderLayout by default
If you want to drawn better to do it in a JPanel with null layout

class MyFrame extends JFrame implements ActionListener {

	String[] buttonText = {"But 1", "But 2", "But 3"};
	JButton[] threeButtons = new JButton[buttonText.length];
	int buttonclicked = -1;
 
	 Drawing drawingPanel = new DrawingPanel();

	 MyFrame() {
		 // the 3 buttons with their action listener in a GridLayout
		 JPanel buttonPanel = new JPanel(new GridLayout(1, buttonText.length));
		 for(int i = 0; i < buttonText.length; i++) {
			 threeButtons[i] = new JButton(buttonText[i]);
			 buttonPanel.add(threeButtons[i]);
			 threeButtons[i].addActionListener(this);
		 }
		 // put SOUTH
		 add(buttonPanel, BorderLayout.SOUTH):

		 // the drawing panel
		 add(frawingPanel, BorderLayout.CENTER);
	 }

	 public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		// found which button was clicked
		for(int i = 0; i < threeButtons.length; i++) {
			if(o == threeButtons[i]) {
				buttonclicked = i;
				break;
			}
		 }
	 }

	 class DrawingPanel() {
 
		 DrawingPanel() {
			super(null);			 // null layout
		 }

		 public void paint(Graphics g) {
			super.paint(g);
			
			// depending button clicked
			switch(buttonclicked) {

				  case 0:
					 // draw button 0 clicked
					 break;
				  case 1:
					 // draw button 0 clicked
					 break;
				  case 2:
					 // draw button 0 clicked
					 break;
			}
		 }
	   }
}


Was This Post Helpful? 1

#4 dukky  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 111
  • Joined: 28-November 09

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 04:42 PM

okay i tried the code above and i understand it pretty well but i can't get it to work. i pasted into DrJava and there were a few errors so i worked it down to this:
 import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class MyFrame extends JFrame implements ActionListener {

	String[] buttonText = {"But 1", "But 2", "But 3"};
	JButton[] threeButtons = new JButton[buttonText.length];
	int buttonclicked = -1;

	 DrawingPanel drawingPanel = new DrawingPanel();

	 MyFrame() {
		 // the 3 buttons with their action listener in a GridLayout
		 JPanel buttonPanel = new JPanel(new GridLayout(1, buttonText.length));
		 for(int i = 0; i < buttonText.length; i++) {
			 threeButtons[i] = new JButton(buttonText[i]);
			 buttonPanel.add(threeButtons[i]);
			 threeButtons[i].addActionListener(this);
		 }
		 // put SOUTH
		 add(buttonPanel, BorderLayout.SOUTH);

		 // the drawing panel
		 add(drawingPanel, BorderLayout.CENTER);
	 }

	 public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		// found which button was clicked
		for(int i = 0; i < threeButtons.length; i++) {
			if(o == threeButtons[i]) {
				buttonclicked = i;
				break;
			}
		 }
	 }


	 class DrawingPanel extends JFrame{

		 DrawingPanel() {
			super();			 // null layout
		 }

		 public void paint(Graphics g) {
		   // super.paint(g);
			
			// depending button clicked
			switch(buttonclicked) {

				  case 0:
					 // draw button 0 clicked
					 break;
				  case 1:
					 // draw button 0 clicked
					 break;
				  case 2:
					 // draw button 0 clicked
					 break;
			}
		 }
	   }
	 
	 public static void main(String []args)
	 {
	   MyFrame xd = new MyFrame();
	   xd.show();
	 }
}


this code compiles with no errors and i dont think there were any changes that will change the action of the program, but when i run the program i get this error: java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at MyFrame.<init>(MyFrame.java:26)
at MyFrame.main(MyFrame.java:68)

i understand what the error is saying, i'm putting a JFrame in a JFrame. how do can i fix this problem?
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 06:12 PM

class DrawingPanel extends JFrame{



Should probably be
class DrawingPanel extends JPanel{



That way, it can be added to a container!
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 06:17 PM

View PostDogstopper, on 30 Nov, 2009 - 05:12 PM, said:

class DrawingPanel extends JFrame{



Should probably be
class DrawingPanel extends JPanel{



That way, it can be added to a container!


Sorry my mistake
Thanks Dogstopper... your turn :)
We should always test code instead of just typing it in a reply

and this is wrong to

super(); // null layout

should be

super(null);

super() generates a FlowLayout
Was This Post Helpful? 1
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 06:30 PM

I always try to pbl. I mean look, my 31st project this week in just netbeans!
Attached Image
Was This Post Helpful? 0
  • +
  • -

#8 dukky  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 111
  • Joined: 28-November 09

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 07:59 PM

SWEET xD this is doing exactly what i wanted it to!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Drawing In A JFrame Using A Button

Posted 30 November 2009 - 08:09 PM

View PostDogstopper, on 30 Nov, 2009 - 05:30 PM, said:

I always try to pbl. I mean look, my 31st project this week in just netbeans!
Attachment attachment

What's the fun then ? :D
The one who wins without peril triumphs without glory
See I let you have one more reply and probably one more "This post was Helpful"
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1