Java小程序重新绘制的问题
下面的程序应该将Java小程序重新绘制的问题,java,swing,applet,repaint,Java,Swing,Applet,Repaint,下面的程序应该将X字符串放置在java小程序的tic-tac趾板中,以便在连续放置后它们将保持不变。但是,当用户单击放置X时,X将消失并在其他位置绘制。我如何克服这个问题 import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * Class TicTacToe - write a description of the class here * * @author (your name) * @ve
X
字符串放置在java小程序的tic-tac趾板中,以便在连续放置后它们将保持不变。但是,当用户单击放置X
时,X
将消失并在其他位置绘制。我如何克服这个问题
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Class TicTacToe - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class TicTacToe extends JApplet implements MouseListener
{
// instance variables - replace the example below with your own
private boolean[][] board = new boolean[3][3];
int j; int h;
/**
* Called by the browser or applet viewer to inform this JApplet that it
* has been loaded into the system. It is always called before the first
* time that the start method is called.
*/
public void init()
{
// this is a workaround for a security conflict with some browsers
// including some versions of Netscape & Internet Explorer which do
// not allow access to the AWT system event queue which JApplets do
// on startup to check access. May not be necessary with your browser.
JRootPane rootPane = this.getRootPane();
rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
addMouseListener(this);
// provide any initialisation necessary for your JApplet
}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseClicked(MouseEvent e){
int x = e.getX(); int y = e.getY();
j = x; h = y;
x -= 50;
y-= 50;
x/=50;
y/=50;
board[x][y] = true;
repaint();
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* should start its execution. It is called after the init method and
* each time the JApplet is revisited in a Web page.
*/
public void start()
{
// provide any code requred to run each time
// web page is visited
}
/**
* Called by the browser or applet viewer to inform this JApplet that
* it should stop its execution. It is called when the Web page that
* contains this JApplet has been replaced by another page, and also
* just before the JApplet is to be destroyed.
*/
public void stop()
{
// provide any code that needs to be run when page
// is replaced by another page or before JApplet is destroyed
}
/**
* Paint method for applet.
*
* @param g the Graphics object for this applet
*/
public void paint(Graphics g)
{
// background color
g.setColor(Color.white);
g.fillRect(0, 0, 150, 150);
//draw board
g.setColor(Color.BLACK);
//draw vertical line
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(i,0,i,150);
}
//draw horizontal lines
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(0,i,150,i);
}
//paint x values when applicable
g.setColor(Color.RED);
for(int i = 0; i < 3; i ++){
for(int r = 0; r < 3; r ++){
if(board[i][r] == true) g.drawString("X",j ,h );
}
}
}
/**
* Called by the browser or applet viewer to inform this JApplet that it
* is being reclaimed and that it should destroy any resources that it
* has allocated. The stop method will always be called before destroy.
*/
public void destroy()
{
// provide code to be run when JApplet is about to be destroyed.
}
/**
* Returns information about this applet.
* An applet should override this method to return a String containing
* information about the author, version, and copyright of the JApplet.
*
* @return a String representation of information about this JApplet
*/
public String getAppletInfo()
{
// provide information about the applet
return "Title: \nAuthor: \nA simple applet example description. ";
}
/**
* Returns parameter information about this JApplet.
* Returns information about the parameters than are understood by this JApplet.
* An applet should override this method to return an array of Strings
* describing these parameters.
* Each element of the array should be a set of three Strings containing
* the name, the type, and a description.
*
* @return a String[] representation of parameter information about this JApplet
*/
public String[][] getParameterInfo()
{
// provide parameter information about the applet
String paramInfo[][] = {
{"firstParameter", "1-10", "description of first parameter"},
{"status", "boolean", "description of second parameter"},
{"images", "url", "description of third parameter"}
};
return paramInfo;
}
}
导入java.awt.event.*;
导入java.awt.*;
导入javax.swing.*;
/**
*类TictaToe-在此处编写类的描述
*
*@author(你的名字)
*@version(版本号)
*/
公共类TictaToe扩展JApplet实现MouseStener
{
//实例变量-将下面的示例替换为您自己的
专用布尔值[][]板=新布尔值[3][3];
int j;int h;
/**
*由浏览器或小程序查看器调用以通知此JApplet
*已加载到系统中。它总是在第一个
*调用start方法的时间。
*/
公共void init()
{
//这是与某些浏览器发生安全冲突的一种解决方法
//包括Netscape和Internet Explorer的一些版本
//不允许访问AWT系统事件队列
//启动时检查访问权限。您的浏览器可能不需要。
JRootPane rootPane=this.getRootPane();
putClientProperty(“DefaultSystemEventQueueCheck”,Boolean.TRUE);
addMouseListener(这个);
//为您的JApplet提供任何必要的初始化
}
公共无效mouseExited(MouseEvent e){}
公共无效mouseenterned(MouseEvent e){}
公共无效MouseEvent e{}
public void mousePressed(MouseEvent e){}
公共无效mouseClicked(MouseEvent e){
int x=e.getX();int y=e.getY();
j=x;h=y;
x-=50;
y-=50;
x/=50;
y/=50;
董事会[x][y]=正确;
重新油漆();
}
/**
*由浏览器或小程序查看器调用以通知此JApplet
*应该开始执行。在init方法和
*每次在网页中重新访问JApplet时。
*/
公开作废开始()
{
//提供每次运行所需的任何代码
//网页被访问
}
/**
*由浏览器或小程序查看器调用以通知此JApplet
*它应该停止执行。当该网页
*包含此JApplet的已被另一页替换,并且
*就在日本战舰被摧毁之前。
*/
公共停车场()
{
//提供在创建页面时需要运行的任何代码
//被另一页替换或在JApplet被销毁之前
}
/**
*小程序的绘制方法。
*
*@param g此小程序的图形对象
*/
公共空间涂料(图g)
{
//背景色
g、 setColor(Color.white);
g、 fillRect(0,0,150,150);
//绘图板
g、 设置颜色(颜色为黑色);
//画垂直线
for(int i=50;ij
和h
包含上次鼠标单击的位置。您在此处所做的操作是错误的
if(board[i][r] == true) g.drawString("X",j ,h );
因为您正试图在同一位置绘制所有的X
s
因此,为了解决您的问题,我们需要存储已绘制的X
s的位置。我们需要创建一个position
s表
添加这个类
class Position{
private int x;
private int y;
public Position(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
然后在包含所有位置的TicTacToe
类中添加新属性
private Position[][] positions = new Position[3][3];
最后,这里是mouseClicked
方法
public void mouseClicked(MouseEvent e){
int x = e.getX(); int y = e.getY();
j=x/50;
h=y/50;
board[j][h] = true;
if(positions[j][h]==null){
positions[j][h]=new Position(x,y);
}
positions[j][h].setX(x);
positions[j][h].setY(y);
repaint();
}
以及绘制
方法
public void paint(Graphics g)
{
// background color
g.setColor(Color.white);
g.fillRect(0, 0, 150, 150);
//draw board
g.setColor(Color.BLACK);
//draw vertical line
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(i,0,i,150);
}
//draw horizontal lines
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(0,i,150,i);
}
//paint x values when applicable
g.setColor(Color.RED);
for(int i = 0; i < 3; i ++){
for(int r = 0; r < 3; r ++){
if(board[i][r] == true) g.drawString
("X",positions[i][r].getX() ,positions[i][r].getY() );
}
}
}
你想保持X
s已经画好了,还是相反?为什么2015年人们还在摆弄小程序?
package example;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
class Position{
private int x;
private int y;
public Position(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
public class NewTicTacToe {
public static void main(String[] args) {
new NewTicTacToe();
}
public NewTicTacToe() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("NewTicTacToe");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintTicTacToe());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintTicTacToe extends JPanel {
// instance variables - replace the example below with your own
private boolean[][] board = new boolean[3][3];
private Position[][] positions = new Position[3][3];
int j; int h;
public PaintTicTacToe() {
super();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX(); int y = e.getY();
j=x/50;
h=y/50;
board[j][h] = true;
if(positions[j][h]==null){
positions[j][h]=new Position(x,y);
}
positions[j][h].setX(x);
positions[j][h].setY(y);
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
// background color
g.setColor(Color.white);
g.fillRect(0, 0, 150, 150);
//draw board
g.setColor(Color.BLACK);
//draw vertical line
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(i,0,i,150);
}
//draw horizontal lines
for(int i = 50; i <= 100; i = i + 50){
g.drawLine(0,i,150,i);
}
//paint x values when applicable
g.setColor(Color.RED);
for(int i = 0; i < 3; i ++){
for(int r = 0; r < 3; r ++){
if(board[i][r] == true) g.drawString
("X",positions[i][r].getX() ,positions[i][r].getY() );
}
}
}
}
}