Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Problem withe Jpanel while drawing shapes
looka dicosta
Greenhorn
Posts: 21
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
based on your (recent) poor history, I'm not gunna offer too much,
but mouseClicked() is *always* a poor choice, as it doesn't always fire.
no idea if that's your problem, but that's all a quick glance warrants.
looka dicosta
Greenhorn
Posts: 21
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Not much to say on this. But at least you can give person another chance. I was not deliberate as you say in "recent history" . But I am facing some serious issues with my work. Hope you can take this as my apology. Please help !!!
Michael Dunn
Ranch Hand
Posts: 4632
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
no one can help if once you've deleted the code.
for other (kinder) folk, willing to help, here's the deleted code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Iterator; //////////////////////////////////////////////////////////////PaintDemo class PaintDemo { //============================================================= main public static void main(String[] args) { PaintWindow window = new PaintWindow(); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); }//end main }//endclass PaintDemo ////////////////////////////////////////////////////////////PaintWindow class PaintWindow extends JFrame { PaintPanel canvas = new PaintPanel(); //====================================================== constructor public PaintWindow() { //--- create the buttons JButton circleButton = new JButton("Circle"); circleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setShape(PaintPanel.CIRCLE); }}); JButton rectangleButton = new JButton("Rectangle"); rectangleButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setShape(PaintPanel.RECTANGLE); }}); JButton polyButton = new JButton("Polygon"); polyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { canvas.setShape(PaintPanel.POLY); }}); //--- layout the buttons JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2, 1)); buttonPanel.add(circleButton); buttonPanel.add(rectangleButton); buttonPanel.add(polyButton); //--- layout the window Container content = this.getContentPane(); content.setLayout(new BorderLayout()); content.add(buttonPanel, BorderLayout.WEST); content.add(canvas , BorderLayout.CENTER); this.setTitle("Paint Demo"); this.pack(); }//end constructor }//endclass PaintWindow ///////////////////////////////////////////////////////////// PaintPanel class PaintPanel extends JPanel implements MouseListener, MouseMotionListener { //--- Public constants used to specify shape being drawn. public static final int NONE = 0; public static final int LINE = 1; public static final int RECTANGLE = 2; public static final int CIRCLE = 3; public static final int POLY = 4; //--- Variables to store the current figure info private int _shape = NONE; public int getShape() { return _shape; } private int _currentStartX = 0; // where mouse first pressed private int _currentStartY = 0; private int _currentEndX = 0; // where dragged to or released private int _currentEndY = 0; //--- BufferedImage to store the underlying saved painting. // Will be initialized first time paintComponent is called. private BufferedImage _bufImage = null; private boolean polygonIsNowComplete = false; //--- Private constant for size of paint area. private static final int SIZE = 600; // size of paint area private final Point trackPoint = new Point(); private ArrayList points = new ArrayList(); //====================================================== constructor public PaintPanel() { setPreferredSize(new Dimension(SIZE, SIZE)); setBackground(Color.white); //--- Add the mouse listeners. this.addMouseListener(this); this.addMouseMotionListener(this); }//endconstructor //========================================================= setShape public void setShape(int shape) { //--- Provided so users can set the shape. _shape = shape; }//end setShape //=================================================== paintComponent public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; // downcast to Graphics2D if (_bufImage == null) { //--- This is the first time, initialize _bufImage int w = this.getWidth(); int h = this.getHeight(); _bufImage = (BufferedImage)this.createImage(w, h); Graphics2D gc = _bufImage.createGraphics(); gc.setColor(Color.white); gc.fillRect(0, 0, w, h); // fill in background } g2.drawImage(_bufImage, null, 0, 0); // draw previous shapes drawCurrentShape(g2); }//end paintComponent //================================================= drawCurrentShape private void drawCurrentShape(Graphics2D g2) { //--- Draws current shape on a graphics context, either // on the context passed to paintComponent, or the // context for the BufferedImage. switch (_shape) { case NONE : break; case CIRCLE: g2.drawOval(_currentStartX, _currentStartY, _currentEndX - _currentStartX, _currentEndY - _currentStartY); break; case RECTANGLE: g2.drawRect(_currentStartX, _currentStartY, _currentEndX - _currentStartX, _currentEndY - _currentStartY); break; case POLY: drawPolyGon(g2); break; default: // should never happen g2.drawString("Huh?", 10, 20); break; } }//end paintComponent private void drawPolyGon(Graphics2D g2) { int numPoints = points.size(); if (numPoints == 0) return; // nothing to draw Point prevPoint = (Point) points.get(0); // draw polygon @SuppressWarnings("rawtypes") Iterator it = points.iterator(); while (it.hasNext()) { Point curPoint = (Point) it.next(); draw(g2, prevPoint, curPoint); prevPoint = curPoint; } // now draw tracking line or complete the polygon if (polygonIsNowComplete){ draw(g2, prevPoint, (Point) points.get(0)); } else draw(g2, prevPoint, trackPoint); // TODO Auto-generated method stub } private void draw(Graphics2D g2, Point p1, Point p2) { int x1 = p1.x; int y1 = p1.y; int x2 = p2.x; int y2 = p2.y; // draw the line first so that the points // appear on top of the line ends, not below g2.setColor(Color.green.darker()); g2.drawLine(x1 + 3, y1 + 3, x2 + 3, y2 + 3); g2.drawLine(x1 + 4, y1 + 4, x2 + 4, y2 + 4); g2.drawLine(x1 + 5, y1 + 5, x2 + 5, y2 + 5); g2.setColor(Color.red); g2.fillOval(x1, y1, 8, 8); g2.setColor(Color.blue); g2.fillOval(x2, y2, 8, 8); // TODO Auto-generated method stub } //===================================================== mousePressed public void mousePressed(MouseEvent e) { _currentStartX = e.getX(); // save x coordinate of the click _currentStartY = e.getY(); // save y _currentEndX = _currentStartX; // set end to same pixel _currentEndY = _currentStartY; }//end mousePressed //===================================================== mouseDragged public void mouseDragged(MouseEvent e) { _currentEndX = e.getX(); // save new x and y coordinates _currentEndY = e.getY(); this.repaint(); // show new shape }//end mouseDragged //==================================================== mouseReleased public void mouseReleased(MouseEvent e) { // This will save the shape that has been dragged by // drawing it onto the bufferedImage where all shapes // are written. _currentEndX = e.getX(); // save ending coordinates _currentEndY = e.getY(); //--- Draw the current shape onto the buffered image. Graphics2D grafarea = _bufImage.createGraphics(); drawCurrentShape(grafarea); this.repaint(); }//end mouseReleased //========================================== ignored mouse listeners public void mouseMoved (MouseEvent evt) { if(getShape()==4){ trackPoint.x = evt.getX(); trackPoint.y = evt.getY(); repaint(); } } public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} public void mouseClicked (MouseEvent evt) { if(getShape()==4){ int x = evt.getX(); int y = evt.getY(); switch (evt.getClickCount()) { case 1: // single-click if (polygonIsNowComplete) { points.clear(); polygonIsNowComplete = false; } points.add(new Point(x, y)); repaint(); break; case 2: // double-click polygonIsNowComplete = true; points.add(new Point(x, y)); repaint(); break; default: // ignore anything else break; } } } }//endclass PaintPanel
Maneesh Godbole
Bartender
Posts: 11497
19
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
@looka dicosta
Please do
not
edit your posts after people have replied. I am locking this
thread
. If you need some help, please start another topic.
[
How to ask questions
] [
Donate a pint, save a life!
] [
Onff-turn it on!
]
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
problem with JDialog
drawing arrows
How to make rectangle drawn to be vissible when a new rectangle is drawn
Writing Mouse Events
Java swing
More...