Results 1 to 7 of 7

Thread: Painting

  1. #1
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default Painting

    I have successfully made a GUI, using JFrame components. But, I cannot get anything from the Paint or PaintComponent methods to show up. The following code is from a class whose parent is JPanel; but the program is not an applet, it's a desktop application, but I made the class responsible for drawing such objects a child of JPanel since doing so prevents errors in the following method:
    //example start
    public void paintComponent(Graphics g)
    {super.paintComponent(g);
    Graphics2D g2d= (Graphics2D)g;

    g2d.setColor(Color.orange);
    Font f = new Font("Comic Sans", Font.BOLD, 13);
    g2d.setFont(f);
    g2d.drawString("hi", 100, 100);

    Rectangle2D.Float box = new Rectangle2D.Float(1F,1F, 100F,100F);

    g2d.draw(l);
    }
    //example end

    What am I doing wrong? Nothing will show up... any ideas appreciated!

  2. #2
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    PS, i get an a semi-error(orange underlining) saying I need to apply override annotation, which has no effect whatsoever. I've also tried using paint() instead to no avail

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It could be many things such as the JPanel being too small, not being added to the JFrame, or being covered by something else. Bottom line is it's hard to tell what's wrong without seeing compilable and runnable code.

    If you do post more code, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

    Much luck!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Interestingly (to me at least), I made a small program similar to yours, and it took a curiously long time to load:

    Java Code:
    import java.awt.*;
    import java.awt.geom.Rectangle2D;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class XaelPanel extends JPanel {
       private Rectangle2D box = new Rectangle2D.Float(1f, 1f, 100f, 100f);
       private static final Font MY_FONT = new Font("Comic Sans", Font.BOLD, 20);
       
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2d = (Graphics2D) g;
    
          g2d.setFont(MY_FONT);
          
          g2d.setColor(Color.blue);
          g2d.drawString("hi there!", 110, 100);
    
          g2d.setColor(Color.orange);
          g2d.draw(box); 
       }
       
       private static void createAndShowUI() {
          XaelPanel panel = new XaelPanel();
          panel.setPreferredSize(new Dimension(600, 400));
          
          JFrame frame = new JFrame("XaelTest");
          frame.getContentPane().add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

    I think this may be due to the program searching for but not finding the font, and it seemed to load faster when I changed "Comic Sans" to "Comic Sans MS". Maybe it's my imagination. :)

  5. #5
    xael is offline Member
    Join Date
    Sep 2010
    Posts
    31
    Rep Power
    0

    Default

    OK, thanks, didn't know about the code tags. So, here is all relevant code:
    Main Class:
    Java Code:
    package graphix;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Main {
    
       public Main()
        {
       }
        public static void main(String[] args) {
    Main m = new Main();
            frame f  = new frame();// the GUI and swing child 
            draw d= new draw();// this is the class with the drawing problems
        }
    
    }
    the problematic class:
    Java Code:
    package graphix;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import javax.swing.*;
    // many imports are unused, I know
    
    import java.awt.GridLayout;
    import java.awt.Dimension;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseEvent;
    public class draw  extends JPanel {
     
    public void draw()
        {
        }
    @Override
    public void paintComponent(Graphics g)
    {super.paintComponent(g);
    Graphics2D g2d= (Graphics2D)g;
    
    g2d.setColor(Color.orange);
    Font f = new Font("Comic Sans", Font.BOLD, 13);
    g2d.setFont(f);
    g2d.drawString("hi", 100, 100);
    
    Rectangle2D.Float box = new Rectangle2D.Float(1F,1F, 100F,100F);
    
    g2d.draw(l); 
    }
    }
    This will compile, but nothing will be drawn; I've tried disabling GUI parts in the frame class, leaving just a drawn frame, but still it's just the blank grey

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How does this compile:
    Java Code:
    g2d.draw(l);
    What is the "l" within draw(...) and why not draw box?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    More importantly, here:
    Java Code:
        public static void main(String[] args) {
            Main m = new Main();
            frame f  = new frame();// the GUI and swing child 
            draw d= new draw();// this is the class with the drawing problems
        }
    You're creating a draw object, but you're never putting it inside of a top-level container, so it will never be seen (see my example above where I put the XaelPanel into a JFrame) Also, creating a Main object via this line
    Java Code:
    Main m = new Main();
    does nothing, so get rid of that line, and I've no idea what a "frame" object is. Does it extend a JFrame?

    I strongly urge you to study the Swing tutorials to see how it's done. Much luck!
    Last edited by Fubarable; 09-06-2010 at 06:30 AM.

Similar Threads

  1. Need help with synchronous painting
    By JavaRulez in forum Advanced Java
    Replies: 7
    Last Post: 05-09-2010, 09:34 AM
  2. Images don't painting
    By daniel.henrique in forum New To Java
    Replies: 2
    Last Post: 02-12-2010, 12:48 PM
  3. Objects painting themselves?
    By martypapa in forum Java 2D
    Replies: 19
    Last Post: 02-06-2010, 05:08 AM
  4. painting problem
    By hannes in forum New To Java
    Replies: 3
    Last Post: 01-17-2010, 12:44 PM
  5. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 12:24 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
  •