"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!
Here it is done in Swing. Everything is done in the EDT, as is intended with Swing as it is not thread safe. It is double buffered.
Why the JLabel/Icon combination? It's just the best way to do it, as far as I have discovered, and I'd struggle to give you a historical/technical explanation of why - that's just the way it seems to have been designed. The other approach is to get involved with BufferStrategy but that starts to get more complicated IMHO.
Here it is done in Swing. Everything is done in the EDT, as is intended with Swing as it is not thread safe. It is double buffered.
Why the JLabel/Icon combination? It's just the best way to do it, as far as I have discovered, and I'd struggle to give you a historical/technical explanation of why - that's just the way it seems to have been designed. The other approach is to get involved with BufferStrategy but that starts to get more complicated IMHO.
Import javax.swing. *; import java.awt. *; import java.awt.event.
*; import java.awt.geom. *; public class LineDrawSwing extends JLabel implements Icon { Line2D line1, line2; public LineDrawSwing() { this. SetIcon(this); } Stroke drawingStroke1 = new BasicStroke(3, BasicStroke.
CAP_BUTT, BasicStroke. JOIN_BEVEL, 0, new float { 9 }, 0); public static void main(String args) { JFrame frame = new JFrame(); frame. AddWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.
Exit(0); } }); frame. Add(new LineDrawSwing()); frame.validate(); frame. SetSize(300, 250); frame.
SetVisible(true); } @Override public void paintIcon(Component c, Graphics g, int x, int y) { Dimension size = getSize(); line1 = new Line2D. Double(size. Width/2, size.
Height-10, 10, 10); line2 = new Line2D. Double(size. Width/2, size.
Height-10, size. Width-10, 10); Graphics2D graph = (Graphics2D) g; graph. SetPaint(Color.
Red); graph. Draw(line2); graph. SetStroke(drawingStroke1); graph.
SetPaint(Color. Green); graph. Draw(line1); } @Override public int getIconHeight() { return getSize().
Height; } @Override public int getIconWidth() { return getSize(). Width; } }.
If sticking with AWT, I would use a ComponentListener to track the size changes for the Frame and reset the line coordinates accordingly. You may get away with creating/updating the lines in the Frame.paint() context, but that's just not a very clean implementation, with a lot of implied logic and assumptions and, therefore, probably some issues. So here's the ComponentListener approach.
I had to make a few assumptions about where you wanted your lines to get drawn from/to, as you were not clear on this. (If you can be clearer on this, I can update the example. ) import java.awt.
*; import java.awt.event. *; import java.awt.geom. *; public class LineDraw extends Canvas implements ComponentListener { Line2D line1, line2; public LineDraw() { this.
AddComponentListener(this); } // set up lines every size update public void componentResized(ComponentEvent e) { Dimension size = getSize(); line1 = new Line2D. Double(size. Width/2, size.
Height-10, 10, 10); line2 = new Line2D. Double(size. Width/2, size.
Height-10, size. Width-10, 10); } // required to satisfy ComponentListener interface public void componentdden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } // paint, main both as before Stroke drawingStroke1 = new BasicStroke(3, BasicStroke. CAP_BUTT, BasicStroke.
JOIN_BEVEL, 0, new float { 9 }, 0); public void paint(Graphics g) { Graphics2D graph = (Graphics2D) g; graph. SetPaint(Color. Red); graph.
Draw(line2); graph. SetStroke(drawingStroke1); graph. SetPaint(Color.
Green); graph. Draw(line1); } public static void main(String args) { Frame frame = new Frame(); frame. AddWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.
Exit(0); } }); frame. Add(new LineDraw()); frame.validate(); frame. SetSize(300, 250); frame.
SetVisible(true); } }.
1 Updated it to reflect a good point made by @PaÅlo Ebermann about not painting to the Frame directly. The above code is tested / works. However it is not double buffered, but that's another matter altogether and depends on how you want to approach it.
I will post another answer that uses Swing. – Charles Goodwin Jul 12 at 14:21 Thanks Charles I fixed my problem with your code. – SunyGirl Jul 22 at 19:32 @SunyGirl: click on the 'tick' then and accept the answer.
;-) – Charles Goodwin Jul 22 at 19:35.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.