|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-03-2008, 04:34 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
|
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!
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);
}
}
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) {}
}
}
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;
}
|
|

07-03-2008, 05:49 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 939
|
|
|
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?
|
|

07-03-2008, 05:56 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
|
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.
|
|

07-03-2008, 07:42 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 939
|
|
|
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 07:47 PM.
|
|

07-03-2008, 09:43 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
|
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?
|
|

07-03-2008, 10:39 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 939
|
|
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
|
|

07-04-2008, 12:51 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,144
|
|
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");
}
};
}
|
|

07-07-2008, 02:05 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 6
|
|
Originally Posted by Norm
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!
|
|

07-07-2008, 03:46 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 939
|
|
|
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
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|