Created
March 19, 2019 01:55
-
-
Save uvlad7/87b4465b4b4ef552c6d78d053cbc20de to your computer and use it in GitHub Desktop.
УП 3.2
This file contains 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 Main { | |
public static void main(String[] args) { | |
var view = new View(); | |
} | |
} |
This file contains 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.*; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
public class PaintPanel extends JPanel { | |
private BufferedImage bufferedImage; | |
public PaintPanel(int width, int height) { | |
setPreferredSize(new Dimension(width, height)); | |
} | |
@Override | |
public void setPreferredSize(Dimension dimension) { | |
super.setPreferredSize(dimension); | |
bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_INT_ARGB); | |
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics(); | |
graphics.setBackground(new Color(1, 1, 1, 0f)); | |
graphics.clearRect(0, 0, dimension.width, dimension.height); | |
graphics.dispose(); | |
} | |
@Override | |
protected void paintComponent(Graphics graphics) { | |
graphics.drawImage(bufferedImage, 0, 0, null); | |
} | |
public BufferedImage getBuffer() { | |
return bufferedImage; | |
} | |
public void loadImage(BufferedImage buf) { | |
bufferedImage.createGraphics().setColor(Color.WHITE); | |
bufferedImage.createGraphics().fillRect(0, 0, getWidth(), getHeight()); | |
bufferedImage.createGraphics().drawImage(buf, 0, 0, null); | |
repaint(); | |
} | |
} |
This file contains 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 javafx.scene.layout.Pane; | |
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ComponentAdapter; | |
import java.awt.event.ComponentEvent; | |
import java.io.File; | |
import java.io.IOException; | |
public class View extends JFrame { | |
private JComboBox<String> directionBox; | |
private boolean direction; | |
private int centerX; | |
private int centerY; | |
private JSlider speedSlider; | |
private double speed; | |
private double angle; | |
private JPanel field; | |
private Timer timer; | |
private PaintPanel element; | |
public View() { | |
super("Вращение"); | |
try { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { | |
} | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setLayout(new BorderLayout()); | |
field = new JPanel(true); | |
field.setBackground(Color.GRAY); | |
element = new PaintPanel(40, 40); | |
element.loadImage(ImageIO.read(new File("src\\ball.png"))); | |
field.add(element); | |
field.setLayout(null); | |
add(field, BorderLayout.CENTER); | |
JPanel controls = new JPanel(); | |
controls.setLayout(new BoxLayout(controls, BoxLayout.LINE_AXIS)); | |
directionBox = new JComboBox<>(new String[]{"По часовой", "Против часовой"}); | |
directionBox.setSelectedIndex(0); | |
directionBox.addActionListener(e -> direction = (directionBox.getSelectedIndex() == 0)); | |
direction = true; | |
speedSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50); | |
speedSlider.addChangeListener(e -> speed = speedSlider.getValue()); | |
speed = 50; | |
angle = 0; | |
controls.add(directionBox); | |
controls.add(speedSlider); | |
add(controls, BorderLayout.SOUTH); | |
pack(); | |
timer = new Timer(10, e -> { | |
angle += (direction ? 1 : -1) * speed / (50 * Math.min(centerX, centerY)); | |
move();} | |
); | |
setSize(new Dimension(620, 650)); | |
setMinimumSize(new Dimension(220, 250)); | |
setLocationRelativeTo(null); | |
field.addComponentListener(new ComponentAdapter() { | |
@Override | |
public void componentResized(ComponentEvent e) { | |
centerY = field.getHeight() / 2; | |
centerX = field.getWidth() / 2; | |
move(); | |
} | |
}); | |
centerY = field.getHeight() / 2; | |
centerX = field.getWidth() / 2; | |
move(); | |
timer.start(); | |
setVisible(true); | |
} catch (IOException e) { | |
e.printStackTrace(System.err); | |
} | |
} | |
private void move() { | |
element.setBounds(centerX - 20 + (int) Math.round(Math.min(centerX - 20, centerY - 20) * Math.sin(angle)), | |
centerY - 20 - (int) Math.round(Math.min(centerX - 20, centerY - 20) * Math.cos(angle)), 40, 40); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment