Draw a pixel: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 701: | Line 701: | ||
130 SET PALETTE BLACK,RED |
130 SET PALETTE BLACK,RED |
||
140 PLOT 100,100</lang> |
140 PLOT 100,100</lang> |
||
=={{header|Java}}== |
|||
Basic Implementation via subclass of JFrame: |
|||
<lang Java>import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
import javax.swing.JFrame; |
|||
public class DrawAPixel extends JFrame{ |
|||
public DrawAPixel() { |
|||
super("Red Pixel"); |
|||
setSize(320, 240); |
|||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
setVisible(true); |
|||
} |
|||
@Override |
|||
public void paint(Graphics g) { |
|||
g.setColor(new Color(255, 0, 0)); |
|||
g.drawRect(100, 100, 1, 1); |
|||
} |
|||
public static void main(String[] args) { |
|||
new DrawAPixel(); |
|||
} |
|||
} |
|||
</lang> |
|||
Advanced Implementation via subclass of JPanel (more powerful especially while repainting): |
|||
<lang Java>import java.awt.Color; |
|||
import java.awt.Graphics; |
|||
import java.awt.image.BufferedImage; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JPanel; |
|||
public class DrawAPixel extends JPanel{ |
|||
private BufferedImage puffer; |
|||
private JFrame window; |
|||
private Graphics g; |
|||
public DrawAPixel() { |
|||
window = new JFrame("Red Pixel"); |
|||
window.setSize(320, 240); |
|||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|||
window.setLayout(null); |
|||
setBounds(0, 0, 320, 240); |
|||
window.add(this); |
|||
window.setVisible(true); |
|||
} |
|||
@Override |
|||
public void paint(Graphics gr) { |
|||
if(g == null) { |
|||
puffer = (BufferedImage) createImage(getWidth(), getHeight()); |
|||
g = puffer.getGraphics(); |
|||
} |
|||
g.setColor(new Color(255, 0, 0)); |
|||
g.drawRect(100, 100, 1, 1); |
|||
gr.drawImage(puffer, 0, 0, this); |
|||
} |
|||
public static void main(String[] args) { |
|||
new DrawAPixel(); |
|||
} |
|||
} |
|||
</lang> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |