// TGradientPaint.java import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; class TGradientPaint extends JFrame { DisplayCanvas canvas; String[] colorLabels = {"White", "Red", "Blue", "Green", "Yellow", "Magenta", "Light Gray", "Black"}; JRadioButton acyclicButton, cyclicButton; public TGradientPaint() { // 1. Assign a title to the frame and get a handle on // the frame's content pane super("TGradientPaint"); Container container = getContentPane(); // 2. Add the canvas with rectangles canvas = new DisplayCanvas(); container.add(canvas); // 3. Create a control panel with titled border JPanel panel = new JPanel(); TitledBorder border = new TitledBorder( "Select a Fill Mode, and Double Click Mouse" + " at Two Points with Different Colors..."); panel.setBorder(border); // 4. Create a combo box and the necessary radio buttons JComboBox comboBox = new JComboBox(colorLabels); comboBox.addActionListener(new ComboBoxListener()); acyclicButton = new JRadioButton("Acyclic", true); acyclicButton.addActionListener(new RadioButtonListener()); cyclicButton = new JRadioButton("Cyclic"); cyclicButton.addActionListener(new RadioButtonListener()); ButtonGroup group = new ButtonGroup(); group.add(acyclicButton); group.add(cyclicButton); // 5. Add the combo box, and radio buttons to the panel panel.add(comboBox); panel.add(acyclicButton); panel.add(cyclicButton); // 6. Add the panel to the frame container.add(BorderLayout.SOUTH, panel); // 7. Add a frame closing listener and display the frame addWindowListener(new WindowEventHandler()); pack(); // Packs around the contents with suitable size show(); // Display the frame } // 8. Code to handle closing of the frame class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // 9. The main method... public static void main(String arg[]) { new TGradientPaint(); } // 10. Canvas to draw round-cornered rectangles class DisplayCanvas extends Canvas { // 11. Points and Colors for the gradient patterns Point2D point1, point2; Color fillColor = Color.black; Color[] colors = {Color.white, Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.lightGray, Color.black}; Color fillColor1, fillColor2; boolean cyclicMode = false; // Constructor DisplayCanvas() { // 12. Add the mouse listener to receive a rectangle selection addMouseListener(new MyMouseListener()); setBackground(Color.white); // For canvas background color setSize(400, 225); // Canvas width=400 height=225 } public void paint(Graphics g) { // 13. Create the graphics context object Graphics2D g2D = (Graphics2D) g; if (point1 != null && point2 == null) drawHighlightSquare(g2D, fillColor, point1); if (point1 != null && point2 != null) { Paint gp = new GradientPaint(point1, fillColor1, point2, fillColor2, cyclicMode); g2D.setPaint(gp); g2D.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight())); } } public void drawHighlightSquare(Graphics2D g2D, Color c, Point2D p) { g2D.setColor(c); double x = p.getX(); double y = p.getY(); g2D.fill(new Rectangle.Double(x-3.0, y-3.0, 6.0, 6.0)); } } // 14. The mouse listener to handle double clicks over the canvas. class MyMouseListener extends MouseAdapter { int clickTime = 0; public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { if (clickTime == 0) { clickTime++; canvas.fillColor1 = canvas.fillColor; canvas.point2 = null; canvas.point1 = e.getPoint(); canvas.repaint(); } else if (clickTime == 1) { clickTime--; canvas.fillColor2 = canvas.fillColor; canvas.point2 = e.getPoint(); canvas.repaint(); } } } } // 15. Combo box listener to handle color selections class ComboBoxListener implements ActionListener { public void actionPerformed(ActionEvent e) { JComboBox cBox = (JComboBox) e.getSource(); String color = (String) cBox.getSelectedItem(); for (int i=0; i