0

I am developing a lightweight graphics API for different needs where I want to render the image before it gets displayed. I have overriden the AWT's Component in order to have the highest perfomance. However, if I render the image (draw lines, rectangles & stuff) in the paint method the process of painting becomes visible to the user if the method is being called frequently (e.g. the window is being resized, or an animation is being played), and it is quite a perfomance-heavy solution.

I decided to cache and render the image separatly and simply copy the pixels to the Graphics object in the paint method. But now I don't know how to draw a single pixel to the Graphics object. I suppose the underlaying Sun implementation has such method but I should not address that class. I know that Graphics class declares several methods (drawLine, drawRect, fillRect, etc.) that seem lightweight and can be used to only paint one pixel but I don't know which one will be the quickest.

I do not want to use BufferedImage due to some memory issues and thus drawImage is not a solution in this case. It is also not the case asked in this question because, again, I do not use Images and I do not create new instances in the paint method (the rendered image data class only changes when the component is resized).

tl;dr: the quickest method in java.awt.Graphics to set the color of some pixel by its coordinates to a specified color (java.awt.Color or RGB, last preferred).

Thank you in advance.

Community
  • 1
  • 1
OLEGSHA
  • 388
  • 3
  • 13

1 Answers1

-1

If you ever feel you need to call repaint() and have a paintComponent(Graphics g) STOP immediately. Play around with this that I made this morning. 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
}
}

Play around with this, it's pretty straightforward, tell me if you want some commenting

  • in whatever you call your painting method, you can pass in x,y coordinates, then do g.drawRect(xCoord,yCoord,1,1); – S. Roughani Jan 27 '16 at 02:49