I wanted to create a frame in Java where I can draw any shape and color the shapes. I did the first part and now I have a problem in coloring the image. I have a frame which has a panel and I am drawing directly on the panel with drawLine()
function. I want to color the shape using pixels. How to color the panel (which contains the drawing) based on pixel value?
*Note: This is not an image!! Coloring the panel. I tried many forums. But everything says about coloring an image. How to color the panel by pixels?
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.applet.*;
public class colorch extends Applet implements MouseListener {
int oldx, oldy, x, y;
int flag = 1;
Panel mypanel;
public void init() {
mypanel = new Panel();
add(mypanel);
addMouseListener(this);
Panel colorpan = new Panel();
}
public void mouseExited(MouseEvent e6) { }
public void mouseEntered(MouseEvent e7) { }
public void mousePressed(MouseEvent e8) {
oldx = e8.getX();
oldy = e8.getY();
}
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e3) {
x = e3.getX();
y = e3.getY();
Graphics g = getGraphics();
g.drawLine(oldx, oldy, x, y);
}
}
Sample Output:
Now I want to color inside the triangle. So I am trying to color by each pixel within this image.
Thanks.