Results 1 to 9 of 9
  1. #1
    rstepler is offline Member
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default repainting lines separately

    I am creating a GUI which has three lines. I am trying to get each line to blink separately with each being drawn onto a JPanel (simulation Panel). I have made each line their own class that only differ in where there are drawn in the panel. I have tried a couple different things to get only one line to repaint but nothing seems to be working. I would greatly appreciate any suggestions. Thank you!



    Java Code:
    class SimulationPanel extends JPanel {
    		LineOne lineone = new LineOne();
    		LineTwo linetwo = new LineTwo();
    		LineThree linethree = new LineThree(); 
    		
    		public SimulationPanel () {
    			setLayout(null);
    			ImageIcon ngo1 =  new ImageIcon("C:/Users/Renee/Desktop/toolbarButtonGraphics/INET_Earth_Clear.png");
    			JLabel worldLabel = new JLabel(ngo1);
    			Dimension size = worldLabel.getPreferredSize();
    			worldLabel.setBounds(390, 75, size.width, size.height);
    			add(worldLabel);
    			ImageIcon ngo2 =  new ImageIcon("C:/Users/Renee/Desktop/toolbarButtonGraphics/INET_Earth_Clear.png");
    			JLabel ngo2Label = new JLabel(ngo2);
    			Dimension ngo2Size = ngo2Label.getPreferredSize();
    			ngo2Label.setBounds(110, 300, ngo2Size.width, ngo2Size.height);
    			add(ngo2Label);
    			ImageIcon ngo3 =  new ImageIcon("C:/Users/Renee/Desktop/toolbarButtonGraphics/INET_Earth_Clear.png");
    			JLabel ngo3Label = new JLabel(ngo3);
    			Dimension ngo3Size = ngo3Label.getPreferredSize();
    			ngo3Label.setBounds(730, 300, ngo3Size.width, ngo3Size.height);
    			add(ngo3Label);
    			
    		}
    
    		public void paint(Graphics comp) {	
    			super.paint(comp);
    			Graphics2D comp2D = (Graphics2D) comp
    			Font titleFont = new Font("Serif", Font.BOLD , 24);
    			setFont(titleFont);
    			String str = "Simulation";
    			comp2D.drawString(str, 15, 50);
    			
    			LineOne.paintLineOne(comp);
    			LineTwo.paintLineTwo(comp); 
    			LineThree.paintLineThree(comp); 
    		
    			}
    }
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    		
    class Runner extends Thread   {
    	
    	public SimulationPanel sp;
    	
    	public Runner (SimulationPanel simpanel){
    			this.sp=simpanel;
    			
    	}
    	
    	
    	public void run(Graphics comp) {
    	
    		
    		LineOne.blinkLineOne(comp,true); 
    		sp.repaint();
    		delay(100);
    		
    		}
    	
    	void delay(int milliseconds) {
    		try {
    			Thread.sleep(milliseconds);
    		}
    		catch (InterruptedException e) {}
    	}
    
    	
    	
    }
    Java Code:
    public class LineOne {
    	private static int xPos1 =500;
    	static int xPos2 = 800;
    	static int ypos1 = 150;
    	static int yPos2 = 350; 
    	
    	public void setX(int xPos1){
    		this.xPos1=xPos1;
    	}
    	public void setX2(int xPos2){
    		this.xPos2=xPos2;
    	}
    	public void setY(int ypos1){
    		this.ypos1=ypos1;
    	}
    	public void setY2(int yPos2){
    		this.yPos2=yPos2;
    	}
    	public static int getX(){
    		return xPos1; 
    	}
    	public static int getX2(){
    		return xPos2;
    	}
    	public static int getY(){
    		return ypos1;
    	}
    	public static int getY2(){
    		return yPos2;
    	}
    
    	
    public static void paintLineOne(Graphics comp){
    		Graphics2D comp2D = (Graphics2D) comp;
    		BasicStroke stroke2 = new BasicStroke(2);
    		comp2D.setStroke(stroke2);
    		comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		comp.drawLine(getX(),getY(),getX2(), getY2()); 
    	}
    
    
    public static boolean blinkLineOne(Graphics comp, boolean isVisible){
    	Graphics2D comp2D = (Graphics2D) comp;
    	if (isVisible == true){
    		comp2D.setPaint(Color.WHITE);
    		paintLineOne(comp);
    		 isVisible = false;
    	} else{
    		comp2D.setPaint(Color.BLACK);
    		paintLineOne(comp);
    		isVisible = true;
    	}
    	return isVisible;
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    I don't see any comments in your code that describes your logic to make the lines blink at different rates. Could you explain how you are trying to do this?

  3. #3
    rstepler is offline Member
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    not so much at different rates. I would like to call each line to blink at different times. My problem right now it simply calling one line to blink. I'm unsure of how to set up methods to address the problem.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    A blink would be a time without the line showing and a time with the line showing. Do a repaint() every so often and have the painting method draw the line one time and then erase it the next. Use a boolean to keep track of which to do:
    boolean clearLine = false;
    ...
    clearLine = !clearLine; // switch modes

    One way to clear a line would be to paint the line with the same color as the background.


    Add some debugging println() statements to your code to see the control flow and the values of various variables. That might point out problems with your logic or a coding error.
    Last edited by Norm; 07-03-2008 at 06:47 PM.

  5. #5
    rstepler is offline Member
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    I guess my question was unclear. I have written code to make the graphics blink. However, when i did this i had lines painted from the JPanel therefore all the lines blinked when i did this. I have declared the lines as seperate classes now. How would I access only one line to make it blink?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    access only one line to make it blink
    Have an enabled switch in each class that you set on if you want its lines to blink

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class LineControl extends JPanel {
        Color[] colors = { Color.red, Color.green.darker(), Color.blue };
        Line2D.Double[] lines;
        int sequenceNumber = 0;
        int lineNumber = 0;
    
        public LineControl() {
            lines = new Line2D.Double[3];
            lines[0] = new Line2D.Double(100,330,140,200);
            lines[1] = new Line2D.Double(100,330,200,230);
            lines[2] = new Line2D.Double(100,330,300,330);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            // Two options to choose from:
            // 1 - To show the unfolding sequence.
            AffineTransform at = AffineTransform.getTranslateInstance(0,-150);
            for(int i = 0; i < sequenceNumber; i++) {
                g2.setPaint(colors[i]);
                g2.draw(at.createTransformedShape(lines[i]));
            }
    
            // 2 - To show only one line at a time.
            if(lineNumber > 0 && lineNumber <= lines.length) {
                g2.setPaint(colors[lineNumber-1]);
                g2.draw(lines[lineNumber-1]);
            }
        }
    
        private void start() {
            Thread thread = new Thread(runner);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            LineControl test = new LineControl();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    
        private Runnable runner = new Runnable() {
            public void run() {
                do {
                    try {
                        Thread.sleep(2000);
                    } catch(InterruptedException e) {
                        System.out.println("interrupt");
                        break;
                    }
                    if(sequenceNumber < lines.length)
                        sequenceNumber++;
                    lineNumber++;
                    System.out.println("lineNumber = " + lineNumber);
                    repaint();
                } while(lineNumber <= lines.length);
                System.out.println("exit");
            }
        };
    }

  8. #8
    rstepler is offline Member
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Have an enabled switch in each class that you set on if you want its lines to blink
    I'm not really sure what you mean. Could you explain or send me a website for reference? Thank you!

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    define a boolean okToBlink and have a method the sets it
    public void setOkToBlink(boolean b) {
    oKToBlink = b;
    }
    The in the code that does the blinking:
    if (oktoBlink) {
    // code to blink here
    }

Similar Threads

  1. how can we remove blank lines from a .txt
    By Camden in forum New To Java
    Replies: 12
    Last Post: 07-29-2011, 01:38 PM
  2. Demonstration of drawing lines in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:27 PM
  3. Replies: 0
    Last Post: 04-06-2008, 07:45 PM
  4. how to edit lines.
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:41 AM
  5. Unify these 3 lines of code
    By ulykidjoe in forum Advanced Java
    Replies: 4
    Last Post: 07-13-2007, 01:15 PM

Posting Permissions

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