ورود

View Full Version : گرافیک - کسی میتونه اشکال این برنامه ی ساده ی نقاشی رو حل کنه ؟



jlover
شنبه 01 اسفند 1388, 19:13 عصر
درود
یه برنامه ی خیلی ساده ست ( خیلی از خطوط مربوط به توضیحاته که برای امانتداری پاک نکردم ...)



package painting;

/*
* File: MousePaint.java
* Author: Java, Java, Java
* Description: This class illustrates the use of methods from
* the MouseListener and MouseMotionListener interfaces. The user
* draws within a drawing panel by dragging the mouse. The panel
* is cleared when the user clicks on a small rectangle in the upper-left
* corner of the window.
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Paint extends JPanel implements MouseListener,
MouseMotionListener {

private static final int WIDTH = 300, HEIGHT = 300; // Initial size
private static final int LEFT = 10; // Reference points
private static final int TOP = 10;
private static final int BORDER = 30;

private static final Color lineColor = Color.red; // Outline color

private Point mouse = new Point(); // Mouse's current location

/**
* MousePaint() constructor sets the size of the window and registers
* the mouse listeners. A MouseMotionListener listens for drag and move
* events. A MoustListener listens for click events.
*/
public Paint() {
addMouseMotionListener(this); // Add mouse and mouse motion listeners
addMouseListener(this);
setSize(WIDTH, HEIGHT);
} // MousePaint()

/**
* paintComponent() paints the component each time it is invoked. It must
* distinguish whether the mouse is in the top-left corner of the window,
* in which case it clears the window, or whether the user is drawing.
*/
public void paintComponent(Graphics g) {
Dimension d = getSize();
g.setColor(lineColor);
g.fillRect(0, 0, LEFT, TOP); // The clear rectangle
g.drawRect(LEFT, TOP, d.width - BORDER, d.height - BORDER); // The drawing area
g.drawString("Drag to draw; Click the red rectangle to clear.", LEFT, d.height - 5);
g.setColor(Color.black); // Set drawing color

// If the mouse is within the drawing area, draw a dot
if ((mouse.x > LEFT) && (mouse.x < LEFT + d.width - BORDER)
&& (mouse.y > TOP) && (mouse.y < TOP + d.height - BORDER))
g.fillRect(mouse.x, mouse.y, 3, 3);

// If the mouse is clicked at top left corner clear the drawing
if ((mouse.x < LEFT) && (mouse.y < TOP))
g.clearRect(LEFT + 1, TOP + 1, d.width - BORDER - 1, d.height - BORDER - 1);
} // paintComponent()

/* Mouse Handling Interfaces: MouseMotionListener and MouseListener */

/**
* mouseDragged() handles the mouse drag event. In this case it merely
* gets the mouse's location and repaints the window.
*/
public void mouseDragged(MouseEvent e) { // When the mouse is dragged (mouse motion listener)
mouse = e.getPoint(); // get its coordinates
repaint();
}

/**
* mouseClick() handles mouse click events. In this case it just
* gets the mouse's location and repaints the window.
*/
public void mouseClicked(MouseEvent e) { // When mouse is clicked (mouse listener)
mouse = e.getPoint(); // get its coordinates
repaint();
}
public void mouseEntered(MouseEvent e) { } // These five interface methods are not used
public void mouseExited(MouseEvent e) { } // but must be defined.
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e){ }
public void mouseMoved(MouseEvent e) { }


/**
* main() creates a top-level window and gives it a drawing panel.
*/
public static void main(String args[]) {
JFrame f = new JFrame(); // Create the top-level window
Paint mp = new Paint(); // And give it a drawing panel
f.getContentPane().add(mp);
f.setSize(Paint.WIDTH, Paint.HEIGHT);
f.setVisible(true);
// mp.setOpaque(true);
f.addWindowListener(new WindowAdapter() { // Quit the application
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} // main()
} // MousePaint



و اما یه ایرادی هم داره : وقتی اندازه ی پنجره ی برنامه رو تغییر بدید،هر چی که ترسیم شده بود از بین میره،البته MS paint ویندوز هم چنین مسئله ای داره(اگه ناحیه ی ترسیم رو مثلن کوچیک کنید،تا اونجاییکه کوچیک میشه،ترسیماتش پاک میشه)
خودم خیلی راهها رو امتحان کردم،اما نتونستم ردیفش کنم

ممنون از توجهتون

ermia2008
شنبه 01 اسفند 1388, 22:01 عصر
سلام
خوب وقتی شما فریمتون رو تغییر اندازه میدید باعث میشه که متد paintComponent دوباره فراخوانی بشه که تو اون هم فقط رسم حاشیه های فریم مشخص شده نه اون چیزایی که کاربر روی صفحه رسم میشه! یعنی روز از نو روزی از نو. همه چی از اول رسم میشه!

jlover
شنبه 01 اسفند 1388, 22:50 عصر
سلام
ممنون دوست عزیز
بنده هم اینو میدونم...
اما سوالم چیز دیگه ایه...

ermia2008
یک شنبه 02 اسفند 1388, 00:51 صبح
سلام
اما سوالم چیز دیگه ایه...

میشه دقیقا بگین مشکل چیه؟(غیر از Resize)
من این سوال مربوط به "چیز دیگه ایه..." رو متوجه نشدم :چشمک:

jlover
سه شنبه 11 اسفند 1388, 08:39 صبح
خب اینم که به سلامتی مشکلش حل شد :لبخندساده:

جهت اطلاع _ توضیح کلی روش حل مسایلی از این دست :

باید در هر فراخانی paintComponent ( با repaint ) ابتدا کلیه ی اشکال قبلی ( که در اینجا به سادگی فقط یکسری نقطه هستند ) که به هنگام پردازش رویدادهای موشواره* در یک Collection ( من در اینجا از یک ArrayList استفاده کردم ) ذخیره شده اند رو با استفاده از یک حلقه ترسیم کنیم
با تشکر از مولف بلاگ زیر
http://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/
پیروز باشید :قلب:

----------------------------
* : اینجا اساساً فارسی رو پاس داشتم