View Single Post
  #1 (permalink)  
Old 07-03-2008, 05:34 PM
rstepler rstepler is offline
Member
 
Join Date: Jul 2008
Posts: 6
rstepler is on a distinguished road
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!



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); } }
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) {} } }
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; }
Reply With Quote
Sponsored Links