Eclipse java graphics issues -
i working in eclipse on creating canvas can draw lines or on it, simple program, know. have created canvas class, cartesiancoordinate class, , main class. have created method draw line of specified color on canvas created, whenever try enter color, told wrong. i've checked , seems right, if me i'd appreciative! here's code:
import java.awt.color; public class lab { public static void main(string [ ] args){ cartesiancoordinate mycoordinate; mycoordinate = new cartesiancoordinate(300, 400); mycoordinate.getx(); mycoordinate.gety(); system.out.printf("the coordinates are: (%s)", mycoordinate.tocoord()); canvas mycanvas = new canvas(800, 600); mycanvas.drawlinebetweenpoints(400, 500, green); } } public class cartesiancoordinate { private int xposition; private int yposition; public cartesiancoordinate(int startingx, int startingy){ this.xposition = startingx; this.yposition = startingy; } public int getx() { return this.xposition; } public int gety() { return this.yposition; } public string tocoord(){ return string.format("%02d, %02d", getx(), gety()); } } import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.arraylist; import java.util.list; import java.util.collections; import java.awt.geom.line2d; public class canvas extends jpanel { private int xsize, ysize; private jpanel panel; private string frametitle; private list<ls> lines; private final static int default_x = 800; private final static int default_y = 600; public canvas() { this(default_x, default_y); } public canvas(int x, int y) { xsize = x; ysize = y; frametitle = "canvas"; lines = collections.synchronizedlist(new arraylist<ls>()); setupcanvas(); } private void setupcanvas() { jframe frame = new jframe("my basic gui"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(this); frame.pack(); frame.setvisible(true); } public dimension getpreferredsize() { return new dimension(this.xsize, this.ysize); } public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); // smoother lines g2.setstroke(new basicstroke(3)); synchronized(lines) { (ls line : lines) { g2.setcolor(line.getcolor()); g2.draw(new line2d.float(line.getstartx(), line.getstarty(), line.getendx(), line.getendy())); } } } public void drawlinebetweenpoints(cartesiancoordinate startpoint, cartesiancoordinate endpoint, string color) { lines.add(new ls(startpoint.getx(), startpoint.gety(), endpoint.getx(), endpoint.gety(), this.parsecolour(color))); repaint(); } public void drawlinesegment(linesegment line, string color) { lines.add(new ls(line.getstartpoint().getx(), line.getstartpoint().gety(), line.getendpoint().getx(), line.getendpoint().gety(), this.parsecolour(color))); repaint(); } public void drawlinesegments(linesegment [] linesegments, string color) { (linesegment l : linesegments) { lines.add(new ls(l.getstartpoint().getx(), l.getstartpoint().gety(), l.getendpoint().getx(), l.getendpoint().gety(), this.parsecolour(color))); } repaint(); } private color parsecolour(string colour) { color drawcolor; switch (colour.tolowercase()) { case "white": drawcolor = color.white; break; case "black": drawcolor = color.black; break; case "blue": drawcolor = color.blue; break; case "cyan": drawcolor = color.cyan; break; case "gray": drawcolor = color.gray; break; case "darkgray": drawcolor = color.dark_gray; break; case "lightgray": drawcolor = color.light_gray; break; case "green": drawcolor = color.green; break; case "magenta": drawcolor = color.magenta; break; case "red": drawcolor = color.red; break; case "orange": drawcolor = color.orange; break; case "yellow": drawcolor = color.yellow; break; default: system.out.println("unknown colour '" + colour + "', defaulting black."); drawcolor = color.black; break; } return drawcolor; } public void removemostrecentline() { this.lines.remove(this.lines.size()-1); } public void clear() { lines.clear(); repaint(); } private class ls { private int startx, starty, endx, endy; private color color; public ls(int startx, int starty, int endx, int endy, color c) { this.startx = startx; this.starty = starty; this.endx = endx; this.endy = endy; this.color = c; } public int getstartx() { return this.startx; } public color getcolor() { return this.color; } public int getstarty() { return this.starty; } public int getendx() { return this.endx; } public int getendy() { return this.endy; } } }
according eclipse, when type "mycanvas.drawlinebetweenpoints(400, 500, green)", says doesn't recognise color specified. have tried many colors included in canvas class, , still says same. missing here? if am, please direct me towards error is? , please don't it's because eclipse bad, it's not helpful. thank you.
Comments
Post a Comment