import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import javax.swing.JComponent; class PaintComponent extends JComponent { private static final long serialVersionUID = 1L; Image image; Graphics2D graphics2D; public PaintComponent() { setDoubleBuffered(false); } public void paintComponent(Graphics g) { if(image == null) { image = createImage(getSize().width, getSize().height); graphics2D = (Graphics2D)image.getGraphics(); graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics2D.setPaint(Color.RED); clear(); } g.drawImage(image, 0, 0, null); } public void clear() { graphics2D.setPaint(Color.white); graphics2D.fillRect(0, 0, getSize().width, getSize().height); graphics2D.setPaint(Color.black); repaint(); } public void rysujFigure(Figura figura, int poczatek_x, int poczatek_y, int koniec_x, int koniec_y, boolean wypelniajFigury) { int min_x = Math.min(koniec_x, poczatek_x); int min_y = Math.min(koniec_y, poczatek_y); int mid_x = Math.abs(koniec_x - poczatek_x); int mid_y = Math.abs(koniec_y - poczatek_y); switch(figura) { case KOLKO: if(wypelniajFigury) graphics2D.fillOval(min_x, min_y, mid_x, mid_y); else graphics2D.drawOval(min_x, min_y, mid_x, mid_y); break; case KRESKA: graphics2D.drawLine(poczatek_x, poczatek_y, koniec_x, koniec_y); break; case PROSTOKAT: if(wypelniajFigury) graphics2D.fillRect(min_x, min_y, mid_x, mid_y); else graphics2D.drawRect(min_x, min_y, mid_x, mid_y); break; default: break; } repaint(); } public void setColor(Color newColor) { graphics2D.setPaint(newColor); repaint(); } }