Java NullPointerException (need some timer) - java

I draw simple splines and I got code here. I editted code and receive NullPointerException in dataset.size(). I guess that my programm don't be at time filling all dataset points from files (Scanner works slowly) and it throws this error. I also guess that I need to add certain timer to wait while dataset == null. But how to make it?
public class SimpleGrapher2 extends JPanel {
...
private static List<Double> scores;
private static File[] sFiles;
...
private static List<List<Point2D.Double>> dataset;
private static int snumber = 0;
public SimpleGrapher2(List<Double> scores) {
SimpleGrapher2.scores = scores;
addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent me) {
double x = me.getX();
double y = me.getY();
List<Point2D.Double> series = findNearPoint(dataset, x, y);
editSerie(x, y, series);
revalidate();
repaint();
}
#Override
public void mouseMoved(MouseEvent me) {}
});
}
static JPanel paintingComponent = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//painting X,Y axises
drawSplines(g2); // painting splines
}
};
public static void drawSplines(Graphics2D g2) {
int precision = 10;
for (int k = 0; k < dataset.size(); k++) { // NULLPOINTEREXCEPTION HERE
List<Point2D.Double> series = dataset.get(k);
int np = series.size();
//algorithm and painting of splines
}
}
public List<Point2D.Double> findNearPoint(List<List<Point2D.Double>> dataset, double x, double y) {
//hidden part of code: to move points
}
public void editSerie(double x, double y, List<Point2D.Double> serie) {
//hidden part of code: edit series in case of MouseDragged
}
public void readFromFiles() {
dataset = new ArrayList<>();
for (File polFile : sFiles) {
List<Point2D.Double> series = new ArrayList<>();
Scanner s = null;
try {
s = new Scanner(new File(polFile.getAbsolutePath()));
}catch (FileNotFoundException ex) {
System.out.println("Scanner error!");
}
s.useLocale(Locale.US);
while (s.hasNext()) {
double x = s.nextDouble();
double y = s.nextDouble();
series.add(new Point2D.Double(x, y));
}
dataset.add(series);
}
}
//hidden part of code: some helpfull functions
private static void createAndShowGui() {
...
SimpleGrapher2 mainPanel = new SimpleGrapher2(scores);
mainPanel.setPreferredSize(new Dimension(800, 600));
JFrame frame = new JFrame("DrawGraph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
//frame.setContentPane(new GLG2DCanvas(paintingComponent));
frame.setContentPane(paintingComponent);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGui();
}
});
}
}

Related

How to create multiple falling down objects by using threads in java?

