Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
992 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
[X]
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
  • 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

688 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