Results 1 to 8 of 8
Thread: Drawing Shapes on to a Jpanel
- 03-08-2012, 06:16 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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
- 03-08-2012, 06:18 PM #2
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!
- 03-09-2012, 12:49 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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();
}
}
- 03-09-2012, 09:55 AM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-09-2012, 01:45 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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(); } }
- 03-09-2012, 02:37 PM #6
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!
- 03-09-2012, 02:46 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
Re: Drawing Shapes on to a 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
- 03-09-2012, 05:39 PM #8
Re: Drawing Shapes on to a Jpanel
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 6How 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
-
Having issues drawing Shapes into Jpanel
By Greedychris in forum AWT / SwingReplies: 2Last Post: 03-06-2012, 05:32 AM -
Drawing different shapes with stars.
By Amazineous in forum New To JavaReplies: 23Last Post: 11-14-2011, 03:55 PM -
Drawing simple shapes using loops
By Quinn in forum New To JavaReplies: 2Last Post: 05-29-2011, 09:05 AM -
Drawing shapes with for loops using characters
By Strawlion in forum New To JavaReplies: 3Last Post: 02-16-2011, 08:32 PM -
drawing shapes in java help
By alphajoseph in forum Java 2DReplies: 2Last Post: 09-29-2009, 06:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks