Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I use a graphics program to draw 12 strings, one for each standard color (not including white) each in its own color.

Posted on 2010-11-10
5
Medium Priority
?
1,010 Views
Last Modified: 2012-05-10
How do I use a graphics program to draw 12 strings, one for each standard color (not including white) each in its own color. Here are my two classes I started. Please, I am stuck form here.

import javax.swing.JFrame;

/**
   Shows frame with the standard color names.
*/

public class ColorNameViewer
{

   public static void main(String[] args)

   {

      JFrame frame = new JFrame();


      frame.setSize(300, 400);

      frame.setTitle("ColorNameViewer");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      ColorNameComponent component = new ColorNameComponent();

      frame.add(component);


      frame.setVisible(true);

   }

}
 
import javax.swing.JComponent;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;


public class ColorNameComponent extends JComponent
{

   public void paintComponent(Graphics g)

   {
 
     Graphics2D g2 = (Graphics2D) g;

      . . .
  
 }

}

Open in new window

0
Comment
Question by:Rowley4
  • 4
5 Comments
 
LVL 2

Accepted Solution

by:
rachel83 earned 2000 total points
ID: 34102190
ColorNameComponent.java
import javax.swing.JComponent;

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.geom.Line2D;
import java.awt.geom.Line2D.Double;

public class ColorNameComponent extends JComponent
{

   public void paintComponent(Graphics g)

   {
   
   Color[] colors = {
   	Color.BLACK,
   	Color.BLUE,
   	Color.CYAN,
   	Color.DARK_GRAY,
   	Color.GRAY,
	Color.GREEN,
	Color.LIGHT_GRAY,
	Color.MAGENTA,
	Color.ORANGE,
	Color.PINK,
	Color.RED,
	Color.WHITE,
	Color.YELLOW   
   };
   
 
     Graphics2D g2 = (Graphics2D) g;

    
     
     for (int i = 0; i<colors.length; i++) {
     	g2.setColor(colors[i]);
   	g2.draw(new Line2D.Double(i, 0, i, 10));
  }
 }

}

Open in new window

0
 
LVL 2

Assisted Solution

by:rachel83
rachel83 earned 2000 total points
ID: 34102193
ColorNameViewer.java
import javax.swing.JFrame;

/**
   Shows frame with the standard color names.
*/

public class ColorNameViewer
{

   public static void main(String[] args)

   {

      JFrame frame = new JFrame();


      frame.setSize(300, 400);

      frame.setTitle("ColorNameViewer");

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


      ColorNameComponent component = new ColorNameComponent();

      frame.add(component);


      frame.setVisible(true);

   }

}

Open in new window

0
 
LVL 2

Expert Comment

by:rachel83
ID: 34102213
This draws very small lines in the top-left corner. To change the length, position, adjust the following line of code in ColorNameComponent:

g2.draw(new Line2D.Double(i, 0, i, 10));

format: x1, y1, x2, y2
0
 

Author Closing Comment

by:Rowley4
ID: 34102233
This is GREAT!! Thank you SOO much!
0
 
LVL 2

Expert Comment

by:rachel83
ID: 34102360
No problem! Glad you like it :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question