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.
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();
}
}
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)
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();
}
}
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
Re: Drawing Shapes on to a Jpanel
Ok sorry about that, here is the code properly..
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();
}
}
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?
Re: Drawing Shapes on to a Jpanel
Quote:
Originally Posted by
KevinWorkman
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
Re: Drawing Shapes on to a Jpanel
Quote:
Originally Posted by
pepsi
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