[Java] Wątki + grafika 2D

wehcazraw
Użytkownik
Użytkownik
Posty: 1
Rejestracja: 27 mar 2013, o 13:56
Płeć: Mężczyzna
Lokalizacja: Ziemia

[Java] Wątki + grafika 2D

Post autor: wehcazraw »

Cześć. Zrobiłem prostą aplikację i działała, ale jednak muszę do niej dodać wątki. I tu się zaczął problem, bo są błędy, a nie mam pojęcia co jest źle. Prosiłbym o pomoc.

Wg Eclipse błąd jest przy linii 118.
Tutaj screen z debugera:

Kod: Zaznacz cały

package Pakiet;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Line2D;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.*;

public class Board extends JPanel implements Runnable {

	public Car c;
	public Sprites p;
	public Meta m, m1, m2, m3;
	public Image img;
	public Image still2;
	Timer time;
	public boolean running = false;
	private JFrame frame;

	public Board() {
		// time = new Timer(4, this);
		// time.start();
		frame = new JFrame();
		// JFrame frame = new JFrame("Gra");
		frame.setLayout(new BorderLayout());
		frame.add(this);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.setVisible(true);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);

		JMenuBar menubar = new JMenuBar();
		frame.setJMenuBar(menubar);

		JMenu file = new JMenu("Plik");
		menubar.add(file);
		JMenuItem exit = new JMenuItem("Zakoncz");
		file.add(exit);
		JMenu help = new JMenu("Pomoc");
		menubar.add(help);
		final JMenuItem about = new JMenuItem("O programie");
		help.add(about);

		class exitaction implements ActionListener {

			public void actionPerformed(ActionEvent e2) {
				System.exit(0);
			}

		}

		class programaction implements ActionListener {

			public void actionPerformed(ActionEvent e3) {
				JOptionPane.showMessageDialog(about, "Gra");
			}

		}
		exit.addActionListener(new exitaction());
		about.addActionListener(new programaction());
	}

	public void init() {

		ImageIcon i = new ImageIcon("E:/back.png");
		img = i.getImage();
		addKeyListener(new AL());
		setFocusable(true);
		c = new Car();

		m = new Meta(-110, 230);
		m1 = new Meta(300, 480);
		m2 = new Meta(-110, 720);
		m3 = new Meta(100, 820);

	}

	public synchronized void start() {
		running = true;
		new Thread(this).start();
	}

	public synchronized void stop() {
		running = false;

	}

	public void run() {

		init();
		repaint();

		while (running) {

			checkCollisions();
			c.move();

			try {
				Thread.sleep(1);

			} catch (InterruptedException e) {
			}
		}

	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		Graphics2D g2d = (Graphics2D) g;

		g2d.drawImage(img, 600 - c.nx2, 400 - c.ny2, null); // BACKGROUND
		g2d.drawImage(c.getImage(), 300, 100, null);

	}

	public void checkCollisions() {

		Rectangle r1 = c.getBounds();
		Rectangle r2 = m.getBounds();
		Rectangle r3 = m1.getBounds();
		Rectangle r4 = m2.getBounds3();
		Rectangle r5 = m3.getBounds4();

		if (r1.intersects(r2)) {
			if ((c.still == c.i3.getImage() && c.movement_down == true))
				c.dy = 0;
			else
				c.dy = 1;
			if ((c.still == c.i4.getImage() && c.movement_up == true))
				c.dy = 0;
			else
				c.dy = -1;

		}

		if (r1.intersects(r5)) {
			c.dx = 0;
			JOptionPane.showMessageDialog(null, "KONIEC GRY!");
			System.exit(0);

		}

	}

	private class AL extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			c.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			c.keyPressed(e);
		}
	}

	public static void main(String[] args) {
		new Board().start();
	}

}
ODPOWIEDZ