Results 1 to 8 of 8
  1. #1
    pepsi is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Drawing Shapes on to a Jpanel

    I have made a program that draws a shape depending on the button pressed, but my problem is that it draws the shapes all over the Jframe.. is there any way I can contain the shapes within a Jpanel or something. Sorry I am completely new to this and I am trying hard to understand, here is my code if it helps x.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Coursework extends JFrame implements ActionListener
    {
    
    		JButton buttonOvalDraw, buttonRectDraw, buttonBothDraw,calArea, randomColor, clearAll;
    		JLabel Header, xEnter, yEnter, hEnter, wEnter, areaLabel;
    		JPanel headerContainer, xEnterContainer;
    		JTextField textxEnter,textyEnter,texthEnter,textwEnter, areaText;
    		int x, y,h,w;
    		boolean drawOval, drawRect;
    
    
    
    	public Coursework()
    	{
    		setLayout(new FlowLayout());
    		setSize(720,600);
    		setTitle("My PROGRAM");
    
    		//headerContainer = new JPanel();
    
    
    		Header = new JLabel (" Drawing Shapes by ***** **** ");
    		Header.setFont(new Font("Monospaced", Font.BOLD, 35));
    		Header.setForeground(Color.black);
    		add(Header);
    		//add(headerContainer);
    		//headerContainer.add(Header);
    
    		xEnter = new JLabel("Enter X");
    		xEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    		xEnter.setForeground(Color.blue);
    		add(xEnter);
    
    		textxEnter = new JTextField("0", 4);
    		add(textxEnter);
    
    
    		yEnter = new JLabel("Enter Y");
    		yEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    		yEnter.setForeground(Color.blue);
    		add(yEnter);
    
    
    		textyEnter = new JTextField("0", 4);
    		add(textyEnter);
    
    		hEnter = new JLabel("Enter Height");
    		hEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    		hEnter.setForeground(Color.blue);
    		add(hEnter);
    
    
    		texthEnter = new JTextField("0", 4);
    		add(texthEnter);
    
    		wEnter = new JLabel("Enter Width");
    		wEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    		wEnter.setForeground(Color.blue);
    		add(wEnter);
    
    		textwEnter = new JTextField("0", 4);
    		add(textwEnter);
    
    		areaLabel = new JLabel("The Area is");
    		areaLabel.setFont(new Font("Serif",Font.BOLD, 15));
    		areaLabel.setForeground(Color.red);
    		add(areaLabel);
    
    		areaText = new JTextField(5);
    		add(areaText);
    
    		buttonOvalDraw = new JButton("Draw Oval");
    		buttonOvalDraw.setVisible(true);
    		buttonOvalDraw.addActionListener(this);
    		add(buttonOvalDraw);
    
    		buttonRectDraw = new JButton("Draw Rectangle");
    		buttonRectDraw.setVisible(true);
    		buttonRectDraw.addActionListener(this);
    		add(buttonRectDraw);
    
    		buttonBothDraw = new JButton("Draw Both");
    		buttonBothDraw.setVisible(true);
    		buttonBothDraw.addActionListener(this);
    		add(buttonBothDraw);
    
    		calArea = new JButton("Calculate Area");
    		calArea.setVisible(true);
    		calArea.addActionListener(this);
    		add(calArea);
    
    		randomColor = new JButton("Random Color");
    		randomColor.setVisible(true);
    		randomColor.addActionListener(this);
    		add(randomColor);
    
    		clearAll = new JButton("Clear All");
    		clearAll.setVisible(true);
    		clearAll.addActionListener(this);
    		add(clearAll);
    
    
    
    		setVisible(true);
    	}
    
    
    
    	public void paint(Graphics graf)
    	{
    		super.paint(graf);
    		if (drawOval){
    
    
    
    				graf.drawOval(x,y,h,w);
    				}
    
    				if(drawRect){
    
    
    				graf.drawRect(x,y,h,w);
    		}
    
    	}
    
    	public void actionPerformed( ActionEvent e )
    	{
    
    		x = Integer.parseInt(textxEnter.getText());
    		y = Integer.parseInt(textyEnter.getText());
    		h = Integer.parseInt(texthEnter.getText());
    		w = Integer.parseInt(textwEnter.getText());
    
    		if(e.getSource() == buttonOvalDraw){
    
    				drawRect=false;
    				drawOval = true;
    				repaint();
    
    }
    
    		if(e.getSource() == buttonRectDraw){
    
    				drawRect=true;
    				drawOval = false;
    				repaint();
    
    }
    
    		if(e.getSource() == buttonBothDraw){
    
    				drawRect=true;
    				drawOval = true;
    				repaint();
    
    }
    
    		if(e.getSource() == calArea){
    
    
    
    }
    
    		if(e.getSource() == randomColor){
    
    
    }
    
    		if(e.getSource() == clearAll){
    
    
    
    }
    
    	}
    
    	public static void main(String args[])
    	{
    		new Coursework();
    
    	}
    }
    Last edited by Norm; 03-08-2012 at 08:12 PM. Reason: added code tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Drawing Shapes on to a Jpanel

    Sure, instead of extending JFrame, simply extend JPanel and add that JPanel to a JFrame. Also, you should override the paintComponent() method, not the paint() method. Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    pepsi is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Drawing Shapes on to a Jpanel

    I created a Jpanel Class, extended it and changed the paint method.. but now I cant see the shapes at all when i try and draw.. what am i doing wrong??
    here's my new code with slight changes:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Coursework extends JFrame implements ActionListener
    {

    JButton buttonOvalDraw, buttonRectDraw, buttonBothDraw,calArea, randomColor, clearAll;
    JLabel Header, xEnter, yEnter, hEnter, wEnter, areaLabel;
    JPanel headerContainer, xEnterContainer;
    JTextField textxEnter,textyEnter,texthEnter,textwEnter, areaText;
    int x, y,h,w;
    boolean drawOval, drawRect;



    public Coursework()
    {
    setLayout(new FlowLayout());
    setSize(720,600);
    setTitle("My PROGRAM");

    //headerContainer = new JPanel();


    Header = new JLabel (" Drawing Shapes by ****** **** ");
    Header.setFont(new Font("Monospaced", Font.BOLD, 35));
    Header.setForeground(Color.black);
    add(Header);
    //add(headerContainer);
    //headerContainer.add(Header);

    xEnter = new JLabel("Enter X");
    xEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    xEnter.setForeground(Color.blue);
    add(xEnter);

    textxEnter = new JTextField("0", 4);
    add(textxEnter);


    yEnter = new JLabel("Enter Y");
    yEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    yEnter.setForeground(Color.blue);
    add(yEnter);


    textyEnter = new JTextField("0", 4);
    add(textyEnter);

    hEnter = new JLabel("Enter Height");
    hEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    hEnter.setForeground(Color.blue);
    add(hEnter);


    texthEnter = new JTextField("0", 4);
    add(texthEnter);

    wEnter = new JLabel("Enter Width");
    wEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    wEnter.setForeground(Color.blue);
    add(wEnter);

    textwEnter = new JTextField("0", 4);
    add(textwEnter);

    areaLabel = new JLabel("The Area is");
    areaLabel.setFont(new Font("Serif",Font.BOLD, 15));
    areaLabel.setForeground(Color.red);
    add(areaLabel);

    areaText = new JTextField(5);
    add(areaText);

    buttonOvalDraw = new JButton("Draw Oval");
    buttonOvalDraw.setVisible(true);
    buttonOvalDraw.addActionListener(this);
    add(buttonOvalDraw);

    buttonRectDraw = new JButton("Draw Rectangle");
    buttonRectDraw.setVisible(true);
    buttonRectDraw.addActionListener(this);
    add(buttonRectDraw);

    buttonBothDraw = new JButton("Draw Both");
    buttonBothDraw.setVisible(true);
    buttonBothDraw.addActionListener(this);
    add(buttonBothDraw);

    calArea = new JButton("Calculate Area");
    calArea.setVisible(true);
    calArea.addActionListener(this);
    add(calArea);

    randomColor = new JButton("Random Color");
    randomColor.setVisible(true);
    randomColor.addActionListener(this);
    add(randomColor);

    clearAll = new JButton("Clear All");
    clearAll.setVisible(true);
    clearAll.addActionListener(this);
    add(clearAll);



    setVisible(true);
    }



    class MyDrawPanel extends JPanel{

    public void paintComponent(Graphics graf)
    {
    super.paintComponent(graf);
    if (drawOval){



    graf.drawOval(x,y,h,w);
    }

    if(drawRect){


    graf.drawRect(x,y,h,w);
    }

    }


    }

    public void actionPerformed( ActionEvent e )
    {

    x = Integer.parseInt(textxEnter.getText());
    y = Integer.parseInt(textyEnter.getText());
    h = Integer.parseInt(texthEnter.getText());
    w = Integer.parseInt(textwEnter.getText());

    if(e.getSource() == buttonOvalDraw){

    drawRect=false;
    drawOval = true;
    repaint();

    }

    if(e.getSource() == buttonRectDraw){

    drawRect=true;
    drawOval = false;
    repaint();

    }

    if(e.getSource() == buttonBothDraw){

    drawRect=true;
    drawOval = true;
    repaint();

    }

    if(e.getSource() == calArea){



    }

    if(e.getSource() == randomColor){


    }

    if(e.getSource() == clearAll){



    }

    }

    public static void main(String args[])
    {
    new Coursework();

    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Drawing Shapes on to a Jpanel

    Look around the FAQs and find out how to use code tags. A moderator won't add them for you every time.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pepsi is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Drawing Shapes on to a Jpanel

    Ok sorry about that, here is the code properly..

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Coursework extends JFrame implements ActionListener
    {
    
    JButton buttonOvalDraw, buttonRectDraw, buttonBothDraw,calArea, randomColor, clearAll;
    JLabel Header, xEnter, yEnter, hEnter, wEnter, areaLabel;
    JPanel headerContainer, xEnterContainer;
    JTextField textxEnter,textyEnter,texthEnter,textwEnter, areaText;
    int x, y,h,w;
    boolean drawOval, drawRect;
    
    
    
    public Coursework()
    {
    setLayout(new FlowLayout());
    setSize(720,600);
    setTitle("My PROGRAM");
    
    //headerContainer = new JPanel();
    
    
    Header = new JLabel (" Drawing Shapes by ****** **** ");
    Header.setFont(new Font("Monospaced", Font.BOLD, 35));
    Header.setForeground(Color.black);
    add(Header);
    //add(headerContainer);
    //headerContainer.add(Header);
    
    xEnter = new JLabel("Enter X");
    xEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    xEnter.setForeground(Color.blue);
    add(xEnter);
    
    textxEnter = new JTextField("0", 4);
    add(textxEnter);
    
    
    yEnter = new JLabel("Enter Y");
    yEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    yEnter.setForeground(Color.blue);
    add(yEnter);
    
    
    textyEnter = new JTextField("0", 4);
    add(textyEnter);
    
    hEnter = new JLabel("Enter Height");
    hEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    hEnter.setForeground(Color.blue);
    add(hEnter);
    
    
    texthEnter = new JTextField("0", 4);
    add(texthEnter);
    
    wEnter = new JLabel("Enter Width");
    wEnter.setFont(new Font("Serif",Font.ITALIC, 17));
    wEnter.setForeground(Color.blue);
    add(wEnter);
    
    textwEnter = new JTextField("0", 4);
    add(textwEnter);
    
    areaLabel = new JLabel("The Area is");
    areaLabel.setFont(new Font("Serif",Font.BOLD, 15));
    areaLabel.setForeground(Color.red);
    add(areaLabel);
    
    areaText = new JTextField(5);
    add(areaText);
    
    buttonOvalDraw = new JButton("Draw Oval");
    buttonOvalDraw.setVisible(true);
    buttonOvalDraw.addActionListener(this);
    add(buttonOvalDraw);
    
    buttonRectDraw = new JButton("Draw Rectangle");
    buttonRectDraw.setVisible(true);
    buttonRectDraw.addActionListener(this);
    add(buttonRectDraw);
    
    buttonBothDraw = new JButton("Draw Both");
    buttonBothDraw.setVisible(true);
    buttonBothDraw.addActionListener(this);
    add(buttonBothDraw);
    
    calArea = new JButton("Calculate Area");
    calArea.setVisible(true);
    calArea.addActionListener(this);
    add(calArea);
    
    randomColor = new JButton("Random Color");
    randomColor.setVisible(true);
    randomColor.addActionListener(this);
    add(randomColor);
    
    clearAll = new JButton("Clear All");
    clearAll.setVisible(true);
    clearAll.addActionListener(this);
    add(clearAll);
    
    
    
    setVisible(true);
    }
    
    
    
    class MyDrawPanel extends JPanel{
    
    public void paintComponent(Graphics graf)
    {
    super.paintComponent(graf);
    if (drawOval){
    
    
    
    graf.drawOval(x,y,h,w);
    }
    
    if(drawRect){
    
    
    graf.drawRect(x,y,h,w);
    }
    
    }
    
    
    }
    
    public void actionPerformed( ActionEvent e )
    {
    
    x = Integer.parseInt(textxEnter.getText());
    y = Integer.parseInt(textyEnter.getText());
    h = Integer.parseInt(texthEnter.getText());
    w = Integer.parseInt(textwEnter.getText());
    
    if(e.getSource() == buttonOvalDraw){
    
    drawRect=false;
    drawOval = true;
    repaint();
    
    }
    
    if(e.getSource() == buttonRectDraw){
    
    drawRect=true;
    drawOval = false;
    repaint();
    
    }
    
    if(e.getSource() == buttonBothDraw){
    
    drawRect=true;
    drawOval = true;
    repaint();
    
    }
    
    if(e.getSource() == calArea){
    
    
    
    }
    
    if(e.getSource() == randomColor){
    
    
    }
    
    if(e.getSource() == clearAll){
    
    
    
    }
    
    }
    
    public static void main(String args[])
    {
    new Coursework();
    
    }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Drawing Shapes on to a Jpanel

    Do you really not use indentation?

    Add some print statements to figure out what's going on- is your ActionListener working? Is your paintComponent() method being called? Can you see your JPanel?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    pepsi is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Drawing Shapes on to a Jpanel

    Quote Originally Posted by KevinWorkman View Post
    Do you really not use indentation?

    Add some print statements to figure out what's going on- is your ActionListener working? Is your paintComponent() method being called? Can you see your JPanel?
    I'm completely new to all this, so take it easy on me. I thought JPanels were invisble, how can I add a border or background to it so I can see it on the frame, and what do you mean by paintComponent method being called? I genuinely don't know how these things work..

    p.s this is my first Java assignment

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: Drawing Shapes on to a Jpanel

    Quote Originally Posted by pepsi View Post
    I'm completely new to all this, so take it easy on me. I thought JPanels were invisble, how can I add a border or background to it so I can see it on the frame, and what do you mean by paintComponent method being called? I genuinely don't know how these things work..

    p.s this is my first Java assignment
    I'm not sure how any of what I said was not taking it easy on you. Did you read through the tutorial I posted above? Start with that, and add to it in small steps. That will be a lot easier to debug than 200 lines of code.

    And you can check out the API for useful functions that might make the JPanel more visible: Java Platform SE 6
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Having issues drawing Shapes into Jpanel
    By Greedychris in forum AWT / Swing
    Replies: 2
    Last Post: 03-06-2012, 05:32 AM
  2. Drawing different shapes with stars.
    By Amazineous in forum New To Java
    Replies: 23
    Last Post: 11-14-2011, 03:55 PM
  3. Drawing simple shapes using loops
    By Quinn in forum New To Java
    Replies: 2
    Last Post: 05-29-2011, 09:05 AM
  4. Drawing shapes with for loops using characters
    By Strawlion in forum New To Java
    Replies: 3
    Last Post: 02-16-2011, 08:32 PM
  5. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 06:35 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
  •