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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Help with Draw Program

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Draw Program

    Hello everyone! I am currently in a java class in my high school, and I've been having some issues with a program. It's mostly finished, but when I try to draw a vertical line, it remains horizontal. Everything else works perfectly fine. Take a look at my code and see if you can find the problem, because it's frustrating me to no end! Thanks!

     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class DrawPanel extends JPanel
    {
    	private int shape, color, xValue, yValue, length, orientation, i;
    	private final int CIRCLE = 0, RECTANGLE = 1, LINE = 2, RED = 0, YELLOW = 1, GREEN = 2;
    	private final int BLUE = 3, BLACK = 4, HORIZONTAL = 0, VERTICAL = 1, DRAW = 200;
    	private JLabel figure, colorLabel, location;
    	private JRadioButton circle, rectangle, line, red, yellow, green, blue, black;
    	private JCheckBox random;
    	private JTextField xValueField, yValueField;
    	private JButton draw, set, horizontal, vertical;
    	private JPanel panel;
    	private JOptionPane optionPane;
     
    	public DrawPanel()
    	{		
    		panel = new JPanel();
    		panel.setPreferredSize(new Dimension(200, 150));
    		panel.setBackground(Color.gray);
     
    		figure = new JLabel ("Figure panel: ");
     
    		circle = new JRadioButton ("circle", true);
    		circle.setBackground(Color.cyan);
    		rectangle = new JRadioButton ("rectangle");
    		rectangle.setBackground(Color.cyan);
    		line = new JRadioButton ("line");
    		line.setBackground(Color.cyan);
     
    		ButtonGroup figureOptions = new ButtonGroup();
    		figureOptions.add(circle);
    		figureOptions.add(rectangle);
    		figureOptions.add(line);
     
    		colorLabel = new JLabel ("Color panel:");
     
    		red = new JRadioButton ("red", true);
    		red.setBackground(Color.red);
    		yellow = new JRadioButton ("yellow");
    		yellow.setBackground(Color.yellow);
    		green = new JRadioButton ("green");
    		green.setBackground(Color.green);
    		blue = new JRadioButton ("blue");
    		blue.setBackground(Color.blue);
    		black = new JRadioButton ("black");
    		black.setBackground(Color.gray);
     
    		ButtonGroup colorOptions = new ButtonGroup();
    		colorOptions.add(red);
    		colorOptions.add(yellow);
    		colorOptions.add(green);
    		colorOptions.add(blue);
    		colorOptions.add(black);
     
    		ShapeButtonListener sbListener = new ShapeButtonListener();
    		circle.addActionListener(sbListener);
    		rectangle.addActionListener(sbListener);
    		line.addActionListener(sbListener);
     
    		ColorButtonListener cbListener = new ColorButtonListener();
    		red.addActionListener(cbListener);
    		yellow.addActionListener(cbListener);
    		green.addActionListener(cbListener);
    		blue.addActionListener(cbListener);
    		black.addActionListener(cbListener);
     
    		location = new JLabel("Location panel:");
     
    		random = new JCheckBox("random");
    		random.setBackground(Color.cyan);
     
    		xValueField = new JTextField(3);
    		JLabel xValueLabel = new JLabel("x value:");
    		yValueField = new JTextField(3);
    		JLabel yValueLabel = new JLabel("y value:");
     
    		XYListener xyListener = new XYListener();
    		xValueField.addActionListener(xyListener);
    		yValueField.addActionListener(xyListener);
     
    		RandomListener rListener = new RandomListener();
    		random.addItemListener(rListener);
     
    		draw = new JButton("Draw!");
     
    		DrawListener dListener = new DrawListener();
    		draw.addActionListener(dListener);
     
    		set = new JButton("Set");
     
    		SetListener sListener = new SetListener();
    		set.addActionListener(sListener);
     
    		panel.add(circle);
    		panel.add(rectangle);
    		panel.add(line);
    		panel.add(red);
    		panel.add(yellow);
    		panel.add(green);
    		panel.add(blue);
    		panel.add(black);
    		panel.add(random);
    		panel.add(xValueLabel);
    		panel.add(xValueField);
    		panel.add(yValueLabel);
    		panel.add(yValueField);
    		panel.add(set);
    		panel.add(draw);
     
    		add(panel);
    	}
     
    	public void paintComponent(Graphics page)
    	{
    		super.paintComponent(page);
     
    		switch(color)
    		{
    			case RED:
    				page.setColor(Color.red);
    				break;
    			case YELLOW:
    				page.setColor(Color.yellow);
    				break;
    			case GREEN:
    				page.setColor(Color.green);
    				break;
    			case BLUE:
    				page.setColor(Color.blue);
    				break;
    			case BLACK:
    				page.setColor(Color.black);
    				break;
    			default:
    				page.setColor(Color.white);
     
    		}
     
    		switch (shape)
    		{
    			case CIRCLE:
    				page.fillOval(xValue, yValue + DRAW, 50, 50);
    				break;
    			case RECTANGLE:
    				page.fillRect(xValue, yValue + DRAW, 50, 60);
    				break;
    			case LINE:
    				switch(orientation)
    				{
    					case HORIZONTAL:
    						page.drawLine(xValue, yValue + DRAW, xValue + length, yValue + DRAW);
    						break;
    					case VERTICAL:
    						page.drawLine(xValue, yValue + DRAW, xValue, yValue + length + DRAW);
    						break;
    				}				
    				break;
    			default:
    				System.out.println("You didn't select a figure!");
    		}
    	}	
     
    	private class ShapeButtonListener implements ActionListener
    	{
    		public void actionPerformed (ActionEvent event)
    		{
    			if (event.getSource() == circle)
    			{	
    				shape = CIRCLE;
    			}
    			else
    			{
    				if (event.getSource() == rectangle)
    				{
    					shape = RECTANGLE;
    				}
    				else
    				{
    					shape = LINE;
     
    					optionPane = new JOptionPane();
    					length = Integer.parseInt(optionPane.showInputDialog("Enter the length of the line:"));
     
    					horizontal = new JButton("horizontal");
    					vertical = new JButton ("vertical");
     
    					Object[] orientations = {"horizontal", "vertical"};
     
    					i = optionPane.showOptionDialog(null,"Pick your orientation.",
    						"Line Options", optionPane.YES_NO_OPTION,
    							optionPane.QUESTION_MESSAGE, null, orientations, orientations[1]);
     
    					OrientButtonListener obListener = new OrientButtonListener();
    					horizontal.addActionListener(obListener);
    					vertical.addActionListener(obListener);
    				}
    			}
    		}
     
    		private class OrientButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent event)
    			{
    				if (i == optionPane.YES_OPTION)
    				{
    					orientation = HORIZONTAL;
    				}
    				else
    				{
    					orientation = VERTICAL;
    				}
    			}
    		}
    	}
     
    	private class ColorButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			if (event.getSource() == red)
    			{
    				color = RED;
    			}
    			else
    			{
    				if (event.getSource() == yellow)
    				{
    					color = YELLOW;
    				}
    				else
    				{
    					if (event.getSource() == green)
    					{
    						color = GREEN;
    					}
    					else
    					{
    						if (event.getSource() == blue)
    						{
    							color = BLUE;
    						}
    						else
    						{
    							color = BLACK;
    						}
    					}
    				}
    			}
    		}
    	}
     
    	private class XYListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			String xText = xValueField.getText();
     
    			xValue = Integer.parseInt(xText);
     
    			String yText = yValueField.getText();
     
    			yValue = Integer.parseInt(yText);
    		}
    	} 	
     
    	private class RandomListener implements ItemListener
    	{
    		public void itemStateChanged(ItemEvent event)
    		{
    			if (random.isSelected())
    			{
    				xValue = (int)(Math.random() * 100) + DRAW;
    				yValue = (int)(Math.random() * 100)+ DRAW;
    			}
    		}
    	}
     
    	private class DrawListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			int okay = JOptionPane.showConfirmDialog(null, "Are you sure you want to continue?");
     
    			if (okay == JOptionPane.YES_OPTION);
    			{
    				repaint();
    			}
    		}
    	}
     
    	private class SetListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    			xValue = Integer.parseInt(xValueField.getText());
    			yValue = Integer.parseInt(yValueField.getText());
    		}
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Draw Program

    Where are you drawing the line? If you want help, it's best to create an SSCCE that demonstrates the problem in as little code as possible. What values are you passing into drawLine? Where do you expect that to be drawn?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Draw Program

    I'm guessing you mean this part?

     
    case LINE:
    	switch(orientation)
    	{
    		case HORIZONTAL:
    			page.drawLine(xValue, yValue + DRAW, xValue + length, yValue + DRAW);
    			break;
    		case VERTICAL:
    			page.drawLine(xValue, yValue + DRAW, xValue, yValue + length + DRAW);
    			break;
    	}				
    break;

    Have you tried checking the all of the values using debug to see if they have been set wrong somwhere

Similar Threads

  1. Re: Draw labyrinth ( I need help)
    By tubluforu in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2012, 12:08 AM
  2. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  3. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM
  4. Won't Draw??
    By The_Mexican in forum Java Applets
    Replies: 4
    Last Post: March 13th, 2010, 06:00 PM
  5. lucky draw.. (pls help)
    By amin in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 20th, 2009, 11:30 PM