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.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.TreeSet;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
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();
JButton startButton = new JButton();
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");
startButton = new JButton("Odswież");
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(startButton);
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();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(1200, 1000);
window.setTitle("Algortym łucznika");
GuiShow guiNew = new GuiShow();
window.add(guiNew);
JButton button = new JButton("Startuj");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
guiNew.draw();
}
});
window.add(button, BorderLayout.SOUTH);
// window.add(x,BorderLayout.NORTH);
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 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;
@Override
public void paintComponent(Graphics p) {
BufferedImage arrow = LoadImage("C:\\Users\\Pawel Celuch\\Desktop\\arrow.png");
super.paintComponent(p);
Graphics2D g2d = (Graphics2D) p;
g2d.drawImage(arrow, (int)x, (int)y+550, this);
}
BufferedImage LoadImage(String Filename){
BufferedImage img = null;
try{
img = ImageIO.read(new File(Filename));
} catch (IOException e){
}
return img;
}
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 = Math.cos(R * alfa);
vx = v * co;
return vx;
}
double alfaVY(double v, double alfa) {
double si = Math.sin(R * alfa);
vy = v * si;
return vy;
}
public void draw(){
if (runner==null)
{
runner=new Thread(this);
runner.start();
}
}
public void run(){
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;
try{
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
y=tmp;
}
runner=null;
}
}
Witam. Mam problem z obracaniem się obrazka. Próbowałem używać funkcji rotate, ale gdy używam jej to znika mi obrazek. Nie mam pojęcia jak zrobić by po każdej iteracji pętli zmieniał się kąt nachylenia strzały.