944,033 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 609
  • Java RSS
Oct 28th, 2009
-1

Graphics Programming

Expand Post »
Hi guys,
I need to display "Java Mug" (in red) inside the mug I created in the following code. I spent hours working on it but couldn't get anywhere. Can you guys help me please?
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.geom.*;
  4.  
  5. public class DrawJavaMugTest {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. EventQueue.invokeLater(new Runnable(){
  10.  
  11. public void run(){
  12. DrawFrame frame = new DrawFrame();
  13. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14. frame.setVisible(true);
  15. }
  16. });
  17. }
  18. }
  19.  
  20. class DrawFrame extends JFrame{
  21.  
  22. public DrawFrame(){
  23. setTitle("babylonlion");
  24. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  25.  
  26. //DrawJavaMugPanel panel = new DrawJavaMugPanel();
  27. //add(panel);
  28.  
  29. DrawJavaMugComponent component = new DrawJavaMugComponent();
  30. add(component);
  31. }
  32. public static final int DEFAULT_WIDTH = 400;
  33. public static final int DEFAULT_HEIGHT = 400;
  34. }
  35.  
  36. class DrawJavaMugComponent extends JComponent{
  37.  
  38. public void paintComponent(Graphics g){
  39.  
  40. Graphics2D g2 = (Graphics2D) g;
  41.  
  42. g2.draw(new Line2D.Double(100, 100, 100, 300));//Vertical line 1
  43. g2.draw(new Line2D.Double(276, 100, 276, 300));//Vertical line 2
  44. g2.draw(new Line2D.Double(276, 300, 100, 300));//Horizontal line
  45.  
  46. Ellipse2D ellipse1 = new Ellipse2D.Double();//the opening of the mug
  47. ellipse1.setFrame(276, 125, 40, 150);
  48. g2.draw(ellipse1);
  49.  
  50. Ellipse2D ellipse2 = new Ellipse2D.Double();//the handle of the mug
  51. ellipse2.setFrame(99.5, 91, 177, 15);//(leftX,topY,width, height)
  52. g2.draw(ellipse2);
  53.  
  54. }
  55. }
Last edited by babylonlion; Oct 28th, 2009 at 3:04 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009
Oct 28th, 2009
1
Re: Graphics Programming
Just use the drawString method to draw your words. g.drawString(parameters go here) http://java.sun.com/j2se/1.4.2/docs/.../Graphics.html

You can set the Graphics contexts' font and color and stuff before you draw the string, and it will draw in that font and color. At least so says the API if you read that link.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 28th, 2009
0
Re: Graphics Programming
Just use the drawString method to draw your words. g.drawString(parameters go here) http://java.sun.com/j2se/1.4.2/docs/.../Graphics.html

You can set the Graphics contexts' font and color and stuff before you draw the string, and it will draw in that font and color. At least so says the API if you read that link.
I'm aware of the API. And I know what tools to use, but I don't know how to use them; I was hoping that someone could help me write the code. Thank you for you time
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009
Oct 28th, 2009
0
Re: Graphics Programming
Never mind; I fixed it thank you for your time
Reputation Points: 10
Solved Threads: 0
Newbie Poster
babylonlion is offline Offline
19 posts
since Oct 2009
Oct 28th, 2009
0
Re: Graphics Programming
Ok, glad you got it fixed. The reason I thought you didn't know the API was because it looked like you were trying to write words by drawing the lines individually and connecting them. But anyway, my mistake. Mark this solved if you have no more questions.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 7th, 2009
0
Re: Graphics Programming
.
Last edited by quuba; Nov 7th, 2009 at 4:26 am. Reason: my error in thread #
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with Code
Next Thread in Java Forum Timeline: P2P File sharing Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC