Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: java graphics2d issue

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Unhappy java graphics2d issue

    well the following code is the main code that I use for a drawing program (the drawing panel) that I use all the other buttons are set but I found it redicilous to copy the huge code so I'd only copied the panel code only.
    I'm encountring the following issues:
    1- when I change the color (for the next shape) all the previous shape colors change! (how can I set the panel that it will set a the color for only the shape that is going to be drawn?)
    2- I want the shape to be drawn by pressing only a certain button (the panel is activated with the values without pressing the button how can I change that?)
    3- I cannot seem to find a way to make a constant drawing (as in the case of the pen and eraser any code examples for it ?
    4- the stroke does not seem to apply to the shapes any changes I should make ?
    5- how can I make the line drawing to be on a certain angle?
    6- how can I change the angle of the curve given (any examples ?)
    7- the thing no matter how much I try to change the background color it doesn't change it... any ideas ?
    I would truly appreciate any answers for any of the following questions ...
    thank you
    public class PaintPanel extends Canvas {
    	private int pointCount = 0;
    	private Point points[] = new Point[10000];
    	private Color color = new Color (0,0,0);
    	public static List<Shape> myShapes = new ArrayList<Shape>();
    	public static List<Stroke> myShapesStrokes = new ArrayList<Stroke>();
    	public static List<Color> myShapesColors = new ArrayList<Color> ();
    	public PaintPanel() {
    		addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent event) {
    	public void paint(Graphics g) {
    		Graphics2D g2d = (Graphics2D) g;
    		color = Animator.fillColor;
    		for (int i = 0; i<myShapes.size(); i++){
    		g2d.setStroke(new BasicStroke(Animator.stroke));
    		if (Animator.dashed){
    			float dashes[] = { 10 };
    			if(Animator.roundEdge == true){
    			g2d.setStroke( new BasicStroke(Animator.stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, dashes, 0 ) );            
    				g2d.setStroke( new BasicStroke(Animator.stroke, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND, 10, dashes, 0 ) );            
    			else {
    				if (Animator.roundEdge == true){
    					g2d.setStroke( new BasicStroke(Animator.stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) );            
    					g2d.setStroke( new BasicStroke(Animator.stroke, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) ); 
    		Animator.draw= false;
    		switch (Animator.panelIndex)//1 pen, 2 eraser, 3 line, 4 curve, 5 circle,6 rectangle, 7 star, 8 clear
    		case 1:
    		case 2:
    			g2d.setColor(new Color (255,255,255));
    			g2d.drawRect(getMousePosition().x, getMousePosition().y, Animator.width, Animator.hight);
    		case 3:
    			g2d.drawLine(getMousePosition().x - Animator.width/2, getMousePosition().y, getMousePosition().x + Animator.width/2, getMousePosition().y );
    			myShapes.add(new Line2D.Double(getMousePosition().x - Animator.width/2, getMousePosition().y, getMousePosition().x + Animator.width, getMousePosition().y ));
    		case 4:
    			g2d.drawArc(getMousePosition().x, getMousePosition().y, Animator.width, Animator.width, 15, 45);
    			myShapes.add(new Arc2D.Double(getMousePosition().x, getMousePosition().y, Animator.width, Animator.width, 15, 45,Arc2D.PIE));
    		case 5:
    			g2d.drawOval(getMousePosition().x - Animator.width/2, getMousePosition().y - Animator.hight/2, Animator.width, Animator.hight);
    			myShapes.add(new Ellipse2D.Double(getMousePosition().x - Animator.width/2, getMousePosition().y - Animator.hight/2, Animator.width, Animator.hight));
    		case 6:
    			color = Animator.fillColor;
    			g2d.drawRect(getMousePosition().x - Animator.width /2, getMousePosition().y - Animator.hight/2, Animator.width, Animator.hight);
    			myShapes.add(new Rectangle2D.Double(getMousePosition().x - Animator.width/2, getMousePosition().y - Animator.hight/2, Animator.width, Animator.hight));
    		case 7:
    			GSegment star = new GSegment();
    		      GStyle starStyle = new GStyle();
    		      starStyle.setForegroundColor (Animator.fillColor);
    		      starStyle.setBackgroundColor (Animator.strokeColor);
    		      starStyle.setLineWidth (Animator.stroke);
    		      star.setStyle (starStyle);
    		      star.setGeometry (Geometry.createStar (Animator.hight,Animator.hight, Animator.width, getMousePosition().x, Animator.points ));
    			Shape s = (Shape) star;
    		case 8:
    			g2d.clearRect(getX(), getY(), getWidth(), getHeight());
    Last edited by copeg; September 15th, 2010 at 12:57 PM. Reason: Please use the code tags

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: java graphics2d issue

    Well, put your code in code tags and I can be more help. For now, I'll help you with what I know about Painting.

    1- This is because every time the repaint() method is called, the ENTIRE visual display is repainted. This means that every element will get the color assigned to the Graphics2d Object at the time it repaints.

    2- Can you explain this more clearly.

    3- If you are looking for an eraser sort of thing, you could always paint over it with the same color as the background.

    I feel what I said in number 1 can explain a few of your issues. I'm in a rush, put your code in tags and tell me if what I've said helps at all. I'll be able to get back to you in a few hours maybe.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Madgaon, Goa, India
    Thanked 48 Times in 46 Posts

    Default Re: java graphics2d issue

    That code appears to be ancient. You would be better off developing your own code step by step than trying to coerce someone else's code into doing whatever it is you require.

    Also, you would be better off doing custom painting in a JPanel with a paintComponent override. Canvas (and all AWT visual components) are passť.


Similar Threads

  1. java - soot issue...counting the jimple line numbers??
    By volatile in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2010, 12:49 PM
  2. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  3. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM
  4. Replies: 1
    Last Post: November 14th, 2008, 03:00 PM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM

Tags for this Thread