import javax.swing.*;
import java.awt.*;
public class FivePoints extends JFrame
{
public static void main(String[] args){
JFrame fr = new JFrame("FivePoints");
fr.setSize(400, 400);
fr.setLocationRelativeTo(null);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.getContentPane().setBackground(Color.BLACK);
fr.setLayout(new BorderLayout(1,1));
Drawing dr = new Drawing ();
fr.add(dr);
fr.setVisible(true);
}
}
/** The class that will draw the points.*/
class Drawing extends JComponent
{
/** The size of the point. If you set 1, 1 pixel will be drawn.*/
private static final int PIXEL_SIZE= 10;
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D gr=(Graphics2D)g;
/**The first pixel will be drawn in the center of the window.*/
int centerX = getWidth()/2;
int centerY = getHeight()/2;
gr.setPaint(Color.WHITE);
gr.fillOval(centerX,centerY, PIXEL_SIZE,PIXEL_SIZE);
gr.setPaint(Color.GREEN);
gr.fillOval(centerX/2,centerY/2, PIXEL_SIZE,PIXEL_SIZE);
gr.setPaint(Color.RED);
gr.fillOval(centerX/2,centerY/2*3, PIXEL_SIZE,PIXEL_SIZE);
gr.setColor(Color.YELLOW);
gr.fillOval(centerX/2*3,centerY/2, PIXEL_SIZE,PIXEL_SIZE);
gr.setColor(Color.BLUE);
gr.fillOval(centerX/2*3,centerY/2*3, PIXEL_SIZE,PIXEL_SIZE);
super.repaint();
}
}
Comments
Leave a comment