Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.FlowLayout;
- import java.awt.Graphics;
- import javax.swing.BorderFactory;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.SwingUtilities;
- public class Teste extends JFrame {
- JPanel jp;
- int dx = 175, dy = 125; //posicoes
- int vx, vy; //velocidades
- public Teste() {
- super("Joguinho");
- Container c = getContentPane();
- c.setLayout(new FlowLayout());
- jp = new JPanel();
- jp.setBackground(Color.black);
- jp.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
- jp.setPreferredSize(new Dimension(380, 250));
- c.add(jp);
- new Update().start();
- setSize(400, 300);
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.setColor(Color.RED);
- g.fillOval(dx, dy, 50, 50);
- }
- private void verificaColisao() {
- if(dx < 0) {
- System.out.println("bateu");
- }
- }
- public class Update extends Thread {
- @Override
- public void run() {
- while(true) {
- try {
- Thread.sleep(400);
- dx -= 20;
- verificaColisao();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- repaint();
- }
- }
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- new Teste().setVisible(true);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement