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?

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

public class DrawJavaMugTest {

	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable(){
			
			public void run(){
				DrawFrame frame = new DrawFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class DrawFrame extends JFrame{
	
	public DrawFrame(){
		setTitle("babylonlion");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		
	    //DrawJavaMugPanel panel = new DrawJavaMugPanel();
	    //add(panel);
	    
		DrawJavaMugComponent component = new DrawJavaMugComponent();
	    add(component);
	}
	public static final int DEFAULT_WIDTH = 400;
	public static final int DEFAULT_HEIGHT = 400;
}

class DrawJavaMugComponent extends JComponent{
	
	public void paintComponent(Graphics g){
		
	   Graphics2D g2 = (Graphics2D) g;
		
	    g2.draw(new Line2D.Double(100, 100, 100, 300));//Vertical line 1
	    g2.draw(new Line2D.Double(276, 100, 276, 300));//Vertical line 2
	    g2.draw(new Line2D.Double(276, 300, 100, 300));//Horizontal line
		
	    Ellipse2D ellipse1 = new Ellipse2D.Double();//the opening of the mug
	    ellipse1.setFrame(276, 125, 40, 150);
	    g2.draw(ellipse1);
	    
	    Ellipse2D ellipse2 = new Ellipse2D.Double();//the handle of the mug
	    ellipse2.setFrame(99.5, 91, 177, 15);//(leftX,topY,width, height)
	    g2.draw(ellipse2);
	    
	}
}

Recommended Answers

All 5 Replies

Just use the drawString method to draw your words. g.drawString(parameters go here) http://java.sun.com/j2se/1.4.2/docs/api/java/awt/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

Never mind; I fixed it :) thank you for your time

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.

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.