** Here i created only one moving object ,i want to create more objects which falls down and has random X coordinate .i know i should implement runnable and i should create squres then store them in a collection but its really hard for me to merge everything.i also might have done some mistakes btw . Could you help me some ? **
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
public class Project3 extends JFrame {
public Project3(){
super("Game");
setSize(600,600);
add(new Game(600,600));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Project3());
}
class Squares{
public int x;
public int y;
public Squares(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
class Game extends JPanel implements ActionListener {
private int score;
private java.util.List<Squares> shapeList=new ArrayList<>();
private boolean play ;
private int X=50;
private int Y=0;
Timer timer=new Timer(10,this);
public Game(int w , int h){
Dimension d = new Dimension(w, h);
setBackground(Color.BLUE);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.fillRect(X,Y,60,60);
timer.start();
}
#Override
public void actionPerformed(ActionEvent e) {
Y=Y+5;
repaint();
if(Y==600){
Random random=new Random();
Y=0;
X=random.nextInt(600-60);
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Project3 extends JFrame {
public Project3() {
super("Game");
setSize(600, 600);
add(new Game(600,600));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Project3());
}
class Square {
public int squareX;
public int squareY;
int squareW = 25;
int squareH = 25;
public Square(int X, int Y) {
this.squareX = X;
this.squareY = Y;
}
public int getSquareX() {
return squareX;
}
public void setSquareX(int X) {
this.squareX = X;
}
public int getSquareY() {
return squareY;
}
public void setSquareY(int Y) {
this.squareY = Y;
}
}
class Game extends JPanel implements ActionListener ,Runnable,MouseListener {
public int score;
private java.util.List<Square> shapeList = new ArrayList<>();
Timer timer = new Timer(10, this);
private boolean play= true;
public Game(int w, int h) {
Dimension d = new Dimension(600, 600);
setBackground(Color.BLUE);
add(new Label("SCORE...."),BorderLayout.PAGE_END);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.YELLOW);
for (Square s:shapeList) {
g.fillRect(s.squareX,s.squareY,25,25);
}
}
#Override
public void actionPerformed(ActionEvent e) {
for (Square k : shapeList) {
k.setSquareY(k.getSquareY() + 5);
repaint();
}
}
public void stop() {
play = false;
}
#Override
public void run() {
while(play){
int randomNumber=(int)(Math.random()*600)+1;
shapeList.add(new Square(randomNumber,0));
for (Square k : shapeList) {
if (k.getSquareY()== 600) {
stop();
}try {
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
}
}
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mousePressed(MouseEvent e) {
int mouseX=e.getX();
int mouseY=e.getY();
for (Square s:shapeList){
if ((mouseX > s.squareX) && (mouseX < s.squareX + s.squareW) && (mouseY > s.squareY) && (mouseY < s.squareY + s.squareH)) {
shapeList.remove(s);
score++;
}
}
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
}
This code produces three layers of 'snow flakes' which drift towards the bottom of the screen.
Have a look over it, for tips:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Random;
public class AnimatedSnowFall {
private JComponent ui = null;
AnimatedSnowFall() {
initUI();
}
public final void initUI() {
if (ui != null) {
return;
}
ui = new JPanel(new BorderLayout(4, 4));
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
ui.add(new SnowFall());
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = () -> {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
AnimatedSnowFall o = new AnimatedSnowFall();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
};
SwingUtilities.invokeLater(r);
}
}
class SnowFall extends JPanel implements ActionListener {
Dimension prefSize = new Dimension(1600, 900);
SnowFlake[] farFlakes = new SnowFlake[200];
SnowFlake[] midFlakes = new SnowFlake[150];
SnowFlake[] closeFlakes = new SnowFlake[75];
Color farColor = new Color(100,100,255);
Color midColor = new Color(150,150,255);
Color closeColor = new Color(255,255,255);
SnowFall() {
setBackground(Color.BLACK);
for (int ii = 0; ii < farFlakes.length; ii++) {
farFlakes[ii] = new SnowFlake(prefSize.width, prefSize.height, 2, 4);
}
for (int ii = 0; ii < midFlakes.length; ii++) {
midFlakes[ii] = new SnowFlake(prefSize.width, prefSize.height, 3, 6);
}
for (int ii = 0; ii < closeFlakes.length; ii++) {
closeFlakes[ii] = new SnowFlake(prefSize.width, prefSize.height, 4, 8);
}
Timer timer = new Timer(50, this);
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(farColor);
for (SnowFlake snowFlake : farFlakes) {
snowFlake.draw(g);
}
g.setColor(midColor);
for (SnowFlake snowFlake : midFlakes) {
snowFlake.draw(g);
}
g.setColor(closeColor);
for (SnowFlake snowFlake : closeFlakes) {
snowFlake.draw(g);
}
}
#Override
public Dimension getPreferredSize() {
return prefSize;
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
}
class SnowFlake {
int w;
int h;
int x;
int y;
int size;
int speed;
static Random r = new Random();
SnowFlake(int w, int h, int size, int speed) {
this.w = w;
this.h = h;
x = r.nextInt(w);
y = r.nextInt(h);
this.size = size;
this.speed = speed;
}
public void draw(Graphics g) {
y += speed;
if (y > h) {
x = r.nextInt(w);
y = 0;
}
g.fillOval(x, y, size, size);
}
}

Repaint() not being called if i use ImageIO.read()

When i try to set value to BufferedImage called dinoImage in Dino.java in a constructor i just get a blank screen every time (second picture) because repaint() is not being called, but if i set it to null it is working just fine but without this image (first picture).
No exceptions, everything seems fine in this code, this problem appears when i try to set value to this field using static method getImage of Resource.java which uses this line of code ImageIO.read(new File(path)) and it causes that repaint() is not being called, i guess this line causes such weird behavior but i dont know how to solve it.
Main.java
public class Main {
public static void main(String[] args) {
GameWindow gameWindow = new GameWindow();
gameWindow.startGame();
}
}
GameWindow.java
public class GameWindow extends JFrame {
private GameScreen gameScreen;
public GameWindow() {
super("Runner");
setSize(1000, 500);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameScreen = new GameScreen();
add(gameScreen);
}
public void startGame() {
gameScreen.startThread();
}
}
GameScreen.java
public class GameScreen extends JPanel implements Runnable, KeyListener {
private Thread thread;
public static final double GRAVITY = 0.1;
public static final int GROUND_Y = 300;
private Dino dino;
public GameScreen() {
thread = new Thread(this);
dino = new Dino();
}
public void startThread() {
thread.start();
}
#Override
public void run() {
while(true) {
try {
Thread.sleep(20);
dino.updatePosition();
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// g.clearRect(0, 0, getWidth(), getHeight());
g.setColor(Color.RED);
g.drawLine(0, GROUND_Y, getWidth(), GROUND_Y);
dino.draw(g);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed");
dino.jump();
}
#Override
public void keyReleased(KeyEvent e) {
System.out.println("Key Released");
}
}
Dino.java
public class Dino {
private double x = 100;
private double y = 100;
private double speedY = 0;
private BufferedImage dinoImage;
public Dino() {
dinoImage = getImage("data/dino.png");
}
public void updatePosition() {
if(y + speedY >= GROUND_Y - 100) {
speedY = 0;
y = GROUND_Y - 100;
} else {
speedY += GRAVITY;
y += speedY;
}
}
public void jump() {
if(y == GROUND_Y - 100) {
speedY = -5;
y += speedY;
}
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect((int)x, (int)y, 100, 100);
g.drawImage(dinoImage, (int)x, (int)y, null);
}
}
Resource.java
public class Resource {
public static BufferedImage getImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
}
setSize(1000, 500);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gameScreen = new GameScreen();
add(gameScreen);
Swing components need to be added to the frame BEFORE the frame is made visible. Otherwise the panel has a size of (0, 0) and there is nothing to paint.
The code should be something like:
gameScreen = new GameScreen();
add(gameScreen);
setSize(1000, 500);
setVisible(true);

How to animate a triangle to move forward in Java Swing

I'm creating a simulation of the Boid algorithm in Swing (Graphics2d library), so I want to animate many shapes (triangles). Right now I'm learning how to move them around and have run into a problem with fps and repainting.
The fps drop significantly when I put more than two triangles on the screen (say, 10), and I really don't know where the problem lies. Here's the code (I omitted the libraries):
public class Program {
/**
* #param args
*/
public static void main(String[] args) {
World world = World.getInstance();
// Initialise and show world
JFrame f = new JFrame();
f.setSize(world.getBoundsX(),world.getBoundsY());
f.setTitle("Boids Alive");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(world);
f.setResizable(false);
f.setVisible(true);
for(int i = 0 ; i < 10; i++)
world.createBoid();
while(true){
//adjust all behaviours
for(Boid b : world.boids){
b.move(0, -2);
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
world.refreshAll();
}
}
public final class World extends JPanel {
private static final long serialVersionUID = 1L;
private Random r = new Random();
private static final int boundsX = 800;
private static final int boundsY = 600;
public ArrayList<Boid> boids = new ArrayList<Boid>();
public ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>();
private static World instance = null;
private World() {
this.setBackground(Color.white);
}
public static World getInstance(){
if(instance == null)
instance = new World();
return instance;
}
public int getBoundsX() { return boundsX; }
public int getBoundsY() { return boundsY; }
public void createBoid(){
Boid b = new Boid() ;
b.setX(r.nextInt(790));
b.setY(r.nextInt(590));
boids.add(b);
b.start() ;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
public void refreshAll() {
for(Boid b : boids) {
b.graphicEle.refreshLocation();
}
}
}
public class Boid extends WorldObject{
public Boid() {
graphicEle = new GraphicElement(this);
World world = World.getInstance() ;
world.add(graphicEle) ;
world.repaint() ;
world.validate() ;
}
public void move(int x, int y) {
this.x += x;
this.y += y;
checkBounds();
try {
sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void checkBounds() {
World world = World.getInstance() ;
if(this.x < 0) x = world.getBoundsX();
if(this.x > world.getBoundsX()) x = 0;
if(this.y < -20) y = world.getBoundsY(); //TODO: get the actual size
if(this.y > world.getBoundsY()) y = 0;
}
}
public class GraphicElement extends JPanel {
private static final long serialVersionUID = 1L;
private WorldObject owner;
public GraphicElement(WorldObject obj) {
owner = obj ;
setPreferredSize(new Dimension(20, 20));
setOpaque(false);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create(); //TODO Move to boid class
g2d.setColor(Color.orange);
Path2D.Double triangle = new Path2D.Double();
triangle.moveTo(0, 20);
triangle.lineTo(10, 0);
triangle.lineTo(20, 20);
triangle.closePath();
g2d.fill(triangle);
g2d.dispose();
}
protected void refreshLocation() {
setLocation(owner.getX(), owner.getY());
}
}
Any ideas what I am doing wrong? Should I use translate() instead of setLocation()?

Key Ghosting Problems

I am making a game and I ran into a key ghosting problem (where the program only detects one keypress at a time, so the player can't go diagonally). I was watching this tutorial: https://www.youtube.com/watch?v=5UaEUrbpDPE
I followed everything they said and it still only detects one key at a time.
Main:
public class Main extends JApplet implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static int width = 900;
public static int height = 600;
public static int fps = 60;
public static Main instance;
public static Ailoid ailoid = new Ailoid();
public static Player player = new Player();
// Initialize
public void init() {
setSize(width, height);
setBackground(Color.white);
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
requestFocus();
instance = this;
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
player.setLocation(new Location(400, 400));
}
// Paint graphics
public void paint(Graphics g) {
super.paint(g);
paintComponent(g);
}
public void paintComponent(Graphics g) {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
g.setColor(Color.GREEN);
g.fillRect(loc.getX(), loc.getY(), 10, 25);
}
g.setColor(Color.BLUE);
Location loc = Main.player.getLocation();
g.fillRect(loc.getX(), loc.getY(), 10, 25);
}
// Thread start
#Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}
// Thread stop
#Override
public void destroy() {
}
// Thread run
#Override
public void run() {
Thread thread = new Thread(this);
while (thread != null) {
Updater.run();
repaint();
try {
// 1000 divided by fps to get frames per second
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void keyPressed(KeyEvent evt) {
if (!KeysDown.get().contains(evt.getKeyCode()))
KeysDown.add(new Integer(evt.getKeyCode()));
KeyPress.run(evt);
}
#Override
public void keyReleased(KeyEvent evt) {
KeysDown.remove(new Integer(evt.getKeyCode()));
}
#Override
public void keyTyped(KeyEvent evt) {
}
}
KeysDown:
public class KeysDown {
private static ArrayList<Integer> keysDown = new ArrayList<Integer>();
public static ArrayList<Integer> get() {
return keysDown;
}
public static void add(Integer key) {
keysDown.add(key);
}
public static void remove(Integer key) {
keysDown.remove(key);
}
}
KeyPress:
public class KeyPress {
public static void run(KeyEvent evt) {
if (KeysDown.get().contains(KeyEvent.VK_RIGHT)) {
Main.player.moveRight();
}
else if (KeysDown.get().contains(KeyEvent.VK_LEFT)) {
Main.player.moveLeft();
}
else if (KeysDown.get().contains(KeyEvent.VK_DOWN)) {
Main.player.moveDown();
}
else if (KeysDown.get().contains(KeyEvent.VK_UP)) {
Main.player.moveUp();
}
}
}
Thank you!
Again as I have mentioned in previous comments:
Don't draw directly on the JApplet or in any top-level window.
If you give your applet a paintComponent method, it won't override any applet methods and won't gain the benefit of double buffering.
Instead draw in the paintComponent method of a JPanel, an thereby gain the benefit of double buffering.
Use Key Bindings not a KeyListener.
Also, I like to use a Swing Timer for simple game loops.
In the code below, I use an enum called Direction to encapsulate the idea of directions on the screen.
I then put the for Directions together with Boolean.FALSE in a Map<Direction, Boolean> called dirMap.
I run a Swing Timer continuously, polling the state of this Map and move a sprite based on the state of Booleans held by the dirMap.
I change the state of the map held values in my Key Binding Actions. So if the down key is pressed, its action will change the Direction.DOWN associated value in the Map to true, and when released, the Direction.DOWN associated value will be changed back to false.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.EnumMap;
import javax.imageio.ImageIO;
import javax.swing.*;
#SuppressWarnings("serial")
public class AnimateExample extends JPanel {
public static final String DUKE_IMG_PATH = // https://duke.kenai.com/iconSized/duke.gif
"https://duke.kenai.com/iconSized/duke4.gif";
private static final int PREF_W = 800;
private static final int PREF_H = 800;
private static final int TIMER_DELAY = 20;
private static final String KEY_DOWN = "key down";
private static final String KEY_RELEASE = "key release";
public static final int TRANSLATE_SCALE = 3;
private static final String BACKGROUND_STRING = "Use Arrow Keys to Move Image";
private static final Font BG_STRING_FONT = new Font(Font.SANS_SERIF,
Font.BOLD, 32);
private EnumMap<Direction, Boolean> dirMap =
new EnumMap<AnimateExample.Direction, Boolean>(Direction.class);
private BufferedImage image = null;
private int imgX = 0;
private int imgY = 0;
private int bgStringX;
private int bgStringY;
public AnimateExample() {
for (Direction dir : Direction.values()) {
dirMap.put(dir, Boolean.FALSE);
}
try {
URL imgUrl = new URL(DUKE_IMG_PATH);
image = ImageIO.read(imgUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
new Timer(TIMER_DELAY, new TimerListener()).start();
// here we set up our key bindings
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition);
ActionMap actionMap = getActionMap();
for (final Direction dir : Direction.values()) {
// for the key down key stroke
KeyStroke keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0,
false);
inputMap.put(keyStroke, dir.name() + KEY_DOWN);
actionMap.put(dir.name() + KEY_DOWN, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
dirMap.put(dir, true);
}
});
// for the key release key stroke
keyStroke = KeyStroke.getKeyStroke(dir.getKeyCode(), 0, true);
inputMap.put(keyStroke, dir.name() + KEY_RELEASE);
actionMap.put(dir.name() + KEY_RELEASE, new AbstractAction() {
#Override
public void actionPerformed(ActionEvent arg0) {
dirMap.put(dir, false);
}
});
}
FontMetrics fontMetrics = getFontMetrics(BG_STRING_FONT);
int w = fontMetrics.stringWidth(BACKGROUND_STRING);
int h = fontMetrics.getHeight();
bgStringX = (PREF_W - w) / 2;
bgStringY = (PREF_H - h) / 2;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g.setFont(BG_STRING_FONT);
g.setColor(Color.LIGHT_GRAY);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.drawString(BACKGROUND_STRING, bgStringX, bgStringY);
if (image != null) {
g.drawImage(image, imgX, imgY, this);
}
}
private class TimerListener implements ActionListener {
public void actionPerformed(java.awt.event.ActionEvent e) {
for (Direction dir : Direction.values()) {
if (dirMap.get(dir)) {
imgX += dir.getX() * TRANSLATE_SCALE;
imgY += dir.getY() * TRANSLATE_SCALE;
}
}
repaint();
};
}
enum Direction {
Up(KeyEvent.VK_UP, 0, -1), Down(KeyEvent.VK_DOWN, 0, 1), Left(
KeyEvent.VK_LEFT, -1, 0), Right(KeyEvent.VK_RIGHT, 1, 0);
private int keyCode;
private int x;
private int y;
private Direction(int keyCode, int x, int y) {
this.keyCode = keyCode;
this.x = x;
this.y = y;
}
public int getKeyCode() {
return keyCode;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
private static void createAndShowGui() {
AnimateExample mainPanel = new AnimateExample();
JFrame frame = new JFrame("Animate Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
For more on Key Bindings, please check out the informative tutorial which you can find here.

Java panel interface

Im trying to make a pline drawing program. when I try to repaint all of the lines(after creating a new one) only the last one is draw out.The problem might be in repainting.
Can someone see what I am doing wrong?
Code is here:
public class Kimp extends JFrame {
private ArrayList<Point[]> pointsArray = new ArrayList<>();
private Point points[] = new Point[10000];
private int pointCounter = 0;
public Kimp () {
panel paintArea = new panel();
add(paintArea, BorderLayout.CENTER);
}
private class panel extends JPanel {
public panel () {
HandlerClass handler = new HandlerClass();
this.addMouseListener(handler);
this.addMouseMotionListener(handler);
}
#Override
void paintComponent(Graphics g) {
super.paintComponent(g);
try {
for (Point[] p : pointsArray) {
for(int i = 0; i < p.length; i++) {
if (p[i].x == 0) {
continue;
} else {
if (p[i + 1].x == 0) {
g.setColor(Color.BLUE);
g.drawLine(p[i].x, p[i].y, p[i].x, p[i].y);
} else {
g.setColor(Color.BLUE);
g.drawLine(p[i].x, p[i].y, p[i + 1].x, p[i + 1].y);
}
}
}
}
points = preFill(points);
} catch (NullPointerException e) {
}
}
}
private class HandlerClass implements MouseListener , MouseMotionListener {
#Override
public void mouseDragged(MouseEvent e) {
points[pointCounter++] = e.getPoint();
}
#Override
public void mousePressed(MouseEvent e) {
points[pointCounter] = e.getPoint();
}
#Override
public void mouseMoved(MouseEvent e) {
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
public static Point[] preFill (Point[] points) {
for (int i = 0; i < points.length; i++) {
points[i] = new Point(-999,-999);
}
return points;
}
}
I quickly rewrote your code to simplify it as much as possible. You may be able to better understand the concepts behind it.
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Kimp {
public static void main(String[] args) {
JFrame frame = new JFrame("Kimp!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.add(new CanvasPanel());
frame.setVisible(true);
}
}
class CanvasPanel extends JPanel {
private final List<List<Point>> lines = new LinkedList<List<Point>>();
private List<Point> points = new LinkedList<Point>();
public CanvasPanel() {
addMouseListener(mouseAdapter);
addMouseMotionListener(mouseAdapter);
}
#Override
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
for (List<Point> line : lines)
drawLine(line, g);
drawLine(points, g);
}
private void drawLine(List<Point> points, Graphics g) {
if (points.size() < 2) return;
Point p1 = points.get(0);
for (int i=1, n=points.size(); i<n; i++) {
Point p2 = points.get(i);
g.setColor(Color.BLUE);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
p1 = p2;
}
}
private MouseAdapter mouseAdapter = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
#Override
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
#Override
public void mouseReleased(MouseEvent e) {
if (points.size() > 1) {
lines.add(points);
points = new LinkedList<Point>();
}
}
};
}
private ArrayList<Point[]> pointsArray = new ArrayList<>();
This is a list of Point[]. Here only one private Point points[] = new Point[10000]; is allocated. Which means that each time you add points into pointArray, you are adding the same instance of points.
When you change the elements of points, all the existing Point[] in pointArray is updated. Since all the elements are referring to the same points.
Allocate new Point[] when drawing a new line.
You are resetting your points array every time you painted it (preFillPoints() is called in paint()). Your pointsArray (which should be called pointsList for gods sake) is completely meaninless, you add the same points[] array each time the mouse is released.
The code makes no sense. You only need the list or the array. Not both. Declare a List<Point> (not Point[]) and just add a new Point every time the mouse moves.

Categories