Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.graphics;
- import java.awt.Color;
- /**
- * A sample component class.
- */
- public class X2 extends javax.swing.JComponent {
- // a field initializing the color variable determining color of lines
- private Color color;
- // our "getter" method for determining the current pen color
- public java.awt.Color getLineColor() {
- return color;
- }
- /**
- * Constructor, sets the preferred size and starting color.
- */
- public X2(Color color) {
- // Initialize the line color.
- this.setLineColor(color);
- setPreferredSize(new java.awt.Dimension(25, 25));
- }
- public void setLineColor(Color hue) {
- color = hue;
- repaint();
- }
- /**
- * The necessary method. This method renders the component.
- *
- * @param g The Graphics object use to render
- */
- @Override
- public void paintComponent(java.awt.Graphics g) {
- // paint the underlying component
- super.paintComponent(g);
- // set the size
- int width = getWidth();
- int height = getHeight();
- g.setColor(color);
- // draw two lines
- g.drawLine(0, 0, width - 1, height - 1);
- g.drawLine(0, height - 1, width - 1, 0);
- }
- /*
- *@args None
- * An application method to test our X2 class out.
- */
- public static void main(String[] args) {
- javax.swing.JFrame win;
- win = new javax.swing.JFrame("Test Component");
- win.add(new X2(Color.BLUE), java.awt.BorderLayout.NORTH);
- win.add(new X2(Color.RED), java.awt.BorderLayout.SOUTH);
- win.add(new X2(Color.GREEN), java.awt.BorderLayout.EAST);
- win.add(new X2(Color.CYAN), java.awt.BorderLayout.WEST);
- win.setSize(400, 300);
- win.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
- win.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement