Results 1 to 9 of 9
Thread: simple drawing program
- 08-12-2011, 12:33 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
simple drawing program
Hey was wondering if you guys could proof my code. I'm trying to write a simple program that lets you draw lines,circles, and polygons. I know that I need to do current state etc, but what I am have trouble with is drawing the circle and polygon. any insight would be nice. Also please let me know why my lines are always connecting to the previous line. here are my codes (sorry as i do not know how to add code with out copying and pasting).
package SimpleDrawing;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class drawingFrame extends JFrame {
JPanel drawingArea;
JButton line,circle,polygon,erase;
JPanel buttonPanel;
drawingFrame(String title){
super(title);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
buttonPanel.add(line = new JButton("LINE"));
line.setHorizontalAlignment(JLabel.RIGHT);
buttonPanel.add(circle = new JButton("CIRCLE"));
circle.setHorizontalAlignment(JLabel.RIGHT);
buttonPanel.add(polygon = new JButton("POLYGON"));
circle.setHorizontalAlignment(JLabel.RIGHT);
buttonPanel.add(erase = new JButton("ERASE"));
circle.setHorizontalAlignment(JLabel.RIGHT);
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
//creates a studentinfopanel object
drawingArea = new JPanel();
//adds both the button panel and the studentinfopanel object to the JDialog, sets it visible
//and disposes of it on close.
add(buttonPanel);
add(drawingArea);
setSize(800,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
public static void main(String args[]){
drawingFrame frame = new drawingFrame("Drawing Tool");
}
}
package SimpleDrawing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;
import javax.swing.*;
public class drawingProgram {
drawingFrame drawingtest = new drawingFrame("Drawing Tool");
int x1,y1,x2,y2,width,height,num;
drawingProgram(){
drawingtest.setVisible(true);
drawingtest.line.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
lineButton();
}
public void lineButton() {
// TODO Auto-generated method stub
drawingtest.drawingArea.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
num = (Integer) e.getSource();
drawingtest.drawingArea.getGraphics().drawPolygon( null, null, num);
}
});
}
});
drawingtest.circle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
circleButton();
}
public void circleButton() {
// TODO Auto-generated method stub
drawingtest.drawingArea.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
drawingtest.drawingArea.getGraphics().drawOval(x1, y1, x2, y2);
}
});
}
});
drawingtest.polygon.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
polygonButton();
}
public void polygonButton() {
// TODO Auto-generated method stub
drawingtest.drawingArea.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
drawingtest.drawingArea.getGraphics().drawLine(x1, y1, x2, y2);
}
});
}
});
drawingtest.erase.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawingtest.drawingArea.setVisible(false);
}});
}
public static void main(String args[]){
drawingProgram dProgram = new drawingProgram();
}
}
- 08-12-2011, 02:23 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
See Custom Painting Approaches « Java Tips Weblog for some ideas to get your started.
- 08-12-2011, 02:30 AM #3
To wrap your code in code tags and preserve formatting, use the # icon above the input box.add code with out copying and pasting)
What errors do you get when you execute the code? I got this first thing:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to java.lang.Integer
at DrawingFrame$DrawingProgram$1$1.mouseReleased(Draw ingFrame.java:82)Last edited by Norm; 08-12-2011 at 02:39 AM.
- 08-12-2011, 04:20 AM #4
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
Well it had compliation or runtime errors. I'll put it in points as to whaat confuses me.
- my lines seem to be setting their first point at the last point of the previous line. How do I end one line and begin another separately?
- if I use getGraphics().drawOval(x1,y1,width,heightt); how do I. Get width and heighht?
- how do I get number of lines need to form a polygon?
- 08-12-2011, 05:16 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Don't use getGraphics().
- 08-12-2011, 05:27 AM #6
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
Did you try repaint(/*optionally an area*/) and use the Graphics (Graphics2D) supplied by the method paint(Graphics).
Optionally with double buffering using BufferedImage (using its getGraphics(), or whatever the method is called, maybe it is createGraphics()).Last edited by Hibernate; 08-12-2011 at 05:29 AM.
Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-12-2011, 05:30 AM #7
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
- 08-12-2011, 05:32 AM #8
- Join Date
- Dec 2010
- Location
- Stockholm, Sweden
- Posts
- 222
- Blog Entries
- 9
- Rep Power
- 3
@OP
It would be nice if used code tags (the # symbol) and indents when you post your code (you can edit your post and do so).
It would be much easier to read the code then.Ex animo! Hibernate
Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.
- 08-12-2011, 01:53 PM #9
Your approach is quite different for drawing. Most apps do it this way:
Create a new class that extends JPanel and override the paintComponent method where you'll add code to draw the lines and shapes determined by other code, such as a mouse listener. Add this class to your GUI.
The mouselistener will determine where the cursor is and save the information needed to draw the shapes. When all the data needed for a shape is obtained, the listener creates instructions for the paintComponent method and calls the repaint method. A short time later the JVM will call the paintComponent method which will look at the instructions saved by the listener code and draw the shape(s) that have been saved.
Similar Threads
-
Problem with drawing program
By gameaddict123 in forum New To JavaReplies: 2Last Post: 07-20-2011, 03:55 AM -
Simple program, simple problem
By taymilll in forum New To JavaReplies: 12Last Post: 06-20-2011, 05:12 AM -
Drawing simple shapes using loops
By Quinn in forum New To JavaReplies: 2Last Post: 05-29-2011, 09:05 AM -
Drawing simple graphics in NetBeans
By arifin in forum AWT / SwingReplies: 1Last Post: 11-12-2010, 10:01 AM -
Simple drawing on a JPanel
By Catfish1 in forum New To JavaReplies: 2Last Post: 10-04-2010, 03:59 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks