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
933 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This is GREAT!! Thank you SOO much!
0
 
LVL 2

Expert Comment

by:rachel83
Comment Utility
No problem! Glad you like it :-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
parentbit challenge 3 51
strCount chalenge 3 50
Requested array size exceeds VM limit 3 46
maven example error 3 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now