Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.geom.Ellipse2D;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- class ShapeExample extends JPanel {
- public Graphics2D g2d = null;
- private static final long serialVersionUID = -3717207634048171363L;
- private List<Float[]> _punkty = new ArrayList<Float[]>();
- JButton liniowaB = new JButton("Skala liniowa");
- JButton logarytmicznaB = new JButton("Skala logarytm...");
- static Boolean czyLogarytmicznaSkalaJest=false;
- public void buttony(){
- add(liniowaB);
- add(logarytmicznaB);
- liniowaB.setBounds(2,2,200,40);
- liniowaB.setVisible(true);
- logarytmicznaB.setBounds(5,50,200,40);
- logarytmicznaB.setVisible(true);
- liniowaB.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- czyLogarytmicznaSkalaJest=false; repaint();
- }
- });
- logarytmicznaB.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e)
- {
- czyLogarytmicznaSkalaJest=true; repaint();
- }
- });
- }
- public ShapeExample(List<Float[]> _punkty2){
- this._punkty=_punkty2;
- buttony();
- }
- public void paintComponent(Graphics g) {
- clear(g);
- g2d = (Graphics2D)g;
- g.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
- g.drawLine(this.getWidth()/2,0,this.getWidth()/2,this.getHeight());// y-axis
- for(Float[] kolo : _punkty){
- if(!czyLogarytmicznaSkalaJest){
- g2d.draw(new Ellipse2D.Float(this.getWidth()/2+kolo[0]-(float)(((this.getWidth()+this.getHeight())/200)*kolo[2])/2,
- this.getHeight()/2+kolo[1]-(float)(((this.getWidth()+this.getHeight())/200)*kolo[2])/2,
- (float)(((this.getWidth()+this.getHeight())/200)*kolo[2]),
- (float)(((this.getWidth()+this.getHeight())/200)*kolo[2])));
- }
- else {
- g2d.draw(new Ellipse2D.Float(this.getWidth()/2+kolo[0]-(float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])/2,
- this.getHeight()/2+kolo[1]-(float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])/2,
- (float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2]),
- (float)(((Math.log(this.getWidth()+this.getHeight())/5))*kolo[2])));
- }
- }
- int n = _punkty.size();
- int i = 1;
- while(i < n){
- Float[] a = _punkty.get(i-1);
- Float[] b = _punkty.get(i);
- g.drawLine((int)(this.getWidth()/2+a[0]), (int)(this.getHeight()/2+a[1]), (int)(this.getWidth()/2+b[0]), (int)(this.getHeight()/2+b[1]));
- ++i;
- }
- }
- // super.paintComponent clears offscreen pixmap,
- // since we're using double buffering by default.
- protected void clear(Graphics g) {
- super.paintComponent(g);
- }
- }
- class Start{
- public static void GUI(List<Float[]> _punkty){
- JFrame _mainWindow = new JFrame("Okienko do rysowania");
- _mainWindow.setMinimumSize(new Dimension(600,600));
- _mainWindow.setBackground(Color.white);
- _mainWindow.setContentPane(new ShapeExample(_punkty));
- _mainWindow.pack();
- _mainWindow.setVisible(true);
- }
- public static void main(String[] args) {
- Scanner wczytaj = new Scanner(System.in);
- int n=0;
- n = wczytaj.nextInt();
- List<Float[]> _punkty = new ArrayList<Float[]>();
- int i = 0;
- float a,b,c;
- while(i<n){
- a = new Float(wczytaj.next());
- b = new Float(wczytaj.next());
- c = new Float(wczytaj.next());
- Float[] x = {a,b,c};
- _punkty.add(x);
- ++i;
- }
- GUI(_punkty);
- }
- }
Add Comment
Please, Sign In to add comment