package com.company;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;
import java.util.TreeSet;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static sun.misc.PostVMInitHook.run;
public class Main {
public static void main(String[] args) {
Gui app = new Gui();
app.buildGui();
}
}
class Gui extends JPanel implements ActionListener {
JFrame window = new JFrame();
// Gui z = new Gui();
JButton exitButton = new JButton();
JButton pushButton = new JButton();
JLabel weightN = new JLabel("Masa");
JLabel speedN = new JLabel("Szybkosc");
JLabel countN = new JLabel("Kat");
static public JTextField weight = new JTextField(5);
static public JTextField speed = new JTextField(5);
static public JTextField count = new JTextField(5);
public void buildGui() {
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(600, 100);
window.setTitle("Algortym łucznika");
exitButton = new JButton("Koniec");
pushButton = new JButton("Strzel");
pushButton.addActionListener(this);
exitButton.addActionListener(this);
FlowLayout cp = new FlowLayout(FlowLayout.LEFT);
window.setLayout(cp);
window.getContentPane().add(pushButton);
window.getContentPane().add(weightN);
window.getContentPane().add(weight);
window.getContentPane().add(speedN);
window.getContentPane().add(speed);
window.getContentPane().add(countN);
window.getContentPane().add(count);
window.getContentPane().add(exitButton);
window.setVisible(true);
}
static public JTextField x = new JTextField(10);
public void buildGui2() {
window = new JFrame();
JPanel anel = new JPanel();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(1300, 800);
window.setTitle("Algortym łucznika");
anel.add(new GuiShow());
JScrollPane pa = new JScrollPane(anel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
window.getContentPane().add(pa);
window.setVisible(true);
}
public void buildGui3() {
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(400, 100);
window.setTitle("Algortym łucznika");
FlowLayout cp = new FlowLayout(FlowLayout.LEFT);
window.setLayout(cp);
GuiShow guiNew = new GuiShow();
double ex = guiNew.sim();
String ex2 = String.valueOf(ex);
JLabel y = new JLabel("Czas");
JTextField x = new JTextField(ex2);
window.getContentPane().add(y);
window.getContentPane().add(x);
window.setVisible(true);
}
public String getWeightDigitID(){
return weight.getText();
}
public String getSpeedDigitID(){
return speed.getText();
}
public String getCountDigitID(){
return count.getText();
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == exitButton) {
System.exit(0);
}
if (source == pushButton) {
getWeightDigitID();
getSpeedDigitID();
getCountDigitID();
buildGui2();
}
}
}
class GuiShow extends JPanel implements Runnable {
private Thread runner = null;
double x = 0;
double y = 0;
double startX = x;
double startY = y;
double endX=10, endY=550;
double R = 3.1415926 / 180.0;
double vx, vy, t = 0.0;
double dt = 0.003;
double eps = 0.01;
double c = 0.7;
double g = 9.81;
double m;
double angle=50;
//
// public GuiShow(){
// JPanel gamePanel = new JPanel();
// gamePanel.setBackground(Color.green);
// gamePanel.setPreferredSize(new Dimension(500,300));
//
// JPanel infoPanel = new JPanel();
// infoPanel.setBackground(Color.WHITE);
//
// infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
// infoPanel.setPreferredSize(new Dimension(100, 400));
//
// add(gamePanel, BorderLayout.CENTER);
//
//
// }
@Override
public void paintComponent(final Graphics p) {
super.paintComponent(p);
Graphics2D g2d = (Graphics2D) p;
vx= alfaVX(speed(),count());
vy= alfaVY(speed(),count());
m = weight();
double r = c / m;
int n = 0;
Thread ten=Thread.currentThread();
while (y > -eps) {
System.out.println(x + " " + y + " " + t);
vx = vx - r * vx * dt;
vy = vy - g * dt - r * vy * dt;
x = x + vx * dt - 0.5 * r * vx * dt * dt;
y = y + vy * dt - 0.5 * g * dt * dt + 0.5 * r * vy * dt * dt;
double tmp = y;
y=-y;
t = t + dt;
n = n + 1;
g2d.drawOval((int)x+5,(int)y+650,3,3);
y=tmp;
}
runner=null;
}
double speed(){
Gui s = new Gui();
String x = s.getSpeedDigitID();
double v=Double.parseDouble(s.getSpeedDigitID());
return v;
}
double weight(){
Gui s = new Gui();
String x = s.getWeightDigitID();
double v=Double.parseDouble(s.getWeightDigitID());
return v;
}
double count(){
Gui s = new Gui();
String x = s.getCountDigitID();
double v=Double.parseDouble(s.getCountDigitID());
return v;
}
double alfaVX(double v, double alfa) {
double co = cos(R * alfa);
vx = v * co;
return vx;
}
double alfaVY(double v, double alfa) {
double si = sin(R * alfa);
vy = v * si;
return vy;
}
// public void draw(){
// if (runner==null)
// {
// runner=new Thread(this);
// runner.start();
// }
// }
public void run(){
}
public double sim(){
vx= alfaVX(speed(),count());
vy= alfaVY(speed(),count());
m = weight();
double r = c / m;
int n = 0;
while (y > -eps) {
vx = vx - r * vx * dt;
vy = vy - g * dt - r * vy * dt;
x = x + vx * dt - 0.5 * r * vx * dt * dt;
y = y + vy * dt - 0.5 * g * dt * dt + 0.5 * r * vy * dt * dt;
t = t + dt;
n = n + 1;
}
return t;
}
}
Witam. Mam problem z JScrollPane. Kiedy pokaże się już obiekt, który narysowałem to nie mogę użyć suwaków by przejść dalej go zobaczyć. Dlaczego tak sie dzieje? Jest jakieś rozwiązanie tego problemu?