Code: Select all
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import com.orsoncharts.plot.PiePlot3D;
public class Graphs2 extends JFrame {
private static final int SIZE = 2;
private final SpringLayout l = new SpringLayout();
JPanel newGF2Panel, newGF3Panel;
ChartPanel newKDPanel;
Scanner sc;
public static int n, sum, temp;
public static int input[][];
public static int norm_input[][];
public static int max = Integer.MIN_VALUE;
public Graphs2() {
super("GRAPHS");
setLayout(l);
setSize(1050, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
/////////////////////////////////////////////////////////////////////////////////////////////////////////
JMenuBar menuBar = new JMenuBar();
JMenu mainMenu = new JMenu("Menu");
JMenu newMenu = new JMenu("Set");
mainMenu.add(newMenu);
JMenuItem GF2Item = new JMenuItem("GF2");
newMenu.add(GF2Item);
GF2Item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addGF2();
}
});
JMenuItem GF3Item = new JMenuItem("GF3");
newMenu.add(GF3Item);
GF3Item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addGF3();
}
});
JMenuItem KDItem = new JMenuItem("KD");
newMenu.add(KDItem);
KDItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
addKD();
}
});
mainMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
mainMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
menuBar.add(mainMenu);
setJMenuBar(menuBar);
}
public final void addGF2() {
try
{
sc = new Scanner(new File("input.txt"));
n = sc.nextInt();
input= new int[n][2];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2; j++)
{
if(sc.hasNextInt())
input[i][j] = sc.nextInt();
}
}
sc.close();
}
catch (FileNotFoundException e)
{
System.out.println("Sorry, File not found!");
}
catch (InputMismatchException e)
{
System.out.println("Sorry, InputMismatchException");
}
newGF2Panel = new GF2();
newGF2Panel.setPreferredSize(new Dimension(300,300));
add(newGF2Panel);
l.putConstraint(SpringLayout.WEST, newGF2Panel, 20, SpringLayout.WEST, this);
l.putConstraint(SpringLayout.NORTH, newGF2Panel, 20, SpringLayout.NORTH, this);
newGF2Panel.updateUI();
}
public final void addGF3() {
try
{
sc = new Scanner(new File("input.txt"));
n = sc.nextInt();
input= new int[n][2];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2; j++)
{
if(sc.hasNextInt())
input[i][j] = sc.nextInt();
}
}
sc.close();
}
catch (FileNotFoundException e)
{
System.out.println("Sorry, File not found!");
}
catch (InputMismatchException e)
{
System.out.println("Sorry, InputMismatchException");
}
for (int i = 0; i < n; i++) { //поиск максимума
if(max<input[i][1])
max = input[i][1];
}
norm_input= new int[n][2];
for (int i = 0; i < n; i++){
for (int j = 0; j < 2; j++)
{
norm_input[i][j]=input[i][j];
}
}
for (int i = 0; i < n; i++){
norm_input[i][1]=input[i][1]*100/max;
}
newGF3Panel = new GF3();
newGF3Panel.setPreferredSize(new Dimension(300,300));
add(newGF3Panel);
l.putConstraint(SpringLayout.WEST, newGF3Panel, 340, SpringLayout.WEST, this);
l.putConstraint(SpringLayout.NORTH, newGF3Panel, 20, SpringLayout.NORTH, this);
newGF3Panel.updateUI();
}
public final void addKD() {
try
{
sc = new Scanner(new File("input.txt"));
n = sc.nextInt();
input= new int[n][2];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 2; j++)
{
if(sc.hasNextInt())
input[i][j] = sc.nextInt();
}
}
sc.close();
}
catch (FileNotFoundException e)
{
System.out.println("Sorry, File not found!");
}
catch (InputMismatchException e)
{
System.out.println("Sorry, InputMismatchException");
}
PieDataset dataset = createDataset();
JFreeChart chart = createChart(dataset, "KD");
newKDPanel = new ChartPanel(chart);
newKDPanel.setPreferredSize(new Dimension(400,300));
add(newKDPanel);
String filename = "chart.jpg";
try {
ChartUtilities.saveChartAsJPEG(new File(filename), chart, 500, 300);
} catch (IOException e) {
e.printStackTrace();
}
l.putConstraint(SpringLayout.WEST, newKDPanel, 660, SpringLayout.WEST, this);
l.putConstraint(SpringLayout.NORTH, newKDPanel, 20, SpringLayout.NORTH, this);
newKDPanel.updateUI();
}
private class GF2 extends JPanel{
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gr2d = (Graphics2D) g;
gr2d.setColor(Color.RED);
gr2d.setStroke(new BasicStroke(SIZE));
gr2d.drawLine(0,0, 0, getHeight());
gr2d.drawLine(0,getHeight(), getWidth(), getHeight());
gr2d.drawString("X", getWidth()-8, getHeight()-8);
gr2d.drawString("Y", 8, 10);
String s;
for (int i =0; i<getWidth();i+=50)
{
s = String.valueOf(i);
gr2d.drawLine(i,getHeight()-5, i, getHeight());
gr2d.drawString(s, i+5, getHeight()-8);
}
for (int i =0; i<getHeight();i+=50)
{
s = String.valueOf(i);
gr2d.drawLine(0,getHeight()-i, 5, getHeight()-i);
gr2d.drawString(s, 5, getHeight()-i-8);
}
gr2d.setColor(Color.BLUE);
for (int i=0;i<n-1;i++){
gr2d.drawLine(input[i][0],getHeight()-input[i][1],input[i+1][0],getHeight()-input[i+1][1]);
}
}
}
private class GF3 extends JPanel{
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D gr2d = (Graphics2D) g;
gr2d.setColor(Color.RED);
gr2d.setStroke(new BasicStroke(SIZE));
gr2d.drawLine(0,0, 0, getHeight());
gr2d.drawLine(0,getHeight(), getWidth(), getHeight());
gr2d.drawString("X", getWidth()-8, getHeight()-8);
gr2d.drawString("Y", 8, 10);
String s;
for (int i =0; i<getWidth();i+=50)
{
s = String.valueOf(i);
gr2d.drawLine(i,getHeight()-5, i, getHeight());
gr2d.drawString(s, i+5, getHeight()-8);
}
for (int i =0; i<10;i++)
{
s = String.valueOf(i*10);
gr2d.drawLine(0,getHeight()/10*i, 5, getHeight()/10*i);
gr2d.drawString(s+"%", 5, getHeight()- getHeight()/10*i-8);
}
gr2d.setColor(Color.BLUE);
for (int i=0;i<n-1;i++){
gr2d.drawLine(norm_input[i][0],getHeight()-getHeight()/100*norm_input[i][1],norm_input[i+1][0],getHeight()-getHeight()/100*norm_input[i+1][1]);
}
}
}
private PieDataset createDataset() {
DefaultPieDataset pieDataset = new DefaultPieDataset();
for (int i =0;i<n;i++){
pieDataset.setValue(input[i][0], input[i][1]);
}
return pieDataset;
}
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true,
false);
return chart;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Graphs2().setVisible(true);
}
});
}
}