package beispiel;
import javax.swing.JFrame;
public class Beispiel
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(false);
frame.setLayout(null);
MainPanel mainPanel = new MainPanel(frame);
mainPanel.setBounds(-1000, -1000, 3000, 3000);
mainPanel.enterNewSector(0, 0);
mainPanel.start();
frame.add(mainPanel);
frame.setVisible(true);
mainPanel.requestFocus();
}
}
package beispiel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MainPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private static final byte SCROLL_MULTIPLICATOR = 10;
private static final Dimension SECTOR_SIZE = new Dimension(1000, 1000);
private static final Point[] SURROUNDING_LOCATIONS = new Point[] {
new Point(-1, -1),
new Point(0, -1),
new Point(1, -1),
new Point(-1, 0),
new Point(0, 0),
new Point(1, 0),
new Point(-1, 1),
new Point(0, 1),
new Point(1, 1),
};
private final Object LIST_SYNC = new Object();
private int onSectorX;
private int onSectorY;
private List<SectorPanel> sectorList;
private ScrollThread scrollThread;
private JFrame frame;
public MainPanel(JFrame frame)
{
super(null);
setLocation(-1000, -1000);
setSize(3000, 3000);
setLayout(null);
setBackground(Color.BLACK);
setOpaque(true);
sectorList = new ArrayList<SectorPanel>();
scrollThread = new ScrollThread();
addKeyListener(scrollThread);
this.frame = frame;
}
public Component add(Component comp)
{
try
{
addSectorPanel(comp);
}
catch (ClassCastException e) {}
finally
{
comp = super.add(comp);
}
return(comp);
}
public Component add(Component comp, int i)
{
try
{
addSectorPanel(comp);
}
catch (ClassCastException e) {}
finally
{
comp = super.add(comp, i);
}
return(comp);
}
private void addSectorPanel(Component comp) throws ClassCastException
{
SectorPanel sectorPanel = (SectorPanel) comp;
synchronized (LIST_SYNC)
{
if(!sectorList.contains(sectorPanel));
sectorList.add(sectorPanel);
}
}
public void remove(Component comp)
{
try
{
removeSectorPanel(comp);
}
catch (ClassCastException e) {}
finally
{
super.remove(comp);
}
}
private void removeSectorPanel(Component comp) throws ClassCastException
{
SectorPanel sectorPanel = (SectorPanel) comp;
synchronized (LIST_SYNC)
{
sectorList.remove(sectorPanel);
}
}
public void enterNewCoordinates(int x, int y, int centerObjectWidth, int centerObjectHeight)
{
int sectorX = xToSector(x);
int sectorY = yToSector(y);
enterNewSector(sectorX, sectorY);
setLocation(-1000, -1000);
}
public void enterNewCoordinates(int x, int y)
{
enterNewCoordinates(x, y, 0, 0);
}
public int getSectorX()
{
return(onSectorX);
}
public int getSectorY()
{
return(onSectorY);
}
public Point getLocationInSpace()
{
int x = getX();
int y = getY();
x+= 1000;
y+= 1000;
x = Math.abs(x);
y = Math.abs(y);
x+= onSectorX * 1000;
y+= onSectorY * 1000;
return(new Point(x, y));
}
public void enterNewSector(final int x, final int y)
{
Runnable runnable = new Runnable() {
public void run()
{
for(byte b = (byte) (sectorList.size() - 1); b > 0; b--)
remove(sectorList.get(b));
for(Point p:SURROUNDING_LOCATIONS)
{
SectorPanel sectorPanel = SectorPanel.getPanel(x + p.x, y + p.y);
sectorPanel.setLocation((p.x + 1) * 1000, (p.y + 1) * 1000);
add(sectorPanel);
}
}
};
if(!SwingUtilities.isEventDispatchThread())
{
try
{
SwingUtilities.invokeAndWait(runnable);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
else
runnable.run();
frame.repaint();
}
public void start()
{
scrollThread.start();
}
private static int xToSector(int xOnSpace)
{
return((xOnSpace >= 0) ?
(xOnSpace / SECTOR_SIZE.width) :
(((xOnSpace * -1) / SECTOR_SIZE.width) + 1) * -1);
}
private static int yToSector(int yOnSpace)
{
return((yOnSpace >= 0) ?
(yOnSpace / SECTOR_SIZE.height) :
(((yOnSpace * -1) / SECTOR_SIZE.height) + 1) * -1);
}
private class ScrollThread extends TimerTask implements KeyListener
{
private double moveWidth;
private double moveHeight;
private double x;
private double y;
private ScrollThread()
{
super();
}
public void run()
{
for(byte b = 0; b < SCROLL_MULTIPLICATOR; b++)
{
x+= moveWidth;
y+= moveHeight;
if(((int)x) > -1000 || ((int)x) < -2000)
{
if(isMovingLeft())
{
onSectorX--;
enterNewSector(-1000, 0);
}
else if(isMovingRight())
{
onSectorX++;
enterNewSector(1000, 0);
}
}
if(((int)y) > -1000 || ((int)y) < -2000)
{
if(isMovingUp())
{
onSectorY--;
enterNewSector(0, -1000);
}
else if(isMovingDown())
{
onSectorY++;
enterNewSector(0, 1000);
}
}
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
setLocation((int)x, (int)y);
}
});
}
}
private void enterNewSector(final int xMove, final int yMove)
{
x+= xMove;
y+= yMove;
try
{
SwingUtilities.invokeAndWait(new Runnable() {
public void run()
{
MainPanel.this.enterNewSector(onSectorX, onSectorY);
setLocation((int)x, (int)y);
}
});
}
catch (InterruptedException e) {}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
}
private boolean isMovingUp()
{
return(moveHeight > 0);
}
private boolean isMovingDown()
{
return(moveHeight < 0);
}
private boolean isMovingLeft()
{
return(moveWidth > 0);
}
private boolean isMovingRight()
{
return(moveWidth < 0);
}
public void keyPressed(KeyEvent arg0)
{
switch(arg0.getKeyCode())
{
case(KeyEvent.VK_UP):
case(KeyEvent.VK_W):
moveHeight = 1.0d;
break;
case(KeyEvent.VK_RIGHT):
case(KeyEvent.VK_D):
moveWidth = -1.0d;
break;
case(KeyEvent.VK_DOWN):
case(KeyEvent.VK_S):
moveHeight = -1.0d;
break;
case(KeyEvent.VK_LEFT):
case(KeyEvent.VK_A):
moveWidth = 1.0d;
break;
}
}
public void keyReleased(KeyEvent arg0)
{
switch(arg0.getKeyCode())
{
case(KeyEvent.VK_UP):
case(KeyEvent.VK_W):
case(KeyEvent.VK_DOWN):
case(KeyEvent.VK_S):
moveHeight = 0.0d;
break;
case(KeyEvent.VK_RIGHT):
case(KeyEvent.VK_D):
case(KeyEvent.VK_LEFT):
case(KeyEvent.VK_A):
moveWidth = 0.0d;
break;
}
}
public void keyTyped(KeyEvent arg0) {}
public synchronized void start()
{
x = getX();
y = getY();
new Timer().schedule(this, 10L, 10L);
}
}
}
package beispiel;
import java.awt.Color;
import java.awt.Point;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JPanel;
public class SectorPanel extends JPanel
{
private static final long serialVersionUID = 1L;
private static final Map<Point, SectorPanel> SECTOR_PANEL_MAP = new HashMap<Point, SectorPanel>();
private static final Lock SECTOR_PANEL_LOCK = new ReentrantLock();
SectorPanel()
{
super(null);
setSize(1000, 1000);
setFocusable(false);
Random random = new Random();
int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);
setBackground(new Color(r, g, b));
}
public static SectorPanel getPanel(int x, int y)
{
return(getPanel(new Point(x, y)));
}
public static SectorPanel getPanel(Point p)
{
SectorPanel sectorPanel = null;
SECTOR_PANEL_LOCK.lock();
try
{
sectorPanel = SECTOR_PANEL_MAP.get(p);
if(sectorPanel == null)
{
sectorPanel = new SectorPanel();
if(p.x == 0 && p.y == 0)
{
StaticPoint staticPoint = new StaticPoint(p);
staticPoint.setLocation(100, 100);
sectorPanel.add(staticPoint);
}
else if(p.x == 1 && p.y == 0)
{
StaticPoint staticPoint = new StaticPoint(p);
staticPoint.setLocation(200, 250);
sectorPanel.add(staticPoint);
}
SECTOR_PANEL_MAP.put(p, sectorPanel);
}
}
finally
{
SECTOR_PANEL_LOCK.unlock();
}
return(sectorPanel);
}
}
package beispiel;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
public class StaticPoint extends JPanel implements MouseListener
{
private static final long serialVersionUID = 1L;
private static List<StaticPoint> staticPointList;
private Point p;
static
{
staticPointList = new ArrayList<StaticPoint>();
}
public StaticPoint(Point p)
{
super();
this.p = p;
setSize(10, 10);
setBackground(Color.RED);
staticPointList.add(this);
addMouseListener(this);
}
private int getRealX()
{
return(p.x * 1000 + getX());
}
private int getRealY()
{
return(p.y * 1000 + getY());
}
public void mousePressed(MouseEvent e)
{
StaticPoint point = null;
for(StaticPoint loopPoint:staticPointList)
if(loopPoint != this)
{
point = loopPoint;
break;
}
int x = point.getRealX();
int y = point.getRealY();
int myX = getRealX();
int myY = getRealY();
double atan = Math.atan2(y - myY, x - myX);
double cosinus = Math.cos(atan);
double sinus = Math.sin(atan);
MovingPoint movingPoint = new MovingPoint();
movingPoint.setLocation(getLocation());
movingPoint.setCosinus(cosinus);
movingPoint.setSinus(sinus);
getParent().add(movingPoint);
movingPoint.start();
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
package beispiel;
import java.awt.Color;
import java.util.concurrent.TimeUnit;
import javax.swing.JPanel;
public class MovingPoint extends JPanel implements Runnable
{
private static final long serialVersionUID = 1L;
private double cosinus;
private double sinus;
private double x;
private double y;
public MovingPoint()
{
super();
setSize(10, 10);
setBackground(Color.BLUE);
}
public void run()
{
x = getX();
y = getY();
do
{
x+= cosinus;
y+= sinus;
setLocation((int)x, (int)y);
try
{
TimeUnit.MILLISECONDS.sleep(10L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
while(true);
}
public void start()
{
new Thread(this).start();
}
public void setCosinus(double c)
{
cosinus = c;
}
public void setSinus(double s)
{
sinus = s;
}
}