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
943 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
tomcat not starting 6 45
MySQL  on Tomcat 8 46
pagenation logic how it is working in my code 1 30
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…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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