0 Replies - 711 Views - Last Post: 08 March 2008 - 04:54 PM Rate Topic: -----

#1 kramed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 03-October 06

Graphics2D: How do I retain color and fill options for shape objects?

Posted 08 March 2008 - 04:54 PM

Hello,

The requirements for my assignment are to draw multiple shapes onto a canvas. These shapes should be of varying size and colour which are selected from an option panel. I have the panel, drawing and most things down but the problem I am having is that when I colour and fill a shape, whenever I draw another shape, the colour and fill of the previous shapes are lost. I have assumed that those properties would have been saved in each Shape object but I am either wrong or am going about it the wrong way. Code attached, please advise!

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.Graphics2D.*;
import java.util.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author mark
 */
public class CanvasPane extends JComponent {

	public CanvasPane(MainPanel p) {
		panel = p;
		shapeList = new ArrayList<Shape>();
		current_shape = null;

		addMouseListener(new MouseHandler());
		addMouseMotionListener(new MouseMotionHandler());
	}

	@Override
	public void paintComponent(Graphics g) {	// Difference between paint() ?
		Graphics2D g2 = (Graphics2D) g;

		// draw shape
		if (panel.getFill()) {
			//g2.setColor(panel.getFillColor());	// No difference?
			g2.setPaint(panel.getFillColor());
			g2.fill(current_shape);
		}
		
		for (Shape s : shapeList)
		{
			g2.draw(s);
		}
		
		if (current_shape != null)
		{
			g2.draw(current_shape);
		}
	}
	
	// Objects
	private MainPanel panel;
	private ArrayList<Shape> shapeList;
	private Shape current_shape;		// most recent shape to add to list

	private class MouseHandler extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent event) {
			double y = event.getY();
			double x = event.getX();
			double height = panel.getShapeHeight(); // get canvas size
			double width = panel.getShapeWidth();
			
			// get selected shape from combo menu on panel
			p = panel.getShape();

			if (p.equals("Rectangle")) {
				current_shape = new Rectangle2D.Double(x - width / 2, y - height / 2, width, height);
				panel.setArea(height, width);
			}
			if (p.equals("Arc")) {
				//
			}
			if (p.equals("Oval")) {
				//
			}
			if (p.equals("Line")) {
				//
			}
			
			// add a new shape to list & update panel counter
			shapeList.add(current_shape);
			panel.setShapeNum(shapeList.size());
			repaint();
		}

		@Override
		public void mouseClicked(MouseEvent event) {
			//
		}
		
		private String p;
	} // END INNER CLASS

	private class MouseMotionHandler implements MouseMotionListener {

		public void mouseMoved(MouseEvent event) {
			double x = event.getX();
			double y = event.getY();
			panel.setCoordsLabel(x, y);

		}

		public void mouseDragged(MouseEvent event) {
			double y = event.getY();
			double x = event.getX();
			double height = panel.getShapeHeight();
			double width = panel.getShapeWidth();
			
			// DOES JACK ALL RIGHT NOW
		}
	} // END INNER CLASS
} // END CanvasPane CLASS





Edit: Something wrong with Syntax highlighting? code=Java did funky things

This post has been edited by kramed: 08 March 2008 - 04:56 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1