Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javakoe;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.GraphicsEnvironment;
  12. import java.awt.RenderingHints;
  13. import java.awt.image.BufferedImage;
  14. import javax.swing.JPanel;
  15.  
  16. /**
  17. *
  18. *
  19. */
  20. public class MyPanel extends JPanel {
  21. private BufferedImage drawing = null;
  22.  
  23. public MyPanel(){
  24. drawing = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(800, 600);
  25.  
  26. for (int y=0;y<drawing.getHeight();y++){
  27. for (int x=0;x<drawing.getWidth();x++){
  28. drawing.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
  29. }
  30. }
  31.  
  32. }
  33. public void clear(){
  34. for (int y=0;y<drawing.getHeight();y++){
  35. for (int x=0;x<drawing.getWidth();x++){
  36. drawing.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
  37. }
  38. }
  39. repaint();
  40. }
  41.  
  42. @Override
  43. public boolean isOpaque(){return true;}
  44.  
  45. // Draws points into the panel
  46. public void drawPoint(int x, int y, int r, int g, int b){
  47. int color = (r << 16) | (g << 8) | b;
  48. if (x>=0 && y >= 0 && x < 800 && y < 600 ){
  49. drawing.setRGB(x, y, color);
  50. }
  51. // re-draw the panel (using the above my_x, my_y)
  52. repaint();
  53. }
  54.  
  55.  
  56.  
  57.  
  58. // Draws points into the panel
  59. public void drawPoint(int x, int y, Color color){
  60. if (x>=0 && y >= 0 && x < 800 && y < 600 ){
  61. drawing.setRGB(x, y, color.getRGB());
  62.  
  63. }
  64. // re-draw the panel (using the above my_x, my_y)
  65. repaint();
  66. }
  67. protected void paintComponent(Graphics g){
  68.  
  69. super.paintComponent(g);
  70. Graphics2D g2 = (Graphics2D) g;
  71. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  72.  
  73. Color original = g2.getColor();
  74.  
  75. //g2.drawRect(x, y, 10, 10);
  76. g2.drawImage(drawing, null, this);
  77. g2.setColor(original);
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement