Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 43

Thread: Drawing in Java

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Drawing in Java

    Hey guys!

    Okay so I have this program I need to write, and im not quiete sure about this part. So basically we are given a txt file with coordinates and are susposed to, read them in using Classes and Methods in order to get a picture consisting of lines rectangles circles. This is the code i have so far

    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.util.concurrent.*;
    import java.lang.reflect.*;
    /**
     * This class supports the output of graphical objects like points, lines,
     * circles and rectangles in a window.
     * 
     */
    public final class Window {
    	private final JPanel panel;
    	private final Graphics2D graphics;
    	private Window(JPanel panel, Graphics2D graphics) {
    		this.panel = panel;
    		this.graphics = graphics;
    	}
    	/**
    	 * Creates a window with a given width and height, and shows it on the screen.
    	 * 
    	 * @param width  the width of the window
    	 * @param height the height of the window
    	 */
    	public static Window create(int width, int height) {
    		RunnableFuture<Window> initTask = new FutureTask<Window>(() -> {
    			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    			Graphics2D graphics = image.createGraphics();
    			graphics.fillRect(0, 0, width, height);
     
    			JFrame frame = new JFrame();
    			JPanel panel = new JPanel() {
     
    				@Override
    				protected void paintComponent(Graphics g) {
    					super.paintComponent(g);
     
    					Graphics2D g2 = (Graphics2D) g.create();
    					g2.drawImage(image, 0, 0, width, height, null);
    					g2.dispose();
    				}
     
    				@Override
    				public Dimension getPreferredSize() {
    					return new Dimension(width, height);
    				}
    			};
     
    			frame.setContentPane(panel);
    			frame.setTitle("Window");
    			frame.pack();
    			frame.setResizable(false);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setVisible(true);
     
    			return new Window(panel, graphics);
    		});
     
    		SwingUtilities.invokeLater(initTask);
    		try {
    			return initTask.get();
    		} catch (InterruptedException e) {
    			return null;
    		} catch (ExecutionException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Clears the content of the window.
    	 */
    	public void clear() {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.fillRect(0, 0, panel.getWidth(), panel.getHeight());
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a point at a given position.
    	 * 
    	 * @param x the x-coordinate for the point
    	 * @param y the y-coordinate for the point
    	 */
    	public void drawPoint(int x, int y) {
    		drawPoint(x, y, Color.BLACK);
    	}
     
    	/**
    	 * Draws a line between two given points.
    	 * 
    	 * @param x1 the x-coordinate for the first point
    	 * @param y1 the y-coordinate for the first point
    	 * @param x2 the x-coordinate for the second point
    	 * @param y2 the y-coordinate for the second point
    	 */
    	public void drawLine(int x1, int y1, int x2, int y2) {
    		drawLine(x1, y1, x2, y2, Color.BLACK);
    	}
     
    	/**
    	 * Draws a rectangle at a top-left position, and with a specific width and
    	 * height.
    	 * 
    	 * @param x the top-left x-coordinate for the rectangle
    	 * @param y the top-left y-coordinate for the rectangle
    	 * @param w the width for the rectangle
    	 * @param h the height for the rectangle
    	 */
    	public void drawRectangle(int x, int y, int w, int h) {
    		drawRectangle(x, y, w, h, Color.BLACK);
    	}
     
    	/**
    	 * Draws a circle at a given center position, and with a specific radius.
    	 * 
    	 * @param x the center x-coordinate for the circle
    	 * @param y the center y-coordinate for the circle
    	 * @param r the radius for the circle
    	 */
    	public void drawCircle(int x, int y, int r) {
    		drawCircle(x, y, r, Color.BLACK);
    	}
     
    	/**
    	 * Draws a text at a given position.
    	 * 
    	 * @param text the text to draw
    	 * @param x    the x-coordinate for the text
    	 * @param y    the y-coordinate for the text
    	 */
    	public void drawText(String text, int x, int y) {
    		drawText(text, x, y, Color.BLACK);
    	}
     
    	/**
    	 * Draws a point at a given position, and with a specific color.
    	 * 
    	 * @param x     the x-coordinate for the point
    	 * @param y     the y-coordinate for the point
    	 * @param color the color for the point
    	 */
    	public void drawPoint(int x, int y, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillRect(x - 1, y - 1, 3, 3);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a line from between two given coordinates, and with a specific color.
    	 * 
    	 * @param x1    the x-coordinate for the first point
    	 * @param y1    the y-coordinate for the first point
    	 * @param x2    the x-coordinate for the second point
    	 * @param y2    the y-coordinate for the second point
    	 * @param color the color for the line
    	 */
    	public void drawLine(int x1, int y1, int x2, int y2, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawLine(x1, y1, x2, y2);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a rectangle at a given top-left position, and with a specific width,
    	 * height, and color.
    	 * 
    	 * @param x     the top-left x-coordinate for the rectangle
    	 * @param y     the top-left y-coordinate for the rectangle
    	 * @param w     the width for the rectangle
    	 * @param h     the height for the rectangle
    	 * @param color the color for the rectangle
    	 */
    	public void drawRectangle(int x, int y, int w, int h, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(Color.BLACK);
    				graphics.drawRect(x, y, w, h);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a circle at a given center position, and with a specific radius and
    	 * color.
    	 * 
    	 * @param x     the center x-coordinate for the circle
    	 * @param y     the center y-coordinate for the circle
    	 * @param r     the radius for the circle
    	 * @param color the color for the circle
    	 */
    	public void drawCircle(int x, int y, int r, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawOval(x - r, y - r, 2 * r, 2 * r);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a text at a given position, and with a specific color.
    	 * 
    	 * @param text  the text to draw
    	 * @param x     the x-coordinate for the text
    	 * @param y     the y-coordinate for the text
    	 * @param color the color for the text
    	 */
    	public void drawText(String text, int x, int y, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawString(text, x, y);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Fills a rectangle at a given top-left position, and with a specific width,
    	 * height, and color.
    	 * 
    	 * @param x     the top-left x-coordinate for the rectangle
    	 * @param y     the top-left y-coordinate for the rectangle
    	 * @param w     the width for the rectangle
    	 * @param h     the height for the rectangle
    	 * @param color the color for the rectangle
    	 */
    	public void fillRectangle(int x, int y, int w, int h, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillRect(x, y, w, h);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Fills a circle at a given center position, and with a specific radius and
    	 * color.
    	 * 
    	 * @param x     the center x-coordinate for the circle
    	 * @param y     the center y-coordinate for the circle
    	 * @param r     the radius for the circle
    	 * @param color the color for the circle
    	 */
    	public void fillCircle(int x, int y, int r, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillOval(x - r, y - r, 2 * r, 2 * r);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
    }
     
    class Point {
    	int x;
    	int y;
     
    	Point(int x, int y) {
    		this.x = x;
    		this.y = y;
     
     
    	}	
    	static Point fromInput() {   
        int x = In.readInt();
        int y = In.readInt();
     
    	if (In.done()) return new Point(x, y);
        else return null;
    }
     
     
     
    class Line {
    	int x1;
    	int y1;
    	int x2;
    	int y2;
     
    	Line(int x1,int x2,int y1,int y2){
    		this.x1 = x1;
    		this.y1 = x2;
    		this.x2 = x2;
    		this.y2 = y2;
     
    	}
    	static Line fromInput() {
    		int x1 = In.readInt();
    		int y1 = In.readInt();
    		int x2 = In.readInt();
    		int y2 = In.readInt();
     
    		if(In.done()) return new Line(x1,y1,x2,y2);
    		else return null;
    	}
     
     
    class Rectangle {
    	int x;
    	int y;
    	int h;
    	int w;
     
    	Rectangle(int x,int y,int h,int w) {
    		this.x = x;
    		this.y = y;
    		this.h = h;
    		this.w = w;
    	}
    	static Rectangle fromInput() {
    		int x = In.readInt();
    		int y = In.readInt();
    		int h = In.readInt();
    		int w = In.readInt();
     
    		if(In.done()) return new Rectangle(x,y,h,w);
    		else return null;
    	}

    Now im not sure how to actually print out the lines and rectangels on the screen. It says to use some void draw(Window w) object method but i'm not really sure how to implement that.Any help?

    Thanks!
    Last edited by arhzz; May 5th, 2020 at 12:02 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing in Java

    given a txt file with coordinates and are susposed to, read them in using Classes and Methods in order to get a picture consisting of lines rectangles circles.
    Can you describe the values in the text file? How is the data in the text file organized? Can you post a few lines of the file so we can see?

    how to actually print out the lines and rectangels on the screen.
    The posted code has methods to do that. Talk to the person that wrote the code to see how to use it.

    Note: Window is the name of a Java class. It is better if you do not use the same names as used by Java. Better would be MyWindow.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Hey my man thanks for the speedy reply !
    Sure here is how the txt file is organized;
    300 300
    L 0 200 300 200
    L 150 100 200 50
    L 200 50 250 100
    R 150 100 100 100
    R 185 160 30 40
    R 170 120 20 20
    L 180 120 180 140
    L 170 130 190 130
    R 210 120 20 20
    L 220 120 220 140
    L 210 130 230 130
    C 50 50 10
    L 30 50 40 50
    L 35 35 42 42
    L 50 30 50 40
    L 65 35 58 42
    L 60 50 70 50

    The L R C indicate if it's a circle, a line or a rectangle, okay so in the code are actually methods that i can use to print out the "picture"? So if i'm getting this right i should use drawLine for the lines, drawCircle for cirles etc.. ??

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing in Java

    i should use drawLine for the lines, drawCircle for cirles etc..
    Yes that sounds reasonable.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Aight Im gonna try that and let you know what happens, thanks for your help!

  6. #6
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Static method problem

    Hey!

    So im stuck with this problem in my code and i cant seem to get it to work properly.

    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import java.util.concurrent.*;
    import java.lang.reflect.*;
    /**
     * This class supports the output of graphical objects like points, lines,
     * circles and rectangles in a window.
     * 
     * @author Florian Latifi
     */
    public final class Window {
    	private final JPanel panel;
    	private final Graphics2D graphics;
    	private Window(JPanel panel, Graphics2D graphics) {
    		this.panel = panel;
    		this.graphics = graphics;
    	}
    	/**
    	 * Creates a window with a given width and height, and shows it on the screen.
    	 * 
    	 * @param width  the width of the window
    	 * @param height the height of the window
    	 */
    	public static Window create(int width, int height) {
    		RunnableFuture<Window> initTask = new FutureTask<Window>(() -> {
    			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    			Graphics2D graphics = image.createGraphics();
    			graphics.fillRect(0, 0, width, height);
     
    			JFrame frame = new JFrame();
    			JPanel panel = new JPanel() {
     
    				@Override
    				protected void paintComponent(Graphics g) {
    					super.paintComponent(g);
     
    					Graphics2D g2 = (Graphics2D) g.create();
    					g2.drawImage(image, 0, 0, width, height, null);
    					g2.dispose();
    				}
     
    				@Override
    				public Dimension getPreferredSize() {
    					return new Dimension(width, height);
    				}
    			};
     
    			frame.setContentPane(panel);
    			frame.setTitle("Window");
    			frame.pack();
    			frame.setResizable(false);
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setVisible(true);
     
    			return new Window(panel, graphics);
    		});
     
    		SwingUtilities.invokeLater(initTask);
    		try {
    			return initTask.get();
    		} catch (InterruptedException e) {
    			return null;
    		} catch (ExecutionException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Clears the content of the window.
    	 */
    	public void clear() {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.fillRect(0, 0, panel.getWidth(), panel.getHeight());
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a point at a given position.
    	 * 
    	 * @param x the x-coordinate for the point
    	 * @param y the y-coordinate for the point
    	 */
    	public void drawPoint(int x, int y) {
    		drawPoint(x, y, Color.BLACK);
    	}
     
    	/**
    	 * Draws a line between two given points.
    	 * 
    	 * @param x1 the x-coordinate for the first point
    	 * @param y1 the y-coordinate for the first point
    	 * @param x2 the x-coordinate for the second point
    	 * @param y2 the y-coordinate for the second point
    	 */
    	public void drawLine(int x1, int y1, int x2, int y2) {
    		drawLine(x1, y1, x2, y2, Color.BLACK);
    	}
     
    	/**
    	 * Draws a rectangle at a top-left position, and with a specific width and
    	 * height.
    	 * 
    	 * @param x the top-left x-coordinate for the rectangle
    	 * @param y the top-left y-coordinate for the rectangle
    	 * @param w the width for the rectangle
    	 * @param h the height for the rectangle
    	 */
    	public void drawRectangle(int x, int y, int w, int h) {
    		drawRectangle(x, y, w, h, Color.BLACK);
    	}
     
    	/**
    	 * Draws a circle at a given center position, and with a specific radius.
    	 * 
    	 * @param x the center x-coordinate for the circle
    	 * @param y the center y-coordinate for the circle
    	 * @param r the radius for the circle
    	 */
    	public void drawCircle(int x, int y, int r) {
    		drawCircle(x, y, r, Color.BLACK);
    	}
     
    	/**
    	 * Draws a text at a given position.
    	 * 
    	 * @param text the text to draw
    	 * @param x    the x-coordinate for the text
    	 * @param y    the y-coordinate for the text
    	 */
    	public void drawText(String text, int x, int y) {
    		drawText(text, x, y, Color.BLACK);
    	}
     
    	/**
    	 * Draws a point at a given position, and with a specific color.
    	 * 
    	 * @param x     the x-coordinate for the point
    	 * @param y     the y-coordinate for the point
    	 * @param color the color for the point
    	 */
    	public void drawPoint(int x, int y, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillRect(x - 1, y - 1, 3, 3);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a line from between two given coordinates, and with a specific color.
    	 * 
    	 * @param x1    the x-coordinate for the first point
    	 * @param y1    the y-coordinate for the first point
    	 * @param x2    the x-coordinate for the second point
    	 * @param y2    the y-coordinate for the second point
    	 * @param color the color for the line
    	 */
    	public void drawLine(int x1, int y1, int x2, int y2, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawLine(x1, y1, x2, y2);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a rectangle at a given top-left position, and with a specific width,
    	 * height, and color.
    	 * 
    	 * @param x     the top-left x-coordinate for the rectangle
    	 * @param y     the top-left y-coordinate for the rectangle
    	 * @param w     the width for the rectangle
    	 * @param h     the height for the rectangle
    	 * @param color the color for the rectangle
    	 */
    	public void drawRectangle(int x, int y, int w, int h, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(Color.BLACK);
    				graphics.drawRect(x, y, w, h);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a circle at a given center position, and with a specific radius and
    	 * color.
    	 * 
    	 * @param x     the center x-coordinate for the circle
    	 * @param y     the center y-coordinate for the circle
    	 * @param r     the radius for the circle
    	 * @param color the color for the circle
    	 */
    	public void drawCircle(int x, int y, int r, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawOval(x - r, y - r, 2 * r, 2 * r);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Draws a text at a given position, and with a specific color.
    	 * 
    	 * @param text  the text to draw
    	 * @param x     the x-coordinate for the text
    	 * @param y     the y-coordinate for the text
    	 * @param color the color for the text
    	 */
    	public void drawText(String text, int x, int y, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.drawString(text, x, y);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Fills a rectangle at a given top-left position, and with a specific width,
    	 * height, and color.
    	 * 
    	 * @param x     the top-left x-coordinate for the rectangle
    	 * @param y     the top-left y-coordinate for the rectangle
    	 * @param w     the width for the rectangle
    	 * @param h     the height for the rectangle
    	 * @param color the color for the rectangle
    	 */
    	public void fillRectangle(int x, int y, int w, int h, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillRect(x, y, w, h);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    	/**
    	 * Fills a circle at a given center position, and with a specific radius and
    	 * color.
    	 * 
    	 * @param x     the center x-coordinate for the circle
    	 * @param y     the center y-coordinate for the circle
    	 * @param r     the radius for the circle
    	 * @param color the color for the circle
    	 */
    	public void fillCircle(int x, int y, int r, Color color) {
    		try {
    			SwingUtilities.invokeAndWait(() -> {
    				graphics.setColor(color);
    				graphics.fillOval(x - r, y - r, 2 * r, 2 * r);
    				panel.repaint();
    			});
    		} catch (InterruptedException e) {
    			return;
    		} catch (InvocationTargetException e) {
    			throw new RuntimeException(e);
    		}
    	}
     
    class Point {
      final int x;
      final int y;
     
     
      Point(int x,int y) {
        this.x = x;
        this.y = y;
      }
       static Point fromInput() {
        int x = In.readInt();
        int y = In.readInt();
     
        if (In.done()) return new Point(x,y);
        else return null;
      }
     
    void draw(Window w){
    	w.drawPoint(x,y);
    }
    }
     
    class Line {
      final int x1;
      final int y1;
      final int x2;
      final int y2;
     
      Line(int x1,int y1,int x2,int y2) {
        this.x1 = x1;
        this.y1 = y1;
    	this.x2 = x2;
    	this.y2 = y2;
      }
        Line fromInput() {
        int x1 = In.readInt();
        int y1 = In.readInt();
    	int x22 = In.readInt();
    	int y2 = In.readInt();
     
        if (In.done()) return new Line(x1,y1,x2,y2);
        else return null;
      }
    void draw(Window w){
    	w.drawLine(x1, y1, x2, y2);
    }
    }
     
    class Rectangle {
      final int x;
      final int y;
      final int w1;
      final int h;
     
      Rectangle(int x,int y,int w1,int h) {
        this.x = x;
        this.y = y;
    	this.w1 = w1;
    	this.h = h;
      }
        Rectangle fromInput() {
        int x = In.readInt();
        int y = In.readInt();
    	int w1 = In.readInt();
    	int h = In.readInt();
     
        if (In.done()) return new Rectangle(x,y,w1,h);
        else return null;
      }
    void draw(Window w){
    	w.drawRectangle(x,y,w1,h );
    }
    }
     
    class Circle {
      final int x;
      final int y;
      final int r;
     
      Circle(int x,int y,int r) {
        this.x = x;
        this.y = y;
    	this.r = r;
      }
        Circle fromInput() {
        int x = In.readInt();
        int y = In.readInt();
    	int r = In.readInt();
     
        if (In.done()) return new Circle(x,y,r);
        else return null;
      }
    void draw(Window w){
    	w.drawCircle(x, y, r );
    }
    }
    }
    So the problem is whenever i try to compile the program i'm getting an error saying i cant use the static method at static Point fromInput, and for the Line Circle etc... it works fine without static, although it says in the description of the assignment to use a a static method. What am i doing wrong, am i missing some code maybe a class or something and could that be causing the problem. Thanks!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    an error saying i cant use the static method
    What statement(s) did you get that error message on?
    Please copy the full text of the compiler's error message and paste it here so we can see the full text of the message.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Static method problem

    Hey! thanks for the speedy response, here is what i get on CMD
    Window.java:315: error: non-static variable this cannot be referenced from a static context
    if (In.done()) return new Point(x,y);
    ^
    Window.java:311: error: Illegal static declaration in inner class Window.Point
    static Point fromInput() {
    ^
    modifier 'static' is only allowed in constant variable declarations
    2 errors

    C:\Users\Hp\Desktop\figuren>

    And just, a sidenote. We also got an example that is very similiar, and when i run that example i dont get the error.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    Illegal static declaration in inner class Window.Point
    Change the class's declaration to be static:
      static class Point ...
    However That will prevent the inner class (Point) from accessing variables in the enclosing class (Window).
    Last edited by Norm; April 27th, 2020 at 02:14 PM. Reason: clarify
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Static method problem

    Hmmm yea, that aint ideal, i'll have to figure something out

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    What variables does the Point class need to see in the Windows class?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Static method problem

    The class point needs to see the variables x and y

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    Where are those variables declared?
    Are you talking about the x and y declared in the Point class. The Point class can see them. They are in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Static method problem

    Yes I'm talking about those two.Like in the description of the assignment says and i'll try to translate my best its in German; It says that in every class we should declare a Constructor that will initialise the Object fields with help of his given parameters. And in every class use an static fromInput() method that will read in the coordinates out of the txt file create a new object and return it,check with (!indone) if you are at the end of the file. Implement a void draw(Window) method that will help you draw out the appropriate figures. Use these methods in your main function to read in the txt file and get the desired picture.

    I hope this help you in understanding what my actuall asignment is, i tried my best to translate it.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    Did you try making the class static? What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Static method problem

    Okay so I did what you said and it worked Thanks so much man, but now Im facing another problem haha. So now I need to actually to draw out the lines rectangles and so on, and this is what i got so far
    static class Point {
       final int x;
       final int y;
     
     
      Point(int x,int y) {
        this.x = x;
        this.y = y;
      }
        static Point fromInput() {
        int x = In.readInt();
        int y = In.readInt();
     
        if (In.done()) return new Point(x,y);
        else return null;
      }
     
    void draw(Window w){
    	w.drawPoint(x,y);
    }
    }
     
    static class Line {
      final int x1;
      final int y1;
      final int x2;
      final int y2;
     
      Line(int x1,int y1,int x2,int y2) {
        this.x1 = x1;
        this.y1 = y1;
    	this.x2 = x2;
    	this.y2 = y2;
      }
        Line fromInput() {
        int x1 = In.readInt();
        int y1 = In.readInt();
    	int x2 = In.readInt();
    	int y2 = In.readInt();
     
        if (In.done()) return new Line(x1,y1,x2,y2);
        else return null;
      }
    void draw(Window w){
    	w.drawLine(x1, y1, x2, y2);
    }
    }
     
    static class Rectangle {
      final int x;
      final int y;
      final int w1;
      final int h;
     
      Rectangle(int x,int y,int w1,int h) {
        this.x = x;
        this.y = y;
    	this.w1 = w1;
    	this.h = h;
      }
        Rectangle fromInput() {
        int x = In.readInt();
        int y = In.readInt();
    	int w1 = In.readInt();
    	int h = In.readInt();
     
        if (In.done()) return new Rectangle(x,y,w1,h);
        else return null;
      }
    void draw(Window w){
    	w.drawRectangle(x,y,w1,h );
    }
    }
     
    static class Circle {
      final int x;
      final int y;
      final int r;
     
      Circle(int x,int y,int r) {
        this.x = x;
        this.y = y;
    	this.r = r;
      }
        Circle fromInput() {
        int x = In.readInt();
        int y = In.readInt();
    	int r = In.readInt();
     
        if (In.done()) return new Circle(x,y,r);
        else return null;
      }
    void draw(Window w){
    	w.drawCircle(x, y, r );
    }
    }
    public static void main(String[] args){
    	Window w = Window.create(300,300);
    }
    }

    So this is the part where i create the window (I excluded the other part of the code the one where the grapichs are enabled just to make it clearer what im tryin to say) and this are his dimension. Now in order to "draw out" the lines rectangles etc... I need to use this void draw(Window w) method, any tips on how to approach the problem or should i just try a few things and than see what i get? Thanks again for the info with the static, really helped !

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Static method problem

    Where is the code to read the data file with the details on what to draw and where to draw it?
    Use that to create the objects for each of the shapes and to call their draw methods.

    The two threads have been merged.
    If you don't understand my answer, don't ignore it, ask a question.

  18. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (April 27th, 2020)

  19. #18
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Okay i see, im gonna try to implement that and we will see what happens.Thanks

  20. #19
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Okay so our professor gave us an hint on how to solve this;

    public class WindowDemonstration {
     
        public static void main(String[] args) {
            // Create window instance
            Window w = Window.create(400, 400);
            // Create text instance
            // Than Point,Line Circle,Rectangle
            Text t = new Text("Hallo, ich bin ein Window Text", 47, 100);
            // Call the draw method
           t.draw(w);
        }
     
    }
     
    class Text {
        // Felder
        private final String text;
        private final int x, y;
     
        // Konstruktor
        Text(String text, int x, int y) {
            this.text = text;
            this.x = x;
            this.y = y;
        }
     
        // Objektmethode 'draw'
        void draw(Window w) {
            w.drawText(text, x, y);
        }
    }
    We are susposed to use this on the code that i have posted before I've tried various things but I cannot use the method fromInput in order to read the File in,any help?

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing in Java

    I cannot use the method fromInput in order to read the File in
    Please explain what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Okay so here is the code

     
    tatic class Point {
       final int x;
       final int y;
     
     
      Point(int x,int y) {
        this.x = x;
        this.y = y;
      }
        static Point fromInput() {
        int x = In.readInt();
        int y = In.readInt();
     
        if (In.done()) return new Point(x,y);
        else return null;
      }
     
    void draw(Window w){
    	w.drawPoint(x,y);
    }
    }
     
    static class Line {
      final char ch;
      final int x1;
      final int y1;
      final int x2;
      final int y2;
     
        Line(char ch,int x1,int y1,int x2,int y2) {
    	this.ch = ch;  
        this.x1 = x1;
        this.y1 = y1;
    	this.x2 = x2;
    	this.y2 = y2;
      }
        Line fromInput() {
        char ch = In.readChar();
        int x1 = In.readInt();
        int y1 = In.readInt();
    	int x2 = In.readInt();
    	int y2 = In.readInt();
     
        if (In.done()) return new Line(ch,x1,y1,x2,y2);
        else return null;
      }
    void draw(Window w){
    	w.drawLine(x1, y1, x2, y2);
    }
    }
     
    static class Rectangle {
      final char ch;
      final int x;
      final int y;
      final int w1;
      final int h;
     
      Rectangle(char ch,int x,int y,int w1,int h) {
    	this.ch = ch;
        this.x = x;
        this.y = y;
    	this.w1 = w1;
    	this.h = h;
      }
        Rectangle fromInput() {
    	char ch = In.readChar();
        int x = In.readInt();
        int y = In.readInt();
    	int w1 = In.readInt();
    	int h = In.readInt();
     
        if (In.done()) return new Rectangle(ch,x,y,w1,h);
        else return null;
      }
    void draw(Window w){
    	w.drawRectangle(x,y,w1,h );
    }
    }
     
    static class Circle {
      final char ch;
      final int x;
      final int y;
      final int r;
     
      Circle(char ch,int x,int y,int r) {
    	this.ch = ch;
        this.x = x;
        this.y = y;
    	this.r = r;
      }
        Circle fromInput() {
    	char ch = In.readChar();
        int x = In.readInt();
        int y = In.readInt();
    	int r = In.readInt();
     
        if (In.done()) return new Circle(ch,x,y,r);
        else return null;
      }
    void draw(Window w){
    	w.drawCircle(x, y, r );
    }
    }
    public static void main(String[] args){
    	Window w = Window.create(300,300);
    	In.open("shapes.txt");
    	char ch;
    	switch(ch){
    		case 'L':
    		Line l = new Line();
    		l.fromInput();
    		l.draw(w);
    		break;
     
    		case 'R':
    		Rectangle r = new Rectangle();
    		r.fromInput();
    		r.draw(w);
    		break;
     
    		case 'C':
    		Circle c = new Circle();
    		c.draw(w);
    		break;
    	}
     
    }
    }

    So what I am trying here is to open the txt file shapes that has the coordinates and at start of every line has a either an L R or C depending on what the shape is(L for Line...) after opening the file with In.Open i created a char ch and made a switch to check if the letter is L R or C and according to that I want to read in the line with fromInput() and than use the l.draw(w) method, to draw it on the window that i created in the main method. What am i doing wrong here?

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing in Java

    What am i doing wrong here?
    One problem I see is that the variable ch in the main method is not given a value.

    All of The fromInput methods were once declared as static:
       static Point fromInput() {
    That means the method is called using the classname NOT with a reference to an instance of the class:
       Point pnt = Point.fromInput();   // read data and create new Point object
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

    Oh,I missed that I put it as char ch = In.readChar(); I think that works, but still the problem persists;

    Window.java:413: error: constructor Line in class Line cannot be applied to given types;
    Line l = new Line();
    ^
    required: char,int,int,int,int
    found: no arguments
    reason: actual and formal argument lists differ in length
    Window.java:419: error: constructor Rectangle in class Rectangle cannot be applied to given types;
    Rectangle r = new Rectangle();
    ^
    required: char,int,int,int,int
    found: no arguments
    reason: actual and formal argument lists differ in length
    Window.java:425: error: constructor Circle in class Circle cannot be applied to given types;
    Circle c = new Circle();
    ^
    required: char,int,int,int
    found: no arguments
    reason: actual and formal argument lists differ in length
    3 errors

    P.S I dont know how to implement this part of the program, i have tried everything i could come up with, and nothing works.Like an example how to do one of the classes could help me figure it out.

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Drawing in Java

    Read the end of my last post. The fromInput methods are static (see Post#1) and do NOT require an instance of the class to be called. They read the input and create an instance of the class that they are in.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Java

     
    class Point {
       final int x;
       final int y;
     
      Point(int x,int y) {
        this.x = x;
        this.y = y;
      }
        static Point fromInput() {
        int x = In.readInt();
        int y = In.readInt();
     
        if (In.done()) return new Point(x,y);
        else return null;
      }
     
    void draw(Window w){
    	w.drawPoint(x,y);
    }
    }
     
    class Line {
      final char ch;
      final int x1;
      final int y1;
      final int x2;
      final int y2;
     
        Line(char ch,int x1,int y1,int x2,int y2) {
    	this.ch = ch;  
        this.x1 = x1;
        this.y1 = y1;
    	this.x2 = x2;
    	this.y2 = y2;
      }
       static Line fromInput() {
        char ch = In.readChar();
        int x1 = In.readInt();
        int y1 = In.readInt();
    	int x2 = In.readInt();
    	int y2 = In.readInt();
     
        if (In.done()) return new Line(ch,x1,y1,x2,y2);
        else return null;
      }
    void draw(Window w){
    	w.drawLine(x1, y1, x2, y2);
    }
    }
     
    class Rectangle {
      final char ch;
      final int x;
      final int y;
      final int w1;
      final int h;
     
      Rectangle(char ch,int x,int y,int w1,int h) {
    	this.ch = ch;
        this.x = x;
        this.y = y;
    	this.w1 = w1;
    	this.h = h;
      }
        static Rectangle fromInput() {
    	char ch = In.readChar();
        int x = In.readInt();
        int y = In.readInt();
    	int w1 = In.readInt();
    	int h = In.readInt();
     
        if (In.done()) return new Rectangle(ch,x,y,w1,h);
        else return null;
      }
    void draw(Window w){
    	w.drawRectangle(x,y,w1,h );
    }
    }
     
    class Circle {
      final char ch;
      final int x;
      final int y;
      final int r;
     
       Circle(char ch,int x,int y,int r) {
    	this.ch = ch;
        this.x = x;
        this.y = y;
    	this.r = r;
      }
       static Circle fromInput() {
    	char ch = In.readChar();
        int x = In.readInt();
        int y = In.readInt();
    	int r = In.readInt();
     
        if (In.done()) return new Circle(ch,x,y,r);
        else return null;
      }
    void draw(Window w){
    	w.drawCircle(x, y, r );
    }
    }
    public static void main(String[] args){
    	Window w = Window.create(300,300);
    	In.open("shapes.txt");
    	char ch = In.readChar();
    	switch(ch){
    		case 'L':
    		Line l = Line.fromInput();
    		l.draw(w);
    		break;
    		case 'R':
    		Rectangle r = Rectangle.fromInput();
    		r.draw(w);
    		break;
    		case 'C':
    		Circle c = Circle.fromInput();
    		c.draw(w);
    		break;
    	}
     
    }
    }

    When I try it like this i get these error;
    Window.java:314: error: non-static variable this cannot be referenced from a static context
    if (In.done()) return new Point(x,y);
    ^
    Window.java:310: error: Illegal static declaration in inner class Window.Point
    static Point fromInput() {
    ^
    modifier 'static' is only allowed in constant variable declarations
    Window.java:344: error: non-static variable this cannot be referenced from a static context
    if (In.done()) return new Line(ch,x1,y1,x2,y2);
    ^
    Window.java:337: error: Illegal static declaration in inner class Window.Line
    static Line fromInput() {
    ^
    modifier 'static' is only allowed in constant variable declarations
    Window.java:373: error: non-static variable this cannot be referenced from a static context
    if (In.done()) return new Rectangle(ch,x,y,w1,h);
    ^
    Window.java:366: error: Illegal static declaration in inner class Window.Rectangle
    static Rectangle fromInput() {
    ^
    modifier 'static' is only allowed in constant variable declarations
    Window.java:399: error: non-static variable this cannot be referenced from a static context
    if (In.done()) return new Circle(ch,x,y,r);
    ^
    Window.java:393: error: Illegal static declaration in inner class Window.Circle
    static Circle fromInput() {
    ^
    modifier 'static' is only allowed in constant variable declarations
    8 errors

Page 1 of 2 12 LastLast

Similar Threads

  1. Java not drawing my string
    By bartreijmer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 08:09 AM
  2. Drawing Image in Java
    By Shania_01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2013, 12:10 PM
  3. [SOLVED] Drawing lines using angles in Java HELP
    By shadysback in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 8th, 2012, 02:10 PM
  4. [SOLVED] Java Graphics: Drawing one object into another
    By zaxahmed in forum Object Oriented Programming
    Replies: 5
    Last Post: August 22nd, 2011, 08:06 AM
  5. Incremental image drawing in Java
    By ea25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2011, 07:58 AM