📄 pieshape改1.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;
class PieColors
{
Color pieColorArray[] = {new Color(210,60,60), new Color(60,210,60), new Color(60,60,210), new Color(120,60,120), new Color(60,120,210), new Color(210,120,60)};
int curPieColor = 0;
public Color getPieColor()
{
return pieColorArray[curPieColor];
}
public void setNewColor()
{
curPieColor++;
if(curPieColor >= pieColorArray.length)
{
curPieColor = 0;
}
}
}
public class PieShape
{
public static void main(String[] args)
{
TextTestFrame frame = new TextTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class TextTestFrame extends JFrame
{
public static final int DEFAULT_WIDTH = 800;
public static final int DEFAULT_HEIGHT = 600;
public TextTestFrame()
{
setTitle("Pie");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
Container contentPane = getContentPane();
PiePanel pie = new PiePanel();
contentPane.add(pie, BorderLayout.CENTER);
}
}
class PiePanel extends JPanel
{
int WIDTH = 400;
int HEIGHT = 200;
int innerOffset=20;
int pieHeight = HEIGHT - (innerOffset * 2);
int pieWidth = pieHeight;
int halfWidth = WIDTH/2;
int x_pie = innerOffset;
int y_pie = innerOffset;
int border = 20;
String products[] = {"APPLE","PEAR","OYY","kkk"};
float sales[] = {3,4,5,6};
int startAngle = 0;
int legendWidth = 20;
int x_legendText = halfWidth + innerOffset/2 + legendWidth + 5;
int x_legendBar = halfWidth + innerOffset/2;
int textHeight = 20;
int curElement = 0;
int y_legend = 0;
int innerWIDTH = WIDTH - (innerOffset * 2);
//graph dimensions
Dimension graphDim = new Dimension(WIDTH,HEIGHT);
Rectangle graphRect = new Rectangle(graphDim);
//border dimensions
Dimension borderDim = new Dimension(halfWidth-2,HEIGHT-2);
Rectangle borderRect = new Rectangle(borderDim);
public void paintComponent(Graphics g)
{
PieColors pc = new PieColors();
Color dropShadow = new Color(240,240,240);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// Set Antialiasing
RenderingHints renderHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(renderHints);
//Set graph background color to white:
g2d.setColor(Color.white);
g2d.fill(graphRect);
//Draw black border
g2d.setColor(Color.black);
borderRect.setLocation(1,1);
g2d.draw(borderRect);
//Now draw border for legend
borderRect.setLocation((WIDTH/2) + 1,1);
g2d.draw(borderRect);
//Main chart Ellipse
Ellipse2D.Double elb = new Ellipse2D.Double(x_pie - border/2, y_pie - border/2, pieWidth + border, pieHeight + border);
//Shadow
g2d.setColor(dropShadow);
g2d.fill(elb);
//Border
g2d.setColor(Color.black);
g2d.draw(elb);
/////////////////////////////////////////////////////////////////////////////////////
// Calculate the total sales
/////////////////////////////////////////////////////////////////////////////////////
float salesTotal = 0.0f;
int lastElement = 0;
for(int i=0; i<products.length; i++)
{
if(sales[i] > 0.0f)
{
salesTotal += sales[i];
lastElement = i;
}
}
Dimension legendDim = new Dimension(legendWidth , textHeight/2);
Rectangle legendRect = new Rectangle(legendDim);
for(int i=0; i<products.length; i++)
{
if(sales[i] > 0.0f)
{
float perc = (sales[i]/salesTotal);
//Calculate new angle
int sweepAngle = (int)(perc * 360);
//Check that the last element goes back to 0 position
if (i == lastElement)
{
sweepAngle = 360-startAngle;}
// Draw Arc
g2d.setColor(pc.getPieColor());
g2d.fillArc(x_pie, y_pie, pieWidth, pieHeight, startAngle, sweepAngle);
//Increment startAngle with the sweepAngle
startAngle += sweepAngle;
/////////////
//Draw Legend
/////////////
//Set y position for bar
y_legend = curElement * textHeight + innerOffset;
//Display the current product
String display = products[i];
g2d.setColor(Color.black);
g2d.drawString(display, x_legendText, y_legend);
//Display the total sales
display = "" + (int)sales[i];
g2d.setColor(Color.black);
g2d.drawString(display, x_legendText + 80, y_legend);
//Display the sales percentage
display = " (" + (int)(perc*100) + "%)";
g2d.setColor(Color.red);
g2d.drawString(display, x_legendText + 110, y_legend);
//Draw the bar
g2d.setColor(pc.getPieColor());
legendRect.setLocation(x_legendBar,y_legend - textHeight/2);
g2d.fill(legendRect);
//Set new pie color
pc.setNewColor();
//Increment
curElement++;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -