drawing thick line
Hi,
For some graphical user interface, I was overriding PaintComponent() method with Graphics argument and then using drawLine() method of Graphics class of Java. It worked in the way I wanted. However, the lines drawLine() method draws are too thin. For that purpose, I defined paintComponent() method with Graphics2D class to use its setStroke() method. I left all previously defined methods, namely drawRect() and fillOval() unchanged due to the fact that Graphics2D class extend Graphics class. However, now no input is displayed on myJFrame although I get no compile errors. Has anyone idea about the problem?
Thanks in advance,
[652 byte] By [
safisha] at [2007-11-27 5:26:11]

public void paintComponent(Graphics2D g)
{
for(int i=0; i<myBoard.size; i++)
for(int j=0; j><myBoard.size; j++)
{
g.setColor (Color.black);
g.drawRect (50*j, 50*i, 50, 50);
if(myBoard.board[i*2][j*2] == 1)
{
g.setColor (Color.black);
g.fillOval (50*j, 50*i, 50, 50);
}
else if(myBoard.board[i*2][j*2] == 2)
{
g.setColor (Color.lightGray);
g.fillOval (50*j, 50*i, 50, 50);
}
}
int width = 3;
BasicStroke bs = new BasicStroke(width);
g.setStroke(bs);
g.setColor (Color.red);
for(int i=0; i><myBoard.size*2-1; i++)
for(int j=0; j><myBoard.size*2-1; j++)
{
if(myBoard.board[i][j] == 3)
g.drawLine(j*25, (i+1)*25, (j+2)*25, (i+1)*25);
else if(myBoard.board[i][j] == 4)
g.drawLine((j+1)*25, i*25, (j+1)*25, (i+2)*25);
}
}
public static void main(String args[])
{
JFrame frame = new JFrame("Output");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout());
Main panel = new Main();
frame.add(panel);
frame.setSize(50*myBoard.size+8, 50*myBoard.size+33);
frame.setVisible(true);
}
I was hoping to see thick lines but there is displayed nothing on the frame, I don't know why (Why?). (It gives thin lines when I change argument of paintComponent from Graphics2D to Graphics and comment out 3 lines using setStroke().)>
Thanks for posting some code, but try again. First, most of what you posted is
irrelevant (the ovals, the colors) and second, you didn't post a *complete*
program -- for all I know, the bug is what you omitted.
Again, post a small (<1 page) complete *example* program that demonstrates
your problem, and nothing else. Omit what is irrelevant. Concentrate on
just the matter at hand.
Here is an example of what you should have posted. However, the stroke
works for me!
import java.awt.*;
import javax.swing.*;
public class StrokeExample extends JPanel implements Runnable {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
g.drawLine(0,0,w,h);//default
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
g2.drawLine(0,h,w,0);//thick
}
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new StrokeExample());
f.setSize(500,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new StrokeExample());
}
}