Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javakoe;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.GraphicsEnvironment;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import javax.swing.JPanel;
- /**
- *
- *
- */
- public class MyPanel extends JPanel {
- private BufferedImage drawing = null;
- public MyPanel(){
- drawing = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(800, 600);
- for (int y=0;y<drawing.getHeight();y++){
- for (int x=0;x<drawing.getWidth();x++){
- drawing.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
- }
- }
- }
- public void clear(){
- for (int y=0;y<drawing.getHeight();y++){
- for (int x=0;x<drawing.getWidth();x++){
- drawing.setRGB(x, y, 255 << 16 | 255 << 8 | 255);
- }
- }
- repaint();
- }
- @Override
- public boolean isOpaque(){return true;}
- // Draws points into the panel
- public void drawPoint(int x, int y, int r, int g, int b){
- int color = (r << 16) | (g << 8) | b;
- if (x>=0 && y >= 0 && x < 800 && y < 600 ){
- drawing.setRGB(x, y, color);
- }
- // re-draw the panel (using the above my_x, my_y)
- repaint();
- }
- // Draws points into the panel
- public void drawPoint(int x, int y, Color color){
- if (x>=0 && y >= 0 && x < 800 && y < 600 ){
- drawing.setRGB(x, y, color.getRGB());
- }
- // re-draw the panel (using the above my_x, my_y)
- repaint();
- }
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D) g;
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
- Color original = g2.getColor();
- //g2.drawRect(x, y, 10, 10);
- g2.drawImage(drawing, null, this);
- g2.setColor(original);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement