Answer to Question #104769 in Java | JSP | JSF for Flores

Question #104769
write a program that plots 5 different colored pixels anywhere on a graphics screen with a black background
1
Expert's answer
2020-03-10T15:34:09-0400
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();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS