问题描述
有可与替换任何函数的paint()
和重绘()
在java中。
我有一个场景。
有一个三角形的(三角形1)。当用户将在三角点击另一个三角形的(三角2)会出现和第一(三角形1)会从屏幕上消失。 (使用codeD 的JFrame
,的paint()
和重绘()
)
到目前为止我做到了这一点。但问题是,当我最小化或鼠标窗口的大小改变输出窗口,它只是再次油漆的三角1 而非三角2 。 或清除整个屏幕,如果我称之为 g2d.clearRect(0,0,1000,1000);结果
triangle.reset();
注意:这两个功能都去掉previous三角形(三角形1)。
是否有国家不应该最小化或当窗口大小改变?改变任何功能
或者我们可以覆盖重绘()
或根据任何情况下帮助。
下面是工作code。执行它,单击三角形然后最小化并再次看到。你会得到这个问题更清楚一些。
进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类Triangle_shape扩展JFrame中实现的ActionListener { 公共静态的JButton btnSubmit按钮=的新的JButton(提交); 公共Triangle_shape(){
} 公共静态无效的主要(字串[] args){
JFrame的帧=新的JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(新的BorderLayout());
frame.add(新TrianglePanel(),BorderLayout.CENTER);
frame.add(btnSubmit按钮,BorderLayout.PAGE_END);
frame.pack();
frame.repaint();
frame.setTitle(测试帧);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(真);
} @覆盖
公共无效的actionPerformed(ActionEvent的五){
抛出新UnsupportedOperationException异常(不支持呢。);
} 公共静态类TrianglePanel继承JPanel实现的MouseListener { 私人多边形三角形,triangle2; 公共TrianglePanel(){
//创建三角
三角=新的多边形();
triangle.addPoint(400,550); //剩下
triangle.addPoint(600,550); //对
triangle.addPoint(500,350); //最佳 //添加鼠标监听器
addMouseListener将(本); //设置大小,以确保整个三角形示出
集preferredSize(新尺寸(300,300));
} / **
*绘制三角形此帧的画
* /
@覆盖
公共无效的paintComponent(图形G){
Graphics2D的G2D =(Graphics2D的)克;
g2d.draw(三角形);
} //所需的方法的MouseListener,虽然你关心的只有一个是点击
公共无效鼠标pressed(的MouseEvent E){
} 公共无效的mouseReleased(的MouseEvent E){
} 公共无效的mouseEntered(的MouseEvent E){
} 公共无效的mouseExited(的MouseEvent E){
} / **
*调用只要鼠标点击。可以与设置被替换
*一个JLabel的值等
* /
公共无效的mouseClicked(的MouseEvent E){
Graphics2D的G2D =(Graphics2D的)this.getGraphics();
点P = e.getPoint();
如果(triangle.contains(P)){
的System.out.println(1); g2d.clearRect(0,0,1000,1000);
triangle.reset();
g2d.setColor(Color.MAGENTA);
triangle2 =新的多边形();
triangle2.addPoint(600,550); // 剩下
triangle2.addPoint(700,350); //最佳
triangle2.addPoint(800,550); //对
g2d.draw(triangle2);
}其他{
的System.out.println(三角不要有一点);
}
}
}
}
您的mouseClicked
方法不应该创建一个新的三角的对象。复位后三角
只需添加新的指向它。也不要在这个方法中借鉴,但叫重绘
:
公共无效的mouseClicked(的MouseEvent E){
点P = e.getPoint();
如果(triangle.contains(P)){
的System.out.println(1);
triangle.reset(); //改变当前三角形
triangle.addPoint(600,550); //新左派
triangle.addPoint(700,350); //新的顶级
triangle.addPoint(800,550); //新的权利
重绘(); //重绘力
}其他{
的System.out.println(三角不要有一点);
}
}
现在,如果你想多个三角形,你应该有多边形
s的集合。像这样的:
公共静态类TrianglePanel继承JPanel实现的MouseListener {
私人矢量<&多边形GT;三角形; 公共TrianglePanel(){
N = 0;
//创建一个空的集合
三角形=新的矢量<&多边形GT;(); //创建一个三角形
多边形三角=新的Polygon();
triangle.addPoint(400,550); //剩下
triangle.addPoint(600,550); //对
triangle.addPoint(500,350); //最佳 //三角形添加到收藏
triangles.add(三角形); //添加鼠标监听器
addMouseListener将(本); //设置大小,以确保整个三角形示出
集preferredSize(新尺寸(300,300));
} / **
*绘制三角形作为此帧的画
* /
@覆盖
公共无效的paintComponent(图形G){
Graphics2D的G2D =(Graphics2D的)克;
对于(多边形P:三角形)//绘制所有三角形集合中
g2d.draw(P);
} //所需的方法的MouseListener,虽然你关心的只有一个是点击
公共无效鼠标pressed(的MouseEvent E){}
公共无效的mouseReleased(的MouseEvent E){}
公共无效的mouseEntered(的MouseEvent E){}
公共无效的mouseExited(的MouseEvent E){} / **
*调用只要鼠标点击。可以与设置被替换
*一个JLabel的值等
* /
公共无效的mouseClicked(的MouseEvent E){
Graphics2D的G2D =(Graphics2D的)this.getGraphics();
点P = e.getPoint();
//你想用p和收集什么
//例如:删除包含P点的所有三角形
&的ListIterator LT;多边形>李= triangles.listIterator();
而(li.hasNext()){
如果(li.next()contains℗)li.remove();
}
//是否要更新列表什么
//例如:添加一个新的三角形...
多边形三角=新的Polygon();
triangle.addPoint(600 + N,550); // 剩下
triangle.addPoint(700 + N,350); //最佳
triangle.addPoint(800 + N,550); //对
triangles.add(三角形); //新三角形添加到列表
N + = 10; //下一个新的三角形将被搬权
重绘();
}
私人诠释N;
}
Is there any function which can be replaced with paint()
and repaint()
in java.
I have a scenario.
There is a triangle (Triangle 1). when user will click in triangle another triangle (Triangle 2) will appear and 1st (Triangle 1) will be removed from screen. (coded using JFrame
, paint()
and repaint()
)
I have achieved it so far. but problem is when I minimize or change size of window with mouse the output window, it just paint again Triangle 1 rather than Triangle 2. OR Clear the whole screen if i call g2d.clearRect(0, 0, 1000, 1000);
triangle.reset();
Note: These both functions are to remove previous triangle (Triangle 1).
Is there any function that state should not be changed on minimize or when window size changed ?
or can we override repaint()
or anything helping according to scenario.
Here is the Working code. Execute it, Click in triangle then minimize and see again. You will get the problem more clearly.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Triangle_shape extends JFrame implements ActionListener {
public static JButton btnSubmit = new JButton("Submit");
public Triangle_shape() {
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new BorderLayout());
frame.add(new TrianglePanel(), BorderLayout.CENTER);
frame.add(btnSubmit, BorderLayout.PAGE_END);
frame.pack();
frame.repaint();
frame.setTitle("A Test Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public static class TrianglePanel extends JPanel implements MouseListener {
private Polygon triangle, triangle2;
public TrianglePanel() {
//Create triangle
triangle = new Polygon();
triangle.addPoint(400, 550); //left
triangle.addPoint(600, 550); //right
triangle.addPoint(500, 350); //top
//Add mouse Listener
addMouseListener(this);
//Set size to make sure that the whole triangle is shown
setPreferredSize(new Dimension(300, 300));
}
/**
* Draws the triangle as this frame's painting
*/
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.draw(triangle);
}
//Required methods for MouseListener, though the only one you care about is click
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
/**
* Called whenever the mouse clicks. Could be replaced with setting the
* value of a JLabel, etc.
*/
public void mouseClicked(MouseEvent e) {
Graphics2D g2d = (Graphics2D) this.getGraphics();
Point p = e.getPoint();
if (triangle.contains(p)) {
System.out.println("1");
g2d.clearRect(0, 0, 1000, 1000);
triangle.reset();
g2d.setColor(Color.MAGENTA);
triangle2 = new Polygon();
triangle2.addPoint(600, 550); // left
triangle2.addPoint(700, 350); //top
triangle2.addPoint(800, 550); //right
g2d.draw(triangle2);
} else {
System.out.println("Triangle dont have point");
}
}
}
}
Your mouseClicked
method should not create a new Triangle object. After resetting triangle
just add new points to it. Also don't draw in this method but call repaint
:
public void mouseClicked(MouseEvent e) {
Point p = e.getPoint();
if (triangle.contains(p)) {
System.out.println("1");
triangle.reset(); // change the current triangle
triangle.addPoint(600, 550); // new left
triangle.addPoint(700, 350); // new top
triangle.addPoint(800, 550); // new right
repaint(); // force repainting
} else {
System.out.println("Triangle dont have point");
}
}
Now if you want many triangles you should have a collection of Polygon
s. Like this :
public static class TrianglePanel extends JPanel implements MouseListener {
private Vector<Polygon> triangles;
public TrianglePanel() {
n = 0;
// Create an empty collection
triangles = new Vector<Polygon>();
//Create first triangle
Polygon triangle = new Polygon();
triangle.addPoint(400, 550); //left
triangle.addPoint(600, 550); //right
triangle.addPoint(500, 350); //top
// Add the triangle to the collection
triangles.add(triangle);
//Add mouse Listener
addMouseListener(this);
//Set size to make sure that the whole triangle is shown
setPreferredSize(new Dimension(300, 300));
}
/**
* Draws the triangles as this frame's painting
*/
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
for (Polygon p : triangles) // Draw all triangles in the collection
g2d.draw(p);
}
//Required methods for MouseListener, though the only one you care about is click
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
/**
* Called whenever the mouse clicks. Could be replaced with setting the
* value of a JLabel, etc.
*/
public void mouseClicked(MouseEvent e) {
Graphics2D g2d = (Graphics2D) this.getGraphics();
Point p = e.getPoint();
// Do what you want with p and the collection
// For example : remove all triangles that contain the point p
ListIterator<Polygon> li = triangles.listIterator();
while (li.hasNext()) {
if (li.next().contains℗) li.remove();
}
// Do what you want to update the list
// For example: Add a new triangle...
Polygon triangle = new Polygon();
triangle.addPoint(600+n, 550); // left
triangle.addPoint(700+n, 350); //top
triangle.addPoint(800+n, 550); //right
triangles.add(triangle); // add the new triangle to the list
n += 10; // next new triangle will be "moved" right
repaint();
}
private int n;
}
这篇关于什么是涂料的替代性和重绘功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!