首页 > 编程知识 正文

repainted是什么意思,java compareto方法返回值

时间:2023-05-05 19:06:10 阅读:155423 作者:4464

虽然运作超正常,但是你对角色的解释是错误的。 你的问题是试图使用继承来解决继承中未解决的问题。 首先,要避免Key扩展Ball,必须在实际要可视化使用的Ball实例上调用repaint (),而不是在完全不同类的Key实例上调用。

你可能会做这样的事:

f.addkey监听器(new key ) b );

publicclasskeyimplementskeylistener {

私有桶;

公共密钥(ballball ) {

this.ball=ball;

}

公共语音密钥预定义(keyevent e ) {

if (e.get keycode (==keyevent.vk _ w ) {

b.incrx(10 ); //give Ball a public method for this

b.repaint (;

//system.out.println(x: ) super.x );

}

}

//…etc .

请注意。 我自己用Key Bindings。 不是sxdxy。 因为当时不需要键盘的焦点。

例如:

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.RenderingHints;

import java.awt.event.*;

import javax.swing.pgddhxc;

import javax.swing.ActionMap;

import javax.swing.InputMap;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.KeyStroke;

import javax.swing.swing utilities;

公共类移动ball {

privatestaticfinalintpref _ w=500;

privatestaticfinalintpref _ h=pref _ w;

privatestaticvoidcreateandshowgui (

ball panel.addmouselistener (my mouse;

ball panel.addmousemotionlistener (我的mouse;

newcreatekeybindings(ballpanel;

frame.setdefaultcloseoperation (jframe.dispose _ on _ close );

frame.getContentPane ().add ) ballpanel;

frame.pack (;

frame.setlocationbyplatform () true;

frame.setvisible(true;

}

publicstaticvoidmain(string快春args ) )。

SwingUtilities.invokeLater (

createAndShowGui (;

);

}

}

@suppresswarnings('serial”) ) )。

class jpdxg extends JPanel {

privatestaticfinalcolorellipse _ color=color.red;

privatestaticfinalcolorsquare _ color=color.green;

privatestaticfinalintball _ width=40;

私有输入预览;

/p>

private int prefH;

private boolean isEllipse = true;

private int ballX;

private int ballY;

public jpdxg(int prefW, int prefH) {

this.prefW = prefW;

this.prefH = prefH;

}

public boolean isEllipse() {

return isEllipse;

}

public void setEllipse(boolean isEllipse) {

this.isEllipse = isEllipse;

}

public int getBallX() {

return ballX;

}

public void setBallX(int ballX) {

this.ballX = ballX;

}

public void setXY(int x, int y) {

ballX = x;

ballY = y;

repaint();

}

public void setXYCenter(int x, int y) {

ballX = x - BALL_WIDTH / 2;

ballY = y - BALL_WIDTH / 2;

repaint();

}

public void setXYCenter(Point p) {

setXYCenter(p.x, p.y);

}

public int getBallY() {

return ballY;

}

public void setBallY(int ballY) {

this.ballY = ballY;

}

public void incrementBallX(int x) {

ballX += x;

repaint();

}

public void incrementBallY(int y) {

ballY += y;

repaint();

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

if (isEllipse) {

g2.setColor(ELLIPSE_COLOR);

g2.fillOval(ballX, ballY, BALL_WIDTH, BALL_WIDTH);

} else {

g2.setColor(SQUARE_COLOR);

g2.fillOval(ballX, ballY, BALL_WIDTH, BALL_WIDTH);

}

}

@Override

public Dimension getPreferredSize() {

if (isPreferredSizeSet()) {

return super.getPreferredSize();

}

return new Dimension(prefW, prefH);

}

}

class pydmla extends xddmg {

private jpdxg ballPanel;

public pydmla(jpdxg ballPanel) {

this.ballPanel = ballPanel;

}

@Override

public void mousePressed(MouseEvent e) {

ballPanel.setXYCenter(e.getPoint());

}

@Override

public void mouseDragged(MouseEvent e) {

ballPanel.setXYCenter(e.getPoint());

}

@Override

public void mouseReleased(MouseEvent e) {

ballPanel.setXYCenter(e.getPoint());

}

}

enum Direction {

UP(KeyEvent.VK_UP), DOWN(KeyEvent.VK_DOWN), LEFT(KeyEvent.VK_LEFT), RIGHT(KeyEvent.VK_RIGHT);

private int key;

private Direction(int key) {

this.key = key;

}

public int getKey() {

return key;

}

}

// Actions for the key binding

@SuppressWarnings("serial")

class sddmn extends pgddhxc {

private static final int STEP_DISTANCE = 5;

private jpdxg ballPanel;

private Direction direction;

public sddmn(jpdxg ballPanel, Direction direction) {

this.ballPanel = ballPanel;

this.direction = direction;

}

@Override

public void actionPerformed(ActionEvent e) {

switch (direction) {

case UP:

ballPanel.incrementBallY(-STEP_DISTANCE);

break;

case DOWN:

ballPanel.incrementBallY(STEP_DISTANCE);

break;

case LEFT:

ballPanel.incrementBallX(-STEP_DISTANCE);

break;

case RIGHT:

ballPanel.incrementBallX(STEP_DISTANCE);

break;

default:

break;

}

}

}

class CreateKeyBindings {

private jpdxg ballPanel;

public CreateKeyBindings(jpdxg ballPanel) {

this.ballPanel = ballPanel;

int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;

for (Direction direction : Direction.values()) {

KeyStroke keyStroke = KeyStroke.getKeyStroke(direction.getKey(), 0);

String keyString = keyStroke.toString();

inputMap.put(keyStroke, keyString);

actionMap.put(keyString, new sddmn(ballPanel, direction));

}

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。