When I try to draw lines using the for loop below, it works, but it displays pretty slowly even when just loading in. It also pauses at every reload when moving (only added the d key for simplicity) Is that just my monitor/specs being slow, or is there a way to make graphics draw faster using some trade secret technique? Couldn't find anything on the matter by myself
Example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Prosscce extends Canvas implements KeyListener
{
int xshift = 0;
public Prosscce()
{
//Add Keystrokes
setFocusable(true);
requestFocus();
addKeyListener(this);
}
public void keyTyped(KeyEvent e)
{
if (e.getKeyChar() == 'd')
xshift+=2;
repaint();
}
public void keyPressed(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
public void paint(Graphics g)
{
int spacing = 20;
for (int i = xshift%spacing; i < getWidth(); i+=spacing)
g.drawLine(i, 0, i, getHeight());
}
public static void main(String[] args)
{
Prosscce xd = new Prosscce();
JFrame f = new JFrame();
f.setSize(600, 600);
f.setResizable(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(xd);
f.setVisible(true);
}
}