Java 椭圆与图形串的配色及碰撞试验

Java 椭圆与图形串的配色及碰撞试验,java,swing,animation,Java,Swing,Animation,我正在为我的工程项目做一个游戏,我面临一些问题。我认为我的代码中有一部分应该可以工作,但它不起作用。你能告诉我我做错了什么吗?下面是我的两个类的代码,分别是AlphabetsColor和TestalHabets。有一小部分代码用于检测椭圆和字母的碰撞,但它不起作用。请帮帮我 import java.awt.*; import java.util.*; public class AlphabetsColoured { private Color quescolour, an

我正在为我的工程项目做一个游戏,我面临一些问题。我认为我的代码中有一部分应该可以工作,但它不起作用。你能告诉我我做错了什么吗?下面是我的两个类的代码,分别是AlphabetsColor和TestalHabets。有一小部分代码用于检测椭圆和字母的碰撞,但它不起作用。请帮帮我

import java.awt.*;     
import java.util.*;

public class AlphabetsColoured 
{

    private Color quescolour, anscolour;
    private int x, y, width, height; 


    public void getQuestion(Graphics g,String alphabet,int x, int y, Color colour)
    {
        Graphics2D g2 = (Graphics2D) g;

        Font font = new Font("Chalkduster", Font.BOLD+Font.PLAIN, 70);
        g2.setFont(font);
        g2.setColor(this.quescolour = colour);
        g2.drawString(alphabet, x, y);
    }

    public void getAnswers(Graphics g, String alphabet,int x, int y, Color colour)
    {
        Graphics2D g2 = (Graphics2D) g;

        Font font1 = new Font("Bloody", Font.BOLD+Font.PLAIN, 30);
        g2.setFont(font1);
        g2.setColor(this.anscolour = colour);
        g2.drawString(alphabet, x, y);

    }

    public Color getQuestionColour()
    {
        if (this.quescolour.equals(Color.BLACK))
        {
           return Color.BLACK;
        }
        if (this.quescolour.equals(Color.PINK))
        {
           return Color.PINK;
        }
        if (this.quescolour.equals(Color.BLUE))
        {
           return Color.BLUE;
        }
        if (this.quescolour.equals(Color.GREEN))
        {
           return Color.GREEN;
        }
        if (this.quescolour.equals(Color.CYAN))
        {
           return Color.CYAN;
        }
        if (this.quescolour.equals(Color.WHITE))
        {
           return Color.WHITE;
        }
        if (this.quescolour.equals(Color.YELLOW))
        {
           return Color.YELLOW;
        }
        if (this.quescolour.equals(Color.RED))
        {
           return Color.RED;
        }
        if (this.quescolour.equals(Color.ORANGE))
        {
           return Color.ORANGE;
        }
        if (this.quescolour.equals(Color.GRAY))
        {
           return Color.GRAY;
        }
        return Color.MAGENTA;
    }

    public Color getAnswerColour()
    {
        if (this.anscolour.equals(Color.BLACK))
        {
           return Color.BLACK;
        }
        if (this.anscolour.equals(Color.PINK))
        {
           return Color.PINK;
        }
        if (this.anscolour.equals(Color.BLUE))
        {
           return Color.BLUE;
        }
        if (this.anscolour.equals(Color.GREEN))
        {
           return Color.GREEN;
        }
        if (this.anscolour.equals(Color.CYAN))
        {
           return Color.CYAN;
        }
        if (this.anscolour.equals(Color.WHITE))
        {
           return Color.WHITE;
        }
        if (this.anscolour.equals(Color.YELLOW))
        {
           return Color.YELLOW;
        }
        if (this.anscolour.equals(Color.RED))
        {
           return Color.RED;
        }
        if (this.anscolour.equals(Color.ORANGE))
        {
           return Color.ORANGE;
        }
        if (this.anscolour.equals(Color.GRAY))
        {
           return Color.GRAY;
        }
        return Color.MAGENTA;
    }

    public boolean ColourComparisonTest(AlphabetsColoured alpha1, AlphabetsColoured alpha2)
    {
       if (alpha1.getQuestionColour().equals(alpha2.getAnswerColour()) == true)
       {
           return true;
       }
       return false;
    }

    public Rectangle getAlphabetBounds()
    {
        return new Rectangle(x, y, width, height);
    }

}

导入java.awt.Color;
导入java.awt.Font;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.Timer;
导入java.awt.geom.*;
导入java.util.*;
公共类TestalHabets扩展JPanel实现ActionListener、KeyListener
{
字母表颜色=新字母表颜色();
字母表着色ans1=新字母表着色();
字母表着色ans2=新字母表着色();
字母表着色ans3=新字母表着色();
字母表着色ans4=新字母表着色();
字母表彩色比较=新字母表彩色();
private int y=0,vely=1;//字母速度
私有int x1=650,y1=650,velx1=5,vely1=5;//球速度
定时器=新定时器(10,此);
公共测试habets()
{
timer.start();
addKeyListener(此);
此.setFocusTraversalKeysEnabled(false);
此参数为.setFocusable(true);
}
公共组件(图形g)
{
问题(g,“红色”,20,y,颜色。青色);
ans1.getAnswers(g,“橙色”,400,y+20,颜色为橙色);
ans2.getAnswers(g,“黄色”,600,y-20,颜色。粉红色);
ans3.getAnswers(g,“绿色”,800,y+50,彩色,蓝色);
ans4.getAnswers(g,“蓝色”,1000,y-50,颜色为青色);
g、 椭圆形(x1,y1,50,50);
if(comparison.colorComparisonTest(ques,ans1)=true和碰撞(g)=true和(y1==y+20))
{
g、 setColor(颜色为橙色);
g、 抽绳(“你打橙色”,500,500);
尝试{Thread.sleep(1000);}catch(InterruptedException ex){System.err.println(ex.getMessage());}
}
if(comparison.colorComparisonTest(ques,ans2)=true和碰撞(g)=true和(y1==y-20))
{
g、 setColor(颜色为黄色);
g、 抽绳(“你打到黄色”,500,500);
尝试{Thread.sleep(1000);}catch(InterruptedException ex){System.err.println(ex.getMessage());}
}
if(comparison.colorComparisonTest(ques,ans3)=true&&Collides(g)==true&&y1==y+50))
{
g、 setColor(Color.GREEN);
g、 抽绳(“你打到绿色”,500,500);
尝试{Thread.sleep(1000);}catch(InterruptedException ex){System.err.println(ex.getMessage());}
}
if(comparison.colorComparisonTest(ques,ans4)=true&&Collides(g)==true&&y1==y-50))
{
g、 setColor(Color.BLUE);
g、 抽绳(“你打到蓝色”,500,500);
尝试{Thread.sleep(1000);}catch(InterruptedException ex){System.err.println(ex.getMessage());}
}///这部分代码用于4种颜色的椭圆形碰撞检测,但它不起作用。请帮助我。我不确定我的碰撞和颜色比较测试方法是否正确。谢谢
}
已执行的公共无效操作(操作事件e)
{
if(y<0)
{
vc++;
}
如果(y>800)
{
弗利——;
}
if(x1<0 | | y1<0)
{
velx1++;
vely1++;
}
如果(x1>1450 | y1>750)
{
velx1--;
天鹅绒--;
}
重新油漆();
y+=0;
x1+=velx1;
y1+=vely1;
}
公开作废{
velx1=0;
vely1=-4;
}
公众假期{
velx1=0;
vely1=4;
}
公共空间(左){
velx1=-4;
vely1=0;
}
公共无效权(){
velx1=4;
vely1=0;
}
按下公共无效键(按键事件e)
{
int keycode=e.getKeyCode();
if(keycode==KeyEvent.VK_UP)
{
up();
}
if(keycode==KeyEvent.VK_DOWN)
{
向下();
}
if(keycode==KeyEvent.VK_左)
{
左();
}
if(keycode==KeyEvent.VK_RIGHT)
{
对();
}   
}
public void keyTyped(KeyEvent e)
{   
}
公共无效密钥已释放(密钥事件e)
{
velx1=0;
vely1=0;
}
公共布尔碰撞(图形g)
{
if(ans1.getAlphabetBounds()相交(g.getClipBounds())
{
返回true;
}
if(ans2.getAlphabetBounds()相交(g.getClipBounds())
{
返回true;
}
if(ans3.getAlphabetBounds()相交(g.getClipBounds())
{
返回true;
}
if(ans4.getAlphabetBounds()相交(g.getClipBounds())
{
返回true;
}
返回false;
}
公共静态void main(字符串参数[])
{
JFrame f=新的JFrame();
f、 设置大小(1500850);
f、 setVisible(真);
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
新约圣经词条();
TestalHabets ggg=新约TestalHabets();
f、 添加(ggg);
}
}

请告诉我更多信息--它应该如何工作?--有一个问题很明显:您的paintComponent方法缺少super.paintComponent(g)调用它的第一行。但除此之外,我仍在等待您的澄清和问题改进。其他问题:您的绘画方法中有
Thread.sleep(…)
,这是不应该做的。让计时器处理所有睡眠行为。您的绘画方法中也有程序逻辑,这也是应该避免的。画家
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.geom.*;
import java.util.*;

public class testAlphabets extends JPanel implements ActionListener, KeyListener
{

    AlphabetsColoured ques = new AlphabetsColoured();
    AlphabetsColoured ans1 = new AlphabetsColoured();
    AlphabetsColoured ans2 = new AlphabetsColoured();
    AlphabetsColoured ans3 = new AlphabetsColoured();
    AlphabetsColoured ans4 = new AlphabetsColoured();
    AlphabetsColoured comparison = new AlphabetsColoured();

    private int y=0, vely=1;           //alphabets velocity
    private int x1=650, y1=650, velx1=5, vely1=5;   //ball velocites

    Timer timer = new Timer(10, this);

    public testAlphabets()
    {
        timer.start();
        addKeyListener(this); 
        this.setFocusTraversalKeysEnabled(false);
        this.setFocusable(true);

    }

    public void paintComponent(Graphics g)
    {
        ques.getQuestion(g, "Red", 20, y, Color.cyan);
        ans1.getAnswers(g, "Orange", 400, y+20, Color.ORANGE);
        ans2.getAnswers(g, "Yellow", 600, y-20, Color.PINK);
        ans3.getAnswers(g, "Green", 800, y+50, Color.BLUE);
        ans4.getAnswers(g, "Blue", 1000, y-50, Color.cyan);

        g.fillOval(x1, y1, 50, 50);

        if(comparison.ColourComparisonTest(ques, ans1) == true && Collides(g) == true && (y1==y+20)) 
        {
            g.setColor(Color.ORANGE);
            g.drawString("You hit orange" , 500, 500);
            try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
        }
        if(comparison.ColourComparisonTest(ques, ans2) == true && Collides(g) == true && (y1==y-20))
        {
            g.setColor(Color.YELLOW);
            g.drawString("You hit yellow" , 500, 500);
            try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
        }
        if(comparison.ColourComparisonTest(ques, ans3) == true && Collides(g) == true && (y1==y+50))
        {
            g.setColor(Color.GREEN);
            g.drawString("You hit green" , 500, 500);
            try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
        }
        if(comparison.ColourComparisonTest(ques, ans4) == true && Collides(g) == true && (y1==y-50))
        {
            g.setColor(Color.BLUE);
            g.drawString("You hit blue" , 500, 500);
            try{Thread.sleep(1000);} catch(InterruptedException ex) {System.err.println(ex.getMessage());}
        }  ///this part of the code is for collision detection of the oval with the 4 colours buts its not working. please help me. im not sure if my Collides and ColourComparisonTest methods are correct. thanks 

    }

    public void actionPerformed(ActionEvent e)
    {
        if(y < 0)
        {
           vely++;
        }
        if(y > 800)
        {
            vely--;
        }
        if(x1 < 0 || y1 < 0)
        {
           velx1++;
           vely1++;
        }
        if(x1 > 1450 || y1 > 750)
        {
            velx1--;
            vely1--;
        }

        repaint();

        y += vely;

        x1 += velx1;
        y1 += vely1;

    }

    public void up() {
        velx1 = 0;
        vely1 = -4;
    }

    public void down() {
        velx1 = 0;
        vely1 = 4;

    }

    public void left() {
        velx1 = -4;
        vely1 = 0;
    }

    public void right() {
        velx1 = 4;      
        vely1 = 0;
    }

    public void keyPressed(KeyEvent e)
    {
        int keycode = e.getKeyCode();

        if(keycode == KeyEvent.VK_UP)
        {
            up();
        }

        if(keycode == KeyEvent.VK_DOWN)
        {
            down();
        }

        if(keycode == KeyEvent.VK_LEFT)
        {
            left();
        }

        if(keycode == KeyEvent.VK_RIGHT)
        {
            right();
        }   

    }

    public void keyTyped(KeyEvent e)
    {   
    }
    public void keyReleased(KeyEvent e)
    {
        velx1 = 0;
        vely1 = 0;
    }

    public boolean Collides(Graphics g)
    {
        if(ans1.getAlphabetBounds().intersects(g.getClipBounds()))
        {
            return true;
        }
        if(ans2.getAlphabetBounds().intersects(g.getClipBounds()))
        {
            return true;
        }
        if(ans3.getAlphabetBounds().intersects(g.getClipBounds()))
        {
            return true;
        }
        if(ans4.getAlphabetBounds().intersects(g.getClipBounds()))
        {
            return true;
        }
        return false;
    }



    public static void main(String args [])
    {
        JFrame f = new JFrame();
        f.setSize(1500, 850);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        new testAlphabets();
        testAlphabets ggg = new testAlphabets();
        f.add(ggg);
    }

}