Results 1 to 7 of 7
Thread: PaintComponent not working
- 11-10-2011, 04:24 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
PaintComponent not working
First off I would like to say hello to members of the forum as I am new here. I am having a problem with the paintComponent method. I am trying to render an image to the screen when a button is clicked. When I click the button the image is not being displayed. I thought maybe it was a problem with reading in the image however when I try to simple drawString it also does not work. This has been driving me nuts for hours now. I am new to Swing so maybe Im just missing something elementary. I think maybe it has something to do with the fact that I am using border Layout. Here is my code. Any advice would be appreciated.
Java Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.util.Vector; import javax.swing.*; public class LayoutPanel extends JPanel implements ActionListener,MouseListener,MouseMotionListener { Vector<SingleRoom> beds=new Vector<SingleRoom>(); private JButton addSingleBedButton; private JButton addDoubleBedButton; private JButton addSpecialCareButton; public LayoutPanel(){ JButton newButton= new JButton("New"); // Creates Buttons JButton OpenButton= new JButton("Open"); JButton closeButton= new JButton("Close"); JButton saveButton= new JButton("Save"); JButton saveAsButton= new JButton("Save As"); JButton saveAsTemplButton= new JButton("Save As Template"); JButton exitButton= new JButton("Exit"); JPanel content= new JPanel(); content.setLayout(new FlowLayout()); content.add(exitButton); //Adds Buttons content.add(OpenButton); content.add(closeButton); content.add(saveButton); content.add(saveAsButton); content.add(saveAsTemplButton); content.add(newButton); this.setLayout(new BorderLayout()); this.add(content,BorderLayout.NORTH); JPanel drawPanel=new JPanel(); drawPanel.setBackground(Color.WHITE); this.add(drawPanel,BorderLayout.CENTER); addSingleBedButton= new JButton("Add One Bed Room"); addDoubleBedButton= new JButton("Add Double Bed Room"); addSpecialCareButton= new JButton("Add Special Care Room"); addSingleBedButton.addActionListener(this); addDoubleBedButton.addActionListener(this); addSpecialCareButton.addActionListener(this); JPanel buttonPanel=new JPanel(); buttonPanel.setLayout( new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); buttonPanel.add(addSpecialCareButton); buttonPanel.add(addDoubleBedButton); buttonPanel.add(addSingleBedButton); this.add(buttonPanel, BorderLayout.WEST); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); for(int i=0;i<beds.size();i++){ SingleRoom bed=(SingleRoom)beds.get(i); int w=getHeight(); int h=getWidth(); g.drawImage(bed.singleImage,w/2,h/2,null); g.drawString("yoo", w/2, h/2); } g.setColor(Color.BLACK); g.drawString("yooiiiiiiiiiiiiiiiiij", 200, 200); System.out.println("clicked"); } public void actionPerformed(ActionEvent e){ if(e.getSource()==addSingleBedButton){ SingleRoom singleRoom=new SingleRoom(200,200); beds.add(singleRoom); repaint(); } if(e.getSource()==addDoubleBedButton){ //add doubleBed } if(e.getSource()==addSpecialCareButton){ //add DoubleBed } } @Override public void mouseDragged(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }
- 11-10-2011, 05:08 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: PaintComponent not working
I only briefly scanned your code. It looks like you are adding panels to the class which are drawn on top of it. Your drawing draws on the class itself, not the panels inside the class.
- 11-10-2011, 05:50 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: PaintComponent not working
When I did setOpaque it worked. I think you are correct though. I dont think im drawing correctly on the panels.
- 11-10-2011, 06:14 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: PaintComponent not working
I may have been unclear. The problem is where you are drawing. The paintComponent method of your class draws directly to itself. You add generic panels onto your panel however, and these lay on top of your class. This covers up any drawing you are doing. A better approach is to write the class to draw itself separately and then add it into a JFrame with other panels.
- 11-10-2011, 06:25 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: PaintComponent not working
I see what you are saying. The way Im doing it is kind of cheat by calling setOpaque(false). Its more of a design problem with the class.
- 11-10-2011, 07:42 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: PaintComponent not working
So now Im more messed up than before. I created a completely seperate class for the panel that I want to draw on called Canvas Panel. Now when I run it nothing shows up at all. Not even the buttons. When I scroll over the buttons then they appear. This is really messed up.
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Point2D; import java.util.Iterator; import java.util.Vector; public class BedManagement { static Vector<SingleRoom> beds; public static void main(String [] args) { ManagementFrame frame= new ManagementFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ManagementFrame extends JFrame{ public static Vector<SingleRoom> beds; public ManagementFrame(){ super("ImageFrame"); setExtendedState(MAXIMIZED_BOTH); setDefaultCloseOperation(EXIT_ON_CLOSE); String options[]={"Layout Manager","Bed Manager"}; int num=JOptionPane.QUESTION_MESSAGE; int code= JOptionPane.showOptionDialog(this, "Choose an Option", "option", 0,num, null, options, null); if(code==0){ //LayoutPanel l= new LayoutPanel(); //this.add(l,BorderLayout.CENTER); LayoutP p=new LayoutP(); this.add(p); } if(code==1){ } } class LayoutP extends JPanel implements ActionListener{ JButton addSpecialCareButton; JButton addDoubleBedButton; JButton addSingleBedButton; //Vector<SingleRoom> beds=new Vector<SingleRoom>(); public LayoutP(){ JButton newButton= new JButton("New"); // Creates Buttons JButton OpenButton= new JButton("Open"); JButton closeButton= new JButton("Close"); JButton saveButton= new JButton("Save"); JButton saveAsButton= new JButton("Save As"); JButton saveAsTemplButton= new JButton("Save As Template"); JButton exitButton= new JButton("Exit"); JPanel content= new JPanel(); content.setLayout(new FlowLayout()); content.add(exitButton); //Adds Buttons content.add(OpenButton); content.add(closeButton); content.add(saveButton); content.add(saveAsButton); content.add(saveAsTemplButton); content.add(newButton); this.setLayout(new BorderLayout()); this.add(content,BorderLayout.NORTH); addSingleBedButton= new JButton("Add One Bed Room"); addDoubleBedButton= new JButton("Add Double Bed Room"); addSpecialCareButton= new JButton("Add Special Care Room"); addSingleBedButton.addActionListener(this); addDoubleBedButton.addActionListener(this); addSpecialCareButton.addActionListener(this); JPanel buttonPanel=new JPanel(); buttonPanel.setLayout( new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); buttonPanel.add(addSpecialCareButton); buttonPanel.add(addDoubleBedButton); buttonPanel.add(addSingleBedButton); this.add(buttonPanel, BorderLayout.WEST); CanvasPanel drawPanel=new CanvasPanel(beds); this.add(drawPanel,BorderLayout.CENTER); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==addSingleBedButton){ SingleRoom singleRoom=new SingleRoom(200,200); beds.add(singleRoom); repaint(); } if(e.getSource()==addDoubleBedButton){ //add doubleBed } if(e.getSource()==addSpecialCareButton){ //add DoubleBed } } } //class DrawPanel extends JPanel{ // DrawPanel(){ // JPanel canvas=new JPanel(); // this.add(canvas); // } // // public void paintComponent(Graphics g) { // super.paintComponent(g); // this.setBackground(Color.WHITE); // Iterator<SingleRoom> it1 = // beds.iterator(); // while(it1.hasNext()){ // SingleRoom sr=it1.next(); // g.drawImage(sr.singleImage, 200, 200, null); // // } //for(int i=0;i<beds.size();i++){ //SingleRoom bed=(SingleRoom)beds.get(i); //int w=getHeight(); //int h=getWidth(); //g.drawImage(bed.singleImage,w/2,h/2,null); //g.drawString("yoo", w/2, h/2); //} //g.setColor(Color.BLACK); }Java Code:import java.awt.Color; import java.awt.Graphics; import java.util.Iterator; import java.util.Vector; import javax.swing.JPanel; public class CanvasPanel extends JPanel { Vector<SingleRoom> bed; public CanvasPanel(Vector<SingleRoom> beds) { JPanel canvas=new JPanel(); this.add(canvas); bed= beds; } public void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(Color.WHITE); Iterator<SingleRoom> it1 = bed.iterator(); while(it1.hasNext()){ SingleRoom sr=it1.next(); g.drawImage(sr.singleImage, 200, 200, null); } //for(int i=0;i<beds.size();i++){ //SingleRoom bed=(SingleRoom)beds.get(i); //int w=getHeight(); //int h=getWidth(); //g.drawImage(bed.singleImage,w/2,h/2,null); //g.drawString("yoo", w/2, h/2); } //g.setColor(Color.BLACK); }
- 11-10-2011, 07:49 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 5
- Rep Power
- 0
Re: PaintComponent not working
Now nothings is working. AHHHH.
Java Code:import java.awt.Color; import java.awt.Graphics; import java.util.Iterator; import java.util.Vector; import javax.swing.JPanel; public class CanvasPanel extends JPanel { Vector<SingleRoom> bed; public CanvasPanel(Vector<SingleRoom> beds) { JPanel canvas=new JPanel(); this.add(canvas); bed= beds; } public void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(Color.WHITE); Iterator<SingleRoom> it1 = bed.iterator(); while(it1.hasNext()){ SingleRoom sr=it1.next(); g.drawImage(sr.singleImage, 200, 200, null); } //for(int i=0;i<beds.size();i++){ //SingleRoom bed=(SingleRoom)beds.get(i); //int w=getHeight(); //int h=getWidth(); //g.drawImage(bed.singleImage,w/2,h/2,null); //g.drawString("yoo", w/2, h/2); } //g.setColor(Color.BLACK); }Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Point2D; import java.util.Iterator; import java.util.Vector; public class BedManagement { static Vector<SingleRoom> beds; public static void main(String [] args) { ManagementFrame frame= new ManagementFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ManagementFrame extends JFrame{ public static Vector<SingleRoom> beds; public ManagementFrame(){ super("ImageFrame"); setExtendedState(MAXIMIZED_BOTH); setDefaultCloseOperation(EXIT_ON_CLOSE); String options[]={"Layout Manager","Bed Manager"}; int num=JOptionPane.QUESTION_MESSAGE; int code= JOptionPane.showOptionDialog(this, "Choose an Option", "option", 0,num, null, options, null); if(code==0){ //LayoutPanel l= new LayoutPanel(); //this.add(l,BorderLayout.CENTER); LayoutP p=new LayoutP(); this.add(p); } if(code==1){ } } class LayoutP extends JPanel implements ActionListener{ JButton addSpecialCareButton; JButton addDoubleBedButton; JButton addSingleBedButton; //Vector<SingleRoom> beds=new Vector<SingleRoom>(); public LayoutP(){ JButton newButton= new JButton("New"); // Creates Buttons JButton OpenButton= new JButton("Open"); JButton closeButton= new JButton("Close"); JButton saveButton= new JButton("Save"); JButton saveAsButton= new JButton("Save As"); JButton saveAsTemplButton= new JButton("Save As Template"); JButton exitButton= new JButton("Exit"); JPanel content= new JPanel(); content.setLayout(new FlowLayout()); content.add(exitButton); //Adds Buttons content.add(OpenButton); content.add(closeButton); content.add(saveButton); content.add(saveAsButton); content.add(saveAsTemplButton); content.add(newButton); this.setLayout(new BorderLayout()); this.add(content,BorderLayout.NORTH); addSingleBedButton= new JButton("Add One Bed Room"); addDoubleBedButton= new JButton("Add Double Bed Room"); addSpecialCareButton= new JButton("Add Special Care Room"); addSingleBedButton.addActionListener(this); addDoubleBedButton.addActionListener(this); addSpecialCareButton.addActionListener(this); JPanel buttonPanel=new JPanel(); buttonPanel.setLayout( new BoxLayout(buttonPanel,BoxLayout.Y_AXIS)); buttonPanel.add(addSpecialCareButton); buttonPanel.add(addDoubleBedButton); buttonPanel.add(addSingleBedButton); this.add(buttonPanel, BorderLayout.WEST); CanvasPanel drawPanel=new CanvasPanel(beds); this.add(drawPanel,BorderLayout.CENTER); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==addSingleBedButton){ SingleRoom singleRoom=new SingleRoom(200,200); beds.add(singleRoom); repaint(); } if(e.getSource()==addDoubleBedButton){ //add doubleBed } if(e.getSource()==addSpecialCareButton){ //add DoubleBed } } } //class DrawPanel extends JPanel{ // DrawPanel(){ // JPanel canvas=new JPanel(); // this.add(canvas); // } // // public void paintComponent(Graphics g) { // super.paintComponent(g); // this.setBackground(Color.WHITE); // Iterator<SingleRoom> it1 = // beds.iterator(); // while(it1.hasNext()){ // SingleRoom sr=it1.next(); // g.drawImage(sr.singleImage, 200, 200, null); // // } //for(int i=0;i<beds.size();i++){ //SingleRoom bed=(SingleRoom)beds.get(i); //int w=getHeight(); //int h=getWidth(); //g.drawImage(bed.singleImage,w/2,h/2,null); //g.drawString("yoo", w/2, h/2); //} //g.setColor(Color.BLACK); }
Similar Threads
-
Help with paintComponent!
By joeyea in forum Java 2DReplies: 6Last Post: 12-27-2010, 01:59 PM -
JPanel PaintComponent
By capiono in forum AWT / SwingReplies: 5Last Post: 10-31-2010, 03:36 AM -
paintComponent vs paintComponents
By alacn in forum New To JavaReplies: 5Last Post: 07-26-2010, 03:33 AM -
paintComponent is not working
By spazattack in forum New To JavaReplies: 1Last Post: 12-18-2009, 03:48 AM -
Working around paintcomponent
By sahhhm in forum New To JavaReplies: 2Last Post: 05-16-2008, 02:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks