📄 shapespanel.java
字号:
import java.awt.Graphics;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ShapesPanel extends JPanel {
public final int TITLEBAR_SIZE = 35, SHAPE_NUMBER = 5;
ShapeGenerator gen;
private int size;
Shape[] shapes = new Shape[SHAPE_NUMBER];
public ShapesPanel(int size){
this.size = size;
gen = new ShapeGenerator( size - TITLEBAR_SIZE );
gen.setShapeType(ShapeGenerator.ShapeType.CIRCLE);
for(int i = 0 ; i < this.SHAPE_NUMBER; i++){
shapes[i] = gen.next( );
}
Shape.setBackgroundColor( this.getBackground() );
}
public void createShapes( ShapeGenerator.ShapeType type){
gen = new ShapeGenerator( size - TITLEBAR_SIZE );
gen.setShapeType(type);
for(int i = 0 ; i < this.SHAPE_NUMBER; i++){
shapes[i] = gen.next( );
}
repaint();
}
public void sortShapes1(){
java.util.Arrays.sort(shapes);
}
public void sortShapes2(){
// You need to write this method using the bubblesort algorithm, but
// our version of bubblesort will sort in decsending order...largest to
// smallest. Remember because Shape says it implements the compareTo
// method, but as an abstract method, you will need to override
// this method. You will need to use the compareTo method to do the sorting
/*for( int i = 0; i < this.SHAPE_NUMBER; i++)
{
for ( int j = i + 1; j < this.SHAPE_NUMBER; j++)
{
if( shapes[i].compareTo( shapes[j]) < 0)
{
Shape temp = shapes[i];
shapes[i] = shapes[j];
shapes[j] = temp;
}
}
}*/
boolean exchange;
for ( int i = 0; i < this.SHAPE_NUMBER - 1; i++)
{
exchange = false;
for ( int j = this.SHAPE_NUMBER - 2; j >= i; j--)
{
if( shapes[j].compareTo( shapes[j + 1]) < 0 )
{
Shape temp = shapes[j];
shapes[j] = shapes[j + 1];
shapes[j + 1] = temp;
exchange = true;
}
}
if( !exchange )
return;
}
}
public void paintComponent(Graphics g){
super.paintComponent(g);
for(Shape s : shapes )
s.draw( g );
}
public void eraseShape(int shapeNumber ){
if( shapeNumber <= SHAPE_NUMBER && shapeNumber > 0 ){
shapes[shapeNumber -1 ].setVisible(false);
repaint();
}
}
public void showShape(int shapeNumber ){
if( shapeNumber <= SHAPE_NUMBER && shapeNumber > 0 ){
shapes[shapeNumber -1 ].setVisible(true);
repaint();
}
}
} // end of ShapesPanel class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -