Advertisement
Vita_Harvey

LA3_X2

Jan 18th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. package csc143.graphics;
  2.  
  3. import java.awt.Color;
  4.  
  5. /**
  6.  * A sample component class.
  7.  */
  8. public class X2 extends javax.swing.JComponent {
  9.    
  10.     // a field initializing the color variable determining color of lines
  11.     private Color color;
  12.    
  13.     // our "getter" method for determining the current pen color
  14.     public java.awt.Color getLineColor() {
  15.       return color;
  16. }  
  17.    
  18.     /**
  19.      * Constructor, sets the preferred size and starting color.
  20.      */
  21.     public X2(Color color) {
  22.         // Initialize the line color.      
  23.         this.setLineColor(color);
  24.        
  25.         setPreferredSize(new java.awt.Dimension(25, 25));
  26.        
  27.        
  28.     }
  29.    
  30.     public void setLineColor(Color hue) {
  31.         color = hue;
  32.         repaint();
  33.     }
  34.    
  35.     /**
  36.      * The necessary method. This method renders the component.
  37.      *
  38.      * @param g The Graphics object use to render
  39.      */
  40.     @Override
  41.     public void paintComponent(java.awt.Graphics g) {
  42.    
  43.         // paint the underlying component
  44.         super.paintComponent(g);
  45.        
  46.         // set the size
  47.         int width = getWidth();
  48.         int height = getHeight();        
  49.         g.setColor(color);
  50.         // draw two lines      
  51.         g.drawLine(0, 0, width - 1, height - 1);
  52.         g.drawLine(0, height - 1, width - 1, 0);
  53.     }
  54.    
  55.     /*
  56.      *@args None
  57.      * An application method to test our X2 class out.
  58.      */
  59.     public static void main(String[] args) {          
  60.        
  61.        javax.swing.JFrame win;
  62.        win = new javax.swing.JFrame("Test Component");            
  63.        win.add(new X2(Color.BLUE), java.awt.BorderLayout.NORTH);
  64.        win.add(new X2(Color.RED), java.awt.BorderLayout.SOUTH);
  65.        win.add(new X2(Color.GREEN), java.awt.BorderLayout.EAST);
  66.        win.add(new X2(Color.CYAN), java.awt.BorderLayout.WEST);
  67.        win.setSize(400, 300);
  68.        win.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
  69.        win.setVisible(true);  
  70.    
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement