如何不重新绘制JFrame?只是画画
我需要简单程序的帮助。我想,当我单击程序时标记它。我想看看我过去的举动。就像铅笔在油漆中一样。
类 Test.java
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.IOException;
import java.lang.*;
import javax.swing.*;
public class Test{
JFrame frame;
public static void main(String[] args){
Test smallTest = new Test();
smallTest.letsDoIt();
}
public void letsDoIt(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonOfTheEnd = new JButton("EXIT");
buttonOfTheEnd.addActionListener(new theEndListener());
graphPanel panelR = new graphPanel();
panelR.setBackground(Color.WHITE);
frame.getContentPane().add(BorderLayout.CENTER, panelR);
frame.getContentPane().add(BorderLayout.SOUTH, buttonOfTheEnd);
frame.setSize(500,500);
frame.setVisible(true);
frame.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
frameMouseClicked(evt);
}
});
}
void frameMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
frame.repaint();
}
class theEndListener implements ActionListener {
public void actionPerformed(ActionEvent zdarzenie){
System.exit(0);
}
}
// public void paintComponent(Graphics g){
// super.paintComponent(g);
// int wspX = MouseInfo.getPointerInfo().getLocation().x;
// int wspY = MouseInfo.getPointerInfo().getLocation().y;
// g.setColor(Color.RED);
// g.fillRect( wspX, wspY, 10, 10);
//
}
类 graphPanel
import java.awt.*;
import javax.swing.*;
class graphPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int locX = MouseInfo.getPointerInfo().getLocation().x - 10;
int locY = MouseInfo.getPointerInfo().getLocation().y - 30;
g.setColor(Color.ORANGE);
g.fillRect(locX, locY, 10, 10);
}
}
感谢您的帮助。
I need help with simple program. I would like, when I click program marks it. I would like to see my moves did in past. Like pencil in paint.
class Test.java
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.IOException;
import java.lang.*;
import javax.swing.*;
public class Test{
JFrame frame;
public static void main(String[] args){
Test smallTest = new Test();
smallTest.letsDoIt();
}
public void letsDoIt(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonOfTheEnd = new JButton("EXIT");
buttonOfTheEnd.addActionListener(new theEndListener());
graphPanel panelR = new graphPanel();
panelR.setBackground(Color.WHITE);
frame.getContentPane().add(BorderLayout.CENTER, panelR);
frame.getContentPane().add(BorderLayout.SOUTH, buttonOfTheEnd);
frame.setSize(500,500);
frame.setVisible(true);
frame.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
frameMouseClicked(evt);
}
});
}
void frameMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
frame.repaint();
}
class theEndListener implements ActionListener {
public void actionPerformed(ActionEvent zdarzenie){
System.exit(0);
}
}
// public void paintComponent(Graphics g){
// super.paintComponent(g);
// int wspX = MouseInfo.getPointerInfo().getLocation().x;
// int wspY = MouseInfo.getPointerInfo().getLocation().y;
// g.setColor(Color.RED);
// g.fillRect( wspX, wspY, 10, 10);
//
}
class graphPanel
import java.awt.*;
import javax.swing.*;
class graphPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
int locX = MouseInfo.getPointerInfo().getLocation().x - 10;
int locY = MouseInfo.getPointerInfo().getLocation().y - 30;
g.setColor(Color.ORANGE);
g.fillRect(locX, locY, 10, 10);
}
}
thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将最近的位置存储在某处。
List
是一个不错的选择。现在,当您单击屏幕上的某个位置时,您可以将该位置添加到列表中,当您需要重新绘制屏幕时,您可以绘制所有存储的位置。这类似于分离模型和视图(模型是您的二维位置列表,视图是您在屏幕上绘制的内容)。因此,在某处创建一个列表:
然后(谢谢 oliholz!)将侦听器添加到
panelR
而不是frame
:并像这样编写frameMouseClicked:
在
paintComponent
内您迭代列表并绘制所有存储的位置(再次强调:非常简单且减少,可改进!)You have to store the recent locations somewhere. A
List
is a good choice. Now, when you click somewhere on the screen, you add the location to the list and when you need to repaint the screen, you paint all stored location. That's something like separating model and view (the model is your list of 2D locations, the view is what you paint on the screen).So create one list somewhere:
Then (thanks, oliholz!) add the listener to
panelR
instead offrame
:and write frameMouseClicked like:
Inside
paintComponent
you iterate through the list and draw all stored locations (again: very simple and reduced, improveable!)尝试在
graphPanel
中绘制图像,然后将图像绘制到组件。如果您不清除图像,请附加每张图画。
Try to paint on an image in your
graphPanel
an paint the image to the component.If you do not clear the image you attach every drawing.