Results 1 to 15 of 15
  1. #1
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default PaintComponant Drawing help?

    Hello, my program is for printing multiple dots on the screen, i want, everytime i click the mouse a new dot is drawn. The problem is that it draws on first click, then Re-draws the dot wherever you click again, rather simple just adding a new dot like i would like. Her is my code.

    Main:
    Java Code:
    package drawingtest;
    import javax.swing.*;
    class Main extends JFrame{
    
        public void frameSetup(){
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Gui g = new Gui();
            g.PanelOneSetup();
            add(g);
            setSize(200, 200);
            setResizable(true);
            setLocationRelativeTo(null);
            setVisible(true);
        }
        public static void main(String[] args) {
            Main m = new Main();
            m.frameSetup();
        }
    
    }
    Gui
    Java Code:
    package drawingtest;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    
    public class Gui extends JPanel{
    
        int px;
        int py;
    
    
        public void PanelOneSetup(){
            addMouseListener(new mlAdapter());
            setBackground(Color.LIGHT_GRAY);
        }
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            System.out.println("Painted");
            g2d.setColor(Color.BLACK);
            g2d.drawOval(px, py, 3, 3);
            g2d.fillOval(px,py,3,3);
            }
         private class mlAdapter extends MouseAdapter {
    
            public void mousePressed(MouseEvent e) {
                px = e.getX();
                py = e.getY();
                repaint();
            }
        }
    }
    Please help! haha.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    you could remove the line super.paintComponent(g); :D
    or:
    use a list and store points in it. in your paintcomponent iterate over the list and draw the points.

    or:
    dont draw directly on the panel, but draw on a e.g. bufferedimage and draw only that image in your paintcomponent !

  3. #3
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Oh wow fail on my part! I see, i understand the super.paintComponent(g); and the iterating over the list, but i dont understand the buffered image.

  4. #4
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    How do i clear the canvas if i do not iterate it over a list or make a buffered image?

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    For example, like:
    Java Code:
    package drawingtest;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    
    public class Gui extends JPanel {
    
    	int px;
    	int py;
    	private BufferedImage image;
    
    	public void PanelOneSetup() {
    		addMouseListener(new mlAdapter());
    		setBackground(Color.LIGHT_GRAY);
    	}
    
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		if (image == null) {
    			image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
    			Graphics2D g2d = image.createGraphics();
    			g2d.setColor(Color.WHITE);
    			g2d.fillRect(0, 0, getWidth(), getHeight()); //clear
    		}
    		System.out.println("Painted");
    		g.drawImage(image, 0, 0, null);
    	}
    
    	private class mlAdapter extends MouseAdapter {
    
    		public void mousePressed(MouseEvent e) {
    			px = e.getX();
    			py = e.getY();
    			Graphics2D g2d = image.createGraphics();
    			g2d.setColor(Color.BLACK);
    			g2d.drawOval(px, py, 3, 3);
    			g2d.fillOval(px, py, 3, 3);
    			repaint();
    		}
    	}
    }
    the image/canvas size is equal to the panels first size! if you don`t want that, think about it :p

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, if you create a Graphics or Graphics2D object from the BufferedImage, don't forget to call dispose() on it when you're done with it to save on system resources.

  7. #7
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Now im wondering how too draw a grid, i would normally do something like this

    Java Code:
    int x = 0;
            int y = 0;
    
            for (int i = 0; i < getWidth(); i += 5) {
                g2d.drawLine((getWidth() / 2) - x, 5, (getWidth() / 2) - x, getHeight() - 5);
                g2d.drawLine((getWidth() / 2) + x, 5, (getWidth() / 2) + x, getHeight() - 5);
                g2d.drawLine((getWidth() / 2) - 1, 5, (getWidth() / 2) - 1, getHeight() - 5);
                g2d.drawLine((getWidth() / 2) + 1, 5, (getWidth() / 2) + 1, getHeight() - 5);
                x = x + 10;
            }
            for (int i = 0; i < getHeight(); i += 5) {
                g2d.drawLine(5, (getHeight() / 2) - y, getWidth() - 5, (getHeight() / 2) - y);
                g2d.drawLine(5, (getHeight() / 2) + y, getWidth() - 5, (getHeight() / 2) + y);
                g2d.drawLine(5, (getHeight() / 2) - 1, getWidth() - 5, (getHeight() / 2) - 1);
                g2d.drawLine(5, (getHeight() / 2) + 1, getWidth() - 5, (getHeight() / 2) + 1);
                y = y + 10;
            }
    Please help!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    So what happens when you do that? And what is the code now?

  9. #9
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Nothing happens, no mather where i pute that snipped, nothing happens.

  10. #10
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    Oooops! haha sorry, it does work! I have a bit of a problem, i can draw a grid and click point to that grid, but, is there a way to make 0,0 the center of the JPanel? rather then just the upper left corner?

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    is there a way to make 0,0 the center of the JPanel?

    I'm not sure I understand what this means. The graphics commands work in coordinates with the origin at the top left corner. You can't change that: it's just the way the Graphics class works. However your application is free to use any coordinate system it likes - although when it wants to use the graphics drawing methods it will have to use the width and height of the component to convert into something suitable for the Graphics class.

    ---------------------

    Actually that's not quite true: you can apply a transform to the graphics instance so that application coordinates are interpreted correctly. But that is conceptually more complicated. The most straightforward thing is to add/subtract half a width/height as required.

  12. #12
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    What im saying is, to size of of the JPanel is in this example 100,100. If i wanted to draw something, i would have to keep in mind, that 100,100 is the lower right corner, and 0,0 is the upper left. But, if you have ever used a graphing calculator, it uses 0,0 as the center, and the four quadrant around it, the +,+ quadrant is the upper right, the -,+ quadrant is the upper left quarter, -,- is the lower left, and +,- is the lower right. I would like to be able to draw according to the quadrant based grid.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    As I said you (A) either do the conversion from app coords to Graphics coords yourself, possibly with some helper methods:

    Java Code:
        /**
         * Draws oval based on (px,py) given in app coords with (0,0) in the center
         * and increasing x/y being right/up.
         */
    static drawOval(Graphics2D g2d, int px, int py, int w, int h) {
        int x = px - w / 2;
        int y = w / 2 - py;  // I *think* these two are right...
        g2d.drawOval(x, y, 3, 3);
    }

    Or (B) look at the Graphics2D method transform() and the associated AffineTransform class. In your case you want to apply a glide reflection to the graphics context.

  14. #14
    Jcbconway is offline Senior Member
    Join Date
    Sep 2010
    Posts
    109
    Rep Power
    0

    Default

    So, i use px,py to determine x,y and draw my oval by x,y...that through selected math determins the position radiating from the center?

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Yes. I meant x and y to be coordinates that Graphics would understand and px and py to be your application coordinates with (0,0) at the center.

    So you would use the helper method instead of the Graphics one:

    Java Code:
    g2d.setColor(Color.BLACK);
    //g2d.drawOval(px, py, 3, 3);
    drawOval(g2d, px, py); // px and py will get converted and the oval drawn

    I think I did make a mistake and it should say "int x=px+w/2". Shameful to admit, but my approach is to throw my "best guess" at the maths at the Java runtime and see if things move backwards or whatever.

Similar Threads

  1. Using Piccolo for Drawing
    By rstepler in forum Java 2D
    Replies: 2
    Last Post: 08-20-2013, 11:03 AM
  2. PaintComponant help
    By Jcbconway in forum Advanced Java
    Replies: 7
    Last Post: 11-19-2010, 02:17 AM
  3. Drawing disappear!!!!
    By gan5016 in forum Java 2D
    Replies: 7
    Last Post: 09-26-2009, 03:58 AM
  4. Drawing a map
    By Karp in forum AWT / Swing
    Replies: 4
    Last Post: 11-07-2008, 01:26 PM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •