Results 1 to 7 of 7
Thread: Painting
- 09-06-2010, 03:27 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
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!
- 09-06-2010, 03:32 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
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
-
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!
-
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. :)
- 09-06-2010, 04:06 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 31
- Rep Power
- 0
OK, thanks, didn't know about the code tags. So, here is all relevant code:
Main Class:
the problematic 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 } }
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 greyJava 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); } }
-
How does this compile:
What is the "l" within draw(...) and why not draw box?Java Code:g2d.draw(l);
-
More importantly, here:
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 lineJava 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 }
does nothing, so get rid of that line, and I've no idea what a "frame" object is. Does it extend a JFrame?Java Code:Main m = new Main();
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 05:30 AM.
Similar Threads
-
Need help with synchronous painting
By JavaRulez in forum Advanced JavaReplies: 7Last Post: 05-09-2010, 08:34 AM -
Images don't painting
By daniel.henrique in forum New To JavaReplies: 2Last Post: 02-12-2010, 11:48 AM -
Objects painting themselves?
By martypapa in forum Java 2DReplies: 19Last Post: 02-06-2010, 04:08 AM -
painting problem
By hannes in forum New To JavaReplies: 3Last Post: 01-17-2010, 11:44 AM -
JPanel not always painting everything
By ekted in forum AWT / SwingReplies: 0Last Post: 11-26-2009, 11:24 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks