Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
961 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 500 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 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 52
egit plugin on eclipse 8 82
iterator/ListIterator approach 17 39
Eclipse Java import and method not resolved 4 45
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 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