Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
File Manager
/
Ebook
/
Informatique
/
JAVA
/
Source_TLS
/
Move
:
blabla.java
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class blabla extends JFrame { // JPanel for the shape and color controls private JPanel controlsJPanel; // JComboBox to allow selection of a shape private JComboBox shapeJComboBox; // JButton to select the color private JButton colorJButton; // PaintJPanel for drawing shapes private PaintJPanel painterPaintJPanel; // array of shape types private String[] shapeTypes = {"Line","Rectangle","Oval"}; public blabla() { createUserInterface(); } // create and position GUI components; register event handlers private void createUserInterface() { // get content pane for attaching GUI components Container contentPane = getContentPane(); // enable explicit positioning of GUI components contentPane.setLayout(null); // set up controlsJPanel controlsJPanel = new JPanel(); controlsJPanel.setBounds(0,0,400,40); controlsJPanel.setLayout(null); contentPane.add( controlsJPanel ); // set up painterPaintJPanel painterPaintJPanel = new PaintJPanel(); painterPaintJPanel.setBounds(0,40,400,340); painterPaintJPanel.setBackground(Color.WHITE); contentPane.add( painterPaintJPanel ); // set up shapeJComboBox shapeJComboBox = new JComboBox( shapeTypes ); shapeJComboBox.setBounds(90,2,100,24); controlsJPanel.add( shapeJComboBox ); shapeJComboBox.addActionListener( new ActionListener()// anonymous inner class { // event method called when shapeJComboBox is selected public void actionPerformed( ActionEvent event ) { shapeJComboBoxActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set up colorJButton colorJButton = new JButton(); colorJButton.setBounds(210,2,80,24); colorJButton.setText("Color"); controlsJPanel.add( colorJButton ); colorJButton.addActionListener( new ActionListener() // anonymous inner class { // event handler called when colorJButton is pressed public void actionPerformed( ActionEvent event ) { colorJButtonActionPerformed( event ); } } // end anonymous inner class ); // end call to addActionListener // set properties of application’s window setTitle("Drawing Shapes"); // set title bar string setSize(408,407); // set window size setVisible(true); // display window } // end method createUserInterface // select a new color for the shape private void colorJButtonActionPerformed( ActionEvent event ) { Color selection = JColorChooser.showDialog(null, "Select a Color",Color.BLACK); if ( selection != null) { colorJButton.setBackground( selection ); painterPaintJPanel.setCurrentColor( selection ); } } // end method colorJButtonActionPerformed // set the selected shape in the painting panel private void shapeJComboBoxActionPerformed( ActionEvent event ) { painterPaintJPanel.setCurrentShapeType( ( String )shapeJComboBox.getSelectedItem() ); } // end method shapeJComboBoxActionPerformed // main method public static void main( String args[] ) { blabla application = new blabla(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }// end method main } class PaintJPanel extends JPanel { // ArrayList to hold the shapes private ArrayList shapesArrayList = new ArrayList(); // currently selected shape type private String currentType = "Line"; // currently selected color private Color currentColor = new Color(204,204,204); // no-argument constructor public PaintJPanel() { addMouseListener( new MouseAdapter() // anonymous inner class { // event handler called when mouse button is pressed public void mousePressed( MouseEvent event ) { paintJPanelMousePressed( event ); } } // end anonymous inner class ); // end call to addMouseListener addMouseMotionListener( new MouseMotionAdapter() // anonymous inner class { // event handler called when the mouse is dragged public void mouseDragged( MouseEvent event ) { paintJPanelMouseDragged( event ); } }// end anonymous inner class );// end call to addMouseMotionListener }// end constructor // change the current shape type public void setCurrentShapeType( String shape ) { currentType = shape; }// end method setCurrentShapeType // change the current color public void setCurrentColor( Color shapeColor ) { currentColor = shapeColor; } // end method setCurrentColor // create a new shape public void paintJPanelMousePressed( MouseEvent event ) { // user selected line if( currentType.equals("Line") ) { currentShape = new MyLine( event.getX(), event.getY(),event.getX(), event.getY(), currentColor ); } // user selected rectangle else if( currentType.equals("Rectangle") ) { currentShape = new MyRectangle( event.getX(), event.getY(),event.getX(), event.getY(), currentColor ); } // user selected oval else if( currentType.equals("Oval") ) { currentShape = new MyOval( event.getX(), event.getY(),event.getX(), event.getY(), currentColor ); } shapesArrayList.add( currentShape ); } // end method paintJPanelMousePressed // reset the second point for the shape public void paintJPanelMouseDragged( MouseEvent event ) { currentShape.setX2( event.getX() ); currentShape.setY2( event.getY() ); repaint(); } // paint all the shapes public void paintComponent( Graphics g ) { super.paintComponent( g ); MyShape nextShape; Iterator shapesIterator = shapesArrayList.iterator(); // iterate through all the shapes while( shapesIterator.hasNext() ) { // draw each shape nextShape = ( MyShape ) shapesIterator.next(); nextShape.draw( g ); } // end method paintComponent } // end class PaintJPanel } abstract class MyShape extends Object { private int x1; private int y1; private int x2; private int y2; private Color color; // constructor public MyShape(int firstX,int firstY,int secondX,int secondY,Color shapeColor ) { setX1( firstX ); setY1( firstY ); setX2( secondX ); setY2( secondY ); setColor( shapeColor ); } // end constructor // set x1 value public void setX1(int x ) { x1 = x; } // end method setX1 // get x1 value public int getX1() { return x1; } // end method getX1 // set Y1 value public void setY1(int y ) { y1 = y; } // end method setY1 // get Y1 value public int getY1() { return y1; } // end method getY1 // set x2 value public void setX2(int x ) { x2 = x; } // end method setX2 // get x2 value public int getX2() { return x2; } // end method getX2 // set y2 value public void setY2(int y ) { y2 = y; } // end method setY2 // get y2 value public int getY2() { return y2; } // end method getY2 // set color value public void setColor( Color c ) { color = c; } // end method setColor // get color value public Color getColor() { return color; } // end method getColor // abstract draw method public abstract void draw( Graphics g ); }// end class MyShape class MyLine extends MyShape { // constructor public MyLine(int firstX,int firstY,int secondX,int secondY,Color shapeColor ) { super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw a line public void draw( Graphics g ) { g.setColor( getColor() ); g.drawLine( getX1(), getY1(), getX2(), getY2() ); } // end method draw }// end class MyLine class MyRectangle extends MyShape { // constructor public MyRectangle(int firstX,int firstY,int secondX,int secondY, Color shapeColor ) { super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw a rectangle public void draw( Graphics g ) { int upperLeftX = Math.min( getX1(), getX2() ); int upperLeftY = Math.min( getY1(), getY2() ); int width = Math.abs( getX1() - getX2() ); int height = Math.abs( getY1() - getY2() ); g.setColor( getColor() ); g.fillRect( upperLeftX, upperLeftY, width, height ); } // end method draw } // end class MyRectangle class MyOval extends MyShape { // constructor public MyOval(int firstX,int firstY,int secondX,int secondY, Color shapeColor ) { super( firstX, firstY, secondX, secondY, shapeColor ); } // end constructor // draw an oval public void draw( Graphics g ) { int upperLeftX = Math.min( getX1(), getX2() ); int upperLeftY = Math.min( getY1(), getY2() ); int width = Math.abs( getX1() - getX2() ); int height = Math.abs( getY1() - getY2() ); g.setColor( getColor() ); g.fillOval( upperLeftX, upperLeftY, width, height ); } // end method draw }// end class MyOval