Draw LIne
Hey i need some help with this program. I am trying to draw a line sorta like a figure 8 (if you know what that is). I'm supposed to draw a line that moves up 10 units, moves down 10 units , moves right 10 units, and moves left 10 units....I have all the code done, just i have the x and y things messed up . Can anyone get this to work?
package LabChp06;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* implement a drawing tool program that allows the user to move a pen around the window.
* The pen will have two states, up and down. when the pen is down, it will leave a trail
* on the drawing canvas as it moves. Basic operations, such as moveUp, moveDown, etc will
* be used to control the postion of the pen.
*/
public class DrawingTool {
JFrame canvas; //drawing window
int penX, penY; //pen position
Color penColor; //(up=Color.WHITE, down=Color.Black)
Graphics g; // used to do the actual drawing
/*
* A pen is created at the center of a 200X200 pixel drawing window.
*/
public DrawingTool(){
penColor = Color.WHITE;
JFrame canvas = new JFrame();
canvas.getContentPane().setLayout(new GridLayout(1,1));
canvas.setSize(200,200);
BufferedImage img;
img = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,200,200);
penX = 100;
penY = 100;
ImageIcon icon = new ImageIcon(img);
JLabel Rect = new JLabel(icon);
canvas.getContentPane().add(Rect);
canvas.setVisible(true);
}
/*
* The pen is set to be down on the canvas.
*/
public void draw()
{
penColor = (Color.BLACK);
}
/*
* The pen is set to be up off of the canvas
*/
public void dontDraw()
{
g.setColor(Color.WHITE);
}
/*
* The pen is moved 10 units up towards the top of the screen.
*/
public void moveUp()
{
g.setColor(penColor);
g.drawLine(penX,penY,penX,penY+10);
penY = penY +10;
}
/*
* The pen is moved 10 units down towards the bottom of the screen.
*/
public void moveDown()
{
g.setColor(penColor);
g.drawLine(penX,penY,penX,penY-10);
penY = penY -10;
}
/*
* The pen is moved right 10 units.
*/
public void moveRight()
{
g.setColor(penColor);
g.drawLine(penX,penY,penX,penX+10);
penX = penX +10;
}
/*
* The pen is moved left 10 units.
*/
public void moveLeft()
{
g.setColor(penColor);
g.drawLine(penX,penY,penX,penX+10);
penX = penX -10;
}
}
This may or may not be your problem (I didn't run your code). But:"y - 10" means up towards top of screen."y + 10" means down towards top of screen.Yes, this is opposite of what you normally think of for mathematical coordinates.
MLRona at 2007-7-15 18:15:07 >

Yeh i know that stuff...i'm just confused why it won't show up on the screen. just create a new project and package...then use the code from the previous post as one class of the package, and this code as the other class of the package. Run the program and see what i mean.The other code is as follows.
package LabChp06;
/*Building a Driver Class
*
*/
public class Figure8 {
public static void main(String[] args) {
DrawingTool dt = new DrawingTool();
dt.draw();
dt.moveUp();
dt.moveDown();
dt.moveLeft();
dt.moveRight();
dt.dontDraw();
}
Where is the connection between the BufferedImage you create and the canvus? You have created an image, now now need to display it on some visual medium. I'm not a Swing programmer and can't help you there.
In AWT, you display images by using a Graphics.draw...() method in the paint() method using the Graphics object passed to the paint() method.
no that is not it.......it has to do with the penX and penY coordinates. Somehwo they just don't do what they are supposed to.
A couple things:
I'd preffer to nake the class inherit from JFrame instead of just using one. Then, I'd make all the methods to make only calculations, and all the drawing in the public void paint(Graphics g) method.
Another thing, if you want to do it this way, is is important that you should initialize g as:
g = canvas.getGraphics();
// you need to get the JFrame Graphics, becouse it
// is where you want the rectangle to be seen
try a couple of this suggestions and tell me how it goes.
Just another thing:I think it is useless to use a BufferedImage and the JLabel... all you want to do is to draw a rect, and that's by simply use g.fillRect function...
I would subclass a JLabel and override the paint method.
Furthermore, the logic for moveUp() and moveDown() is backwards, it should be drawLine(penX, penY, penX, penY + 10) for DOWN and drawLine(penX, penY, penX, penY - 10) for UP. This was already mentioned, but apparently ignored. Beyond that, the logic for moveRight and moveLeft is way off. It should be drawLine(penX, penY, penX + 10, penY) for moveRight and drawLine(penX, penY, penX - 10, penY) for left.
Beyond that, after looking at your code I'm not even sure it should work at all with the way you're getting the graphics context.
ok, now i have this......here is the rest of my code, i don't know what the hell is wrong with it. I think im supposed to a "plus" image within the rectangle.
Im getting nothing even close to this?
package LabChp06;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*
* implement a drawing tool program that allows the user to move a pen around the window.
* The pen will have two states, up and down. when the pen is down, it will leave a trail
* on the drawing canvas as it moves. Basic operations, such as moveUp, moveDown, etc will
* be used to control the postion of the pen.
*/
public class DrawingTool {
JFrame canvas; //drawing window
int penX, penY; //pen position
Color penColor; //(up=Color.WHITE, down=Color.Black)
Graphics g; // used to do the actual drawing
/*
* A pen is created at the center of a 200X200 pixel drawing window.
*/
public DrawingTool(){
penColor = Color.WHITE;
JFrame window = new JFrame();
window.getContentPane().setLayout(new GridLayout(1,1));
window.setSize(200,200);
BufferedImage img;
img = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,200,200);
penX = 100;
penY = 100;
ImageIcon icon = new ImageIcon(img);
JLabel Rect = new JLabel(icon);
window.getContentPane().add(Rect);
window.setVisible(true);
}
/*
* The pen is set to be down on the canvas.
*/
public void draw()
{
penColor = Color.BLACK;
}
/*
* The pen is set to be up off of the canvas
*/
public void dontDraw()
{
g.setColor(Color.WHITE);
}
/*
* The pen is moved 10 units up towards the top of the screen.
*/
public void moveUp()
{
g.setColor(penColor);
g.drawLine(penX, penY, penX, penY - 10);
penY = penY - 10;
}
/*
* The pen is moved 10 units down towards the bottom of the screen.
*/
public void moveDown()
{
g.setColor(penColor);
g.drawLine(penX, penY, penX, penY + 10);
penY = penY + 10;
}
/*
* The pen is moved right 10 units.
*/
public void moveRight()
{
g.setColor(penColor);
g.drawLine(penX, penY, penX + 10, penY);
penX = penX + 10;
}
/*
* The pen is moved left 10 units.
*/
public void moveLeft()
{
g.setColor(penColor);
g.drawLine(penX, penY, penX - 10, penY);
penX = penX - 10;
}
}
