-
-
Save TrebuhD/320b79c08f82d83828b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JFrame; | |
import java.awt.*; | |
import javax.swing.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.beans.PropertyChangeListener; | |
import java.text.*; | |
public class Okno extends JFrame { | |
JFormattedTextField aField, vField; | |
JLabel aLabel, vLabel, degLabel, msLabel; | |
JTextArea textArea; | |
Wykres wykres; | |
JPanel panel, buttonPanel; | |
JButton okButton; | |
public Okno() { | |
super(); | |
prepareWindow(); | |
} | |
void prepareWindow() { | |
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); | |
setTitle("Rzut ukosny"); | |
panel = new JPanel(); | |
panel.setLayout(new BorderLayout()); | |
// TextFields, Buttons and labels | |
aField = new JFormattedTextField(); | |
aField.setColumns(6); | |
aField.setText("35.0"); | |
aLabel = new JLabel("Kat a: ", JLabel.CENTER); | |
aLabel.setFont(aLabel.getFont().deriveFont(Font.BOLD, 14f)); | |
degLabel = new JLabel("\u00b0", JLabel.CENTER); | |
vField = new JFormattedTextField(); | |
vField.setColumns(6); | |
vField.setText("100"); | |
vLabel = new JLabel("Predkosc v: ", JLabel.CENTER); | |
vLabel.setFont(aLabel.getFont().deriveFont(Font.BOLD, 14f)); | |
msLabel = new JLabel("m/s", JLabel.CENTER); | |
ImageIcon okButtonIcon = createImageIcon("ok.png", "ok icon"); | |
okButton = new JButton("OK", okButtonIcon); | |
okButton.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
double a = Double.parseDouble(aField.getText()); | |
double v = Double.parseDouble(vField.getText()); | |
newSimulation(a, v); | |
} | |
}); | |
// Panel stuff | |
buttonPanel = new JPanel(); | |
buttonPanel.add(aLabel); | |
buttonPanel.add(aField); | |
buttonPanel.add(degLabel); | |
buttonPanel.add(vLabel); | |
buttonPanel.add(vField); | |
buttonPanel.add(msLabel); | |
buttonPanel.add(okButton); | |
panel.add(buttonPanel, "North"); | |
wykres = new Wykres(); | |
panel.add(wykres, "Center"); | |
textArea = new JTextArea(10, 60); | |
textArea.setEditable(false); | |
panel.add(new JScrollPane(textArea), "South"); | |
setContentPane(panel); | |
} | |
protected ImageIcon createImageIcon(String path, String description) { | |
java.net.URL imgURL = getClass().getResource(path); | |
if (imgURL != null) { | |
return new ImageIcon(imgURL, description); | |
} else { | |
System.err.println("Couldn't find file: " + path); | |
return null; | |
} | |
} | |
void newSimulation(double kat, double predkoscPoczatkowa) { | |
try { | |
textArea.setText(""); | |
RzutUkosny obliczenia = new RzutUkosny(kat, predkoscPoczatkowa); | |
wykres.setObliczenia(obliczenia); | |
NumberFormat nf = NumberFormat.getInstance(); | |
nf.setMaximumFractionDigits(3); | |
textArea.setText("# Rzut Ukosny:\n\n"); | |
textArea.append("Kat: " + nf.format(obliczenia.angle) + "[rad]\n"); | |
textArea.append("Predkosc: " + nf.format(obliczenia.vel) + "[m/s]\n"); | |
textArea.append("Predkosc pozioma: " + nf.format(obliczenia.velX) + "[m/s]\n"); | |
textArea.append("Predkosc pionowa: " + nf.format(obliczenia.velY) + "[m/s]\n"); | |
textArea.append("Wysokosc maksymalna: " + nf.format(obliczenia.hMax) + "[m]\n"); | |
textArea.append("Czas wznoszenia: " + nf.format(obliczenia.tWznoszenia) + "[s]\n"); | |
textArea.append("Czas calkowity: " + nf.format(obliczenia.tCalkowity) + "[s]\n"); | |
wykres.newSimulation(obliczenia); | |
} catch (Exception e) { | |
textArea.setText("Wystapil blad!"); | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
Okno o = new Okno(); | |
o.pack(); | |
o.setLocationRelativeTo(null); | |
o.setVisible(true); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RzutUkosny { | |
final double g = 9.80665; | |
final public double angle; | |
final public double vel; | |
final public double velX, velY; | |
final public double hMax; | |
final public double tWznoszenia; | |
final public double tCalkowity; | |
public RzutUkosny(double katRzutu, double predkoscPoczatkowa) { | |
double tmp = Math.toRadians(katRzutu); | |
if (tmp < 0) { | |
tmp = 0; | |
} else if (tmp > 90) { | |
tmp = 90; | |
} | |
vel = predkoscPoczatkowa; | |
angle = tmp; | |
velX = vel * Math.cos(angle); | |
velY = vel * Math.sin(angle); | |
hMax = (velY * velY) / (2*g); | |
tWznoszenia = velY / g; | |
tCalkowity = 2 * tWznoszenia; | |
} | |
// zwraca wartosc y dla danego x | |
public double getYForX(double x) { | |
return x * Math.tan(angle) | |
- ( g / (2 * velX * velX) ) * x * x; | |
} | |
// zwraca wartosc x w chwili czasu t | |
public double getXForT(double t) { | |
return velX * t; | |
} | |
// zwraca wartosc y w chwili czasu t | |
public double getYForT(double t) { | |
return velY * t - (g/2) * t * t; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.JPanel; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.image.*; | |
import java.util.*; | |
import java.util.Timer; | |
import java.text.*; | |
public class Wykres extends JPanel { | |
final int marginX = 50; | |
final int marginY = 50; | |
int screenWidth = 800; | |
int screenHeight = 600; | |
double currentTime = 0; | |
double timeStep = 0.05; | |
double imageScale = 1; | |
RzutUkosny obliczenia = null; | |
public void setObliczenia(RzutUkosny obliczenia) { | |
this.obliczenia = obliczenia; | |
} | |
BufferedImage image = null; | |
public Wykres() { | |
image = newImage(screenWidth, screenHeight); | |
drawAxesAndLegend(image); | |
setPreferredSize(new Dimension(screenWidth, screenHeight)); | |
} | |
public void paintComponent(Graphics g) { | |
g.drawImage(image, 0, 0, null); | |
} | |
BufferedImage newImage(int w, int h) { | |
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); | |
img.createGraphics(); | |
return img; | |
} | |
void drawAxesAndLegend(BufferedImage img) { | |
Graphics2D g = (Graphics2D) img.getGraphics(); | |
g.setColor(Color.LIGHT_GRAY); | |
g.fillRect(0, 0, img.getWidth(), img.getHeight()); | |
g.setColor(Color.black); | |
g.drawLine(marginX, screenHeight - marginY, screenWidth - marginX, screenHeight - marginY); | |
g.drawLine(marginX, marginY, marginX, screenHeight - marginY); | |
g.drawLine(marginX - 10, marginY, marginX + 10, marginY); | |
g.drawLine(screenWidth - marginX, screenHeight - marginY - 10, screenWidth - marginX, screenHeight - marginY + 10); | |
NumberFormat nf = NumberFormat.getInstance(); | |
nf.setMaximumFractionDigits(2); | |
g.drawString(nf.format(imageScale * (screenHeight - 2 * marginY)) + " [m]", marginX, marginY - 10); | |
g.drawString(nf.format(imageScale * (screenWidth - 2 * marginX)) + " [m]", screenWidth - 2 * marginX, screenHeight - marginY / 2); | |
g.dispose(); | |
} | |
public void newSimulation(RzutUkosny obliczenia) { | |
this.obliczenia = obliczenia; | |
scaleImage(); | |
image = newImage(screenWidth, screenHeight); | |
drawAxesAndLegend(image); | |
currentTime = 0; | |
repaint(); | |
draw(); | |
} | |
public void draw() { | |
Graphics2D g = (Graphics2D) image.getGraphics(); | |
g.setColor(Color.BLUE); | |
for (double screenX = 0; screenX <= screenWidth - (marginX * 2); screenX += 1.0) { | |
System.out.println("x: " + screenX + "y: " + obliczenia.getYForX(screenX)); | |
g.drawRect((int)screenX + marginX, - (int) obliczenia.getYForX(screenX) + (screenHeight - marginY), 3, 3); | |
} | |
g.dispose(); | |
} | |
void scaleImage() { | |
imageScale = 1 * (obliczenia.hMax / screenWidth - 100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment