What is the fastest way to draw pixels in Java

前端 未结 5 2035
无人共我
无人共我 2020-12-28 23:52

I have some code that generates particles at random locations, and moving in random directions and speed.

Each iteration through a loop, I move all the particles, an

5条回答
  •  自闭症患者
    2020-12-29 00:17

    Never call repaint(); that's for noobs, play around with this where you don't have to call repaint();. That method has caused me so much pain and discomfort in the last 2 months, and I am sad nobody told me there is another way. 1,000,000 particles will get expensive real fast, so you may want to consider a Monte Carlo method, see http://raytracey.blogspot.com/ for cheaper rendering options. I don't know if you can afford manipulation of all of these particles while adhering to 20-30fps, I just watched a 10 second fluid simulation that took 3 weeks on a 2.4ghz 6gb ram machine. I apolagise because my only experience in the BufferedImage is importing .png's to draw with Graphics g. I worked on a project that was very computationally expensive very recently and with the timeline I was not able to gpu accelerate my program, so if you're in the same boat, try this package pet;

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.IOException;
    import javax.swing.*;
    
    
    public class pet extends JPanel implements MouseListener{
    public static JFrame frame = new JFrame("frame");
    public pet() throws IOException{
     setPreferredSize(new Dimension(870, 675));         //configuring panel
     addMouseListener(this);
    }
    public static void main(String[] args) throws IOException{
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new pet();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
        frame.addMouseListener(new pet());
    }
    public void paintRectangleAtPoint(Graphics g, int x, int y){
    g.setColor(Color.BLACK);
    g.drawRect(x, y, 100,100);
    }
    public void paintStuff(Graphics g, int x, int y){
    g.setColor(Color.BLACK);
    g.drawRect(x, y, 100,100);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    paintStuff(frame.getGraphics(),e.getX(), e.getY());
    
    }
    @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    
    }
    }
    

提交回复
热议问题