I DISCARD ALL THESE CODE AND WORK ON TOTALLY DIFFERENT ONE>>> NO NEED TO ANSWER ANYMORE
I got a homework from my professor. He told us to make applet paint program using 4 classes. Main; LightSourcePanel;DrawingPanel;PalettePanel;
(The basic code are not giving; only the class)
The program will take Mouse input and draw circle when I make a circular line. And the circle will have a 'glowing effect' depend on the lightSource (Use JSlider as light). When the JSlider is slided, the circle glowing effect change real time.
I am having problem referring the LightSource event Listener to the Drawing so It change the 'int light' inside the DrawingPanel. I don't know why the refering in the JColorChooser work while this one don't.
(It gave me java "non-static method cannot be referenced from a static" and I can't change it to static since I need to call repaint() method )
This is my Third class assignment and professor just taught us basic action listener in a single class. So I have no idea what Am I doing. If possible please explain my mistake in detail.
Main code:
public class HW3_to_4 extends Applet {
public void init (){
Dimension d = this.getSize();
setLayout(new BorderLayout());
JPanel Pale = new PalettePanel();
Pale.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel Light = new LightSourcePanel();
Light.setBorder(BorderFactory.createLineBorder(Color.black, 5));
JPanel Draw = new DrawingPanel();
Cursor c = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
Draw.setCursor(c);
Draw.setBorder(BorderFactory.createLineBorder(Color.green, 3));
add(Pale, BorderLayout.SOUTH );
add(Light,BorderLayout.NORTH);
add(Draw,BorderLayout.CENTER);
}
}
PalettePanel Code:
class PalettePanel extends JPanel implements ChangeListener {
JColorChooser j;
public PalettePanel () {
j = new JColorChooser ();
j.setPreviewPanel(new JPanel());
j.getSelectionModel().addChangeListener(this);
this.add(j);
}
public void stateChanged (ChangeEvent e){
Color a = j.getColor();
DrawingPanel.changecolor (a);
}
}
DrawingPanel Code & sub class inside (DrawingCanvas and Polyline) I rip this off from a example page in YAIP. :
public class DrawingPanel extends JPanel {
private List<PolyLine> lines = new ArrayList<PolyLine>();
private static PolyLine currentline;
private int maxX,maxY,difX, difY,minX,minY;
private static int lightSource = 0;
public static final int CANVAS_WIDTH = 2000;
public static final int CANVAS_HEIGHT = 800;
public static int[][] circle = new int[1000][4];
public static int check = 0;
public static Color c = Color.RED;
public DrawingPanel(){
DrawingCanvas canvas = new DrawingCanvas();
Dimension d = new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT);
canvas.setPreferredSize(d);
canvas.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
currentline = new PolyLine();
currentline.line_color = c;
lines.add(currentline);
currentline.add(e.getX(), e.getY());
}
});
canvas.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentline.add(e.getX(), e.getY());
repaint();
}
});
canvas.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
for(int i = 0; i<currentline.xList.size()-1; i++){
if(maxX<currentline.xList.get(i)){
maxX = currentline.xList.get(i);
}
}
for(int i = 0; i<currentline.yList.size()-1; i++){
if(maxY<currentline.yList.get(i)){
maxY = currentline.yList.get(i);
}
}
minX = maxX;
for(int i = 0; i<currentline.xList.size()-1; i++){
if(minX>currentline.xList.get(i)){
minX = currentline.xList.get(i);
}
}
minY = maxY;
for(int i = 0; i<currentline.yList.size()-1; i++){
if(minY>currentline.yList.get(i)){
minY = currentline.yList.get(i);
}
}
difX = maxX - minX;
difY = maxY - minY;
currentline.addcircle(minX,minY, difX, difY);
circle[check][0] = minX;
circle[check][1] = minY;
circle[check][2] = difX;
circle[check][3] = difY;
check++;
repaint();
maxX = 0; difX = 0;
maxY = 0; difY = 0;
}
});
this.add(canvas);
}
public static void changecolor(Color b){
c = b;
}
public void lightChange (int light){
lightSource = light;
RE();
}
public void RE (){
for (int x = 0; x< check ; x++) currentline.addcircle(circle[x][0],circle[x][1], circle[x][2],circle[x][3],lightSource);
repaint();
}
private class DrawingCanvas extends JPanel{
public void paint(Graphics g){
for(PolyLine line : lines){
g.setColor(line.line_color);
line.draw(g);
}
}
}
static class PolyLine{
public Color line_color = Color.RED;
private List <Integer> xList;
private List <Integer> yList;
boolean drawcircle = false;
int minx, miny, difx, dify, light;
public PolyLine() {
xList = new ArrayList<Integer>();
yList = new ArrayList<Integer>();
}
public void add(int x, int y){
xList.add(x);
yList.add(y);
}
public void addcircle(int x, int y, int difx, int dify){
this.minx = x; this.miny = y; this.difx = difx; this.dify = dify;
drawcircle = true;
}
public void addcircle(int x, int y, int difx, int dify, int light){
this.minx = x; this.miny = y; this.difx = difx; this.dify = dify; this.light = light;
drawcircle = true;
}
public void draw(Graphics g){
if(drawcircle){
g.fillOval(minx, miny, difx, dify);
g.setColor(Color.WHITE);
g.fillOval((minx+difx/4)+light, miny+dify/4, difx/4, dify/4);
g.setColor(line_color);
}else{
for(int i = 0; i<xList.size()-1; i++){
g.drawLine((int)xList.get(i), (int)yList.get(i), (int)xList.get(i + 1),
(int)yList.get(i + 1));
}
}
}}}
>
Here is the problem.
LightSourcePanel Code:
class LightSourcePanel extends JPanel implements ChangeListener {
static JSlider j;
public LightSourcePanel (){
j = new JSlider(0,180,90);
j.setMajorTickSpacing(45);
j.setMinorTickSpacing(5);
j.setPaintLabels(true);
j.setPaintTicks (true);
j.setPreferredSize(new Dimension (1500,50));
j.addChangeListener(this);
this.add(j);
}
@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
lightChange(j.getValue()); // problem <<<<<< FAIL
}}