Adding Graphics to Jpanel


B

BEHROUZ

Hi again,
I just wondering how I can add a Graphics object like rectangle to a
JPanel?
I have following classes and I can add the Label object into MyPanel
but I cant add drawing object (MyRect) into Jpanel.I know that I am
not constructing any thing in MyRect() constructor! so it make sence
for me that nothing will happen in MyPanel when I call for MyRect
constructor but I don't' know how to call the paintComponent with it's
parameters in MyPanel class!
Could you please let me know is it possible to add a drawing into a
panel?
so what's wrong with this code?

# package MyPack;
# import java.awt.Color;
# import java.awt.Graphics;
# import javax.swing.JPanel;
#
# public class MyRect extends JPanel {
#
# public void paintComponent(Graphics g) {
# g.setColor (Color.BLUE);
# g.drawRect (100, 100, 200, 200);
# =======================
# package MyPack;
# import java.awt.Color;
# import javax.swing.JPanel;
#
# public class MyPanel extends JPanel {
#
# MyPanel() {
# setBackground(Color.green);
# // Add Drawing to Jpanel
# MyRect mrect = new MyRect();
# this.add(mrect);
# // Add Component to Jpanel
# MyLabel mlabel = new MyLabel();
# this.add(mlabel);
# }
# }
# =======================
# package MyPack;
#
# import java.awt.Color;
# import java.awt.Font;
# import javax.swing.JLabel;
#
# public class MyLabel extends JLabel{
# public MyLabel() {
# super("Hello, World!");
# setFont(new Font(null, Font.BOLD, 40));
# setForeground(Color.red);
# }
# }
# =======================
# package mypaneltest;
# import javax.swing.JFrame;
#
# public class MyFrame extends JFrame {
# public MyFrame(){
# super("Test");
# setSize(300,200);
# setLocationRelativeTo(null);
# MyPanel pane = new MyPanel();
# add(pane);
# }
# }
# ======================
# package MyPack;
#
# public class Main {
# public static void main(String[] args) {
# new MyFrame().setVisible(true);
# }
# }
 
Ad

Advertisements

K

Knute Johnson

Hi again,
I just wondering how I can add a Graphics object like rectangle to a
JPanel?
I have following classes and I can add the Label object into MyPanel
but I cant add drawing object (MyRect) into Jpanel.I know that I am
not constructing any thing in MyRect() constructor! so it make sence
for me that nothing will happen in MyPanel when I call for MyRect
constructor but I don't' know how to call the paintComponent with it's
parameters in MyPanel class!
Could you please let me know is it possible to add a drawing into a
panel?
so what's wrong with this code?

# package MyPack;
# import java.awt.Color;
# import java.awt.Graphics;
# import javax.swing.JPanel;
#
# public class MyRect extends JPanel {
#
# public void paintComponent(Graphics g) {
# g.setColor (Color.BLUE);
# g.drawRect (100, 100, 200, 200);
# =======================
# package MyPack;
# import java.awt.Color;
# import javax.swing.JPanel;
#
# public class MyPanel extends JPanel {
#
# MyPanel() {
# setBackground(Color.green);
# // Add Drawing to Jpanel
# MyRect mrect = new MyRect();
# this.add(mrect);
# // Add Component to Jpanel
# MyLabel mlabel = new MyLabel();
# this.add(mlabel);
# }
# }
# =======================
# package MyPack;
#
# import java.awt.Color;
# import java.awt.Font;
# import javax.swing.JLabel;
#
# public class MyLabel extends JLabel{
# public MyLabel() {
# super("Hello, World!");
# setFont(new Font(null, Font.BOLD, 40));
# setForeground(Color.red);
# }
# }
# =======================
# package mypaneltest;
# import javax.swing.JFrame;
#
# public class MyFrame extends JFrame {
# public MyFrame(){
# super("Test");
# setSize(300,200);
# setLocationRelativeTo(null);
# MyPanel pane = new MyPanel();
# add(pane);
# }
# }
# ======================
# package MyPack;
#
# public class Main {
# public static void main(String[] args) {
# new MyFrame().setVisible(true);
# }
# }

Other than it won't compile with all those # symbols in there. You will
get better results from the newsgroup if you post and SSCCE.

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

public class test8 extends JPanel {
public test8() {
setPreferredSize(new Dimension(400,300));
}

public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.drawRect(40,40,getWidth()-80,getHeight()-80);
g.setColor(Color.RED);
g.drawOval(getWidth()/2-40,getHeight()/2-40,80,80);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test8 t8 = new test8();
f.add(t8);
f.pack();
f.setVisible(true);
}
});
}
}
 
Ad

Advertisements

L

Lew

BEHROUZ said:
.. what's wrong with this code?

# package MyPack;

'#' is not a legitimate symbol for Java source like this.

Package names by convention are spelled in all lower-case letters.
# import java.awt.Color;
# import java.awt.Graphics;
# import javax.swing.JPanel;
#
# public class MyRect extends JPanel {
#
# public void paintComponent(Graphics g) {
# g.setColor (Color.BLUE);
# g.drawRect (100, 100, 200, 200);

The class 'MyRect' will not compile as presented here. It lacks at least two
closing braces.
# =======================
# package MyPack;
# import java.awt.Color;
# import javax.swing.JPanel;
#
# public class MyPanel extends JPanel {
#
# MyPanel() {
# setBackground(Color.green);
# // Add Drawing to Jpanel
# MyRect mrect = new MyRect();
# this.add(mrect);
# // Add Component to Jpanel
# MyLabel mlabel = new MyLabel();
# this.add(mlabel);
# }
# }
# =======================
# package MyPack;
#
# import java.awt.Color;
# import java.awt.Font;
# import javax.swing.JLabel;
#
# public class MyLabel extends JLabel{

Your indentation is inconsistent.
# public MyLabel() {
# super("Hello, World!");
# setFont(new Font(null, Font.BOLD, 40));
# setForeground(Color.red);
# }
# }
# =======================
# package mypaneltest;
# import javax.swing.JFrame;
#
# public class MyFrame extends JFrame {

It's not too bad in this context, but gurus like Joshua Bloch suggest
preferring composition to inheritance. That is, your main class would simply
instantiate a local or instance-member 'JFrame' rather than inheriting from
the class.
# public MyFrame(){
^
Indent this line.
# super("Test");
# setSize(300,200);
# setLocationRelativeTo(null);
# MyPanel pane = new MyPanel();
# add(pane);
# }
^
Indent this line.
# }
# ======================
# package MyPack;
#
# public class Main {
# public static void main(String[] args) {
# new MyFrame().setVisible(true);
# }
# }

You forgot to 'pack()'. You have GUI actions that are not happening on the
Event Dispatch Thread (EDT). Both those mistakes will cost you weird bugs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top