如何在java中画心?

时间:2018-04-08 17:46:03

标签: java graphics heartbeat javax

我的朋友和我正在进行爱情计算器项目,我们很难在javax swing中画出一颗心。有人可以帮助我们吗?谢谢!我正在为整个测验添加代码。 (该测验由3个班级构建..) 我想在第一页和最后一页添加心形 我们确实已经添加了实际问题,将来会添加。我们只是初学者,对于这个成熟的代码感到抱歉。

    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class newTry3 extends JFrame implements ActionListener
{
    private int counter=1;
    private JButton b;
    private JButton bl;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JFrame jf;
    private JLabel jl;
    private String[][] context;
    private player currentPlayer;
    private player p1;
    private player p2;
    private JPanel transPage;
    private JPanel firstPage;
    private JPanel jp1;
    private JPanel endPage;

    public newTry3 (player p1, player p2)
    {
        this.currentPlayer=p1;
        this.p1=p1;
        this.p2=p2;
        context= new ArrayTexts().GetArray();

        //creating the JFrame
        jf= new JFrame();
        jf.setSize(1000,1000);
        jf.setVisible(true);
        jf.setLayout(new FlowLayout());

        firstPage= new JPanel();
        transPage= new JPanel();
        jp1=new JPanel();
        endPage= new JPanel();

        jf.add(firstPage,BorderLayout.CENTER);
        endPage.setLayout(new GridLayout(4,1));
        jp1.setLayout(new GridLayout(4,1));
        transPage.setLayout(new GridLayout(4,1));
        GridBagConstraints cl= new GridBagConstraints();

        jf.add(firstPage,cl);
        jf.add(jp1,cl);


        jl= new JLabel(this.context[0][0]);
        b1= new JButton(this.context[0][1]);
        b2= new JButton(this.context[0][2]);
        b3= new JButton(this.context[0][3]);

        jl.setVisible(true);
        b1.setVisible(true);
        b2.setVisible(true);
        b3.setVisible(true);

        jl.setFont(new Font("David", 7,50));
        b1.setFont(new Font("David", 7,30));
        b2.setFont(new Font("David", 7,30));
        b3.setFont(new Font("David", 7,30));

        jl.setBackground(Color.pink);
        b1.setBackground(Color.pink);
        b2.setBackground(Color.pink);
        b3.setBackground(Color.pink);

        GridBagConstraints c= new GridBagConstraints();
        c.insets= new Insets(100,100,100,100);
        c.gridx=0;
        c.gridy=1;
        jp1.setSize(1000,1000);
        transPage.setSize(1000,1000);
        endPage.setSize(1000,1000);
        firstPage.setSize(1000,1000);

        jp1.add(jl,c);
        jp1.add(b1,c);
        jp1.add(b2,c);
        jp1.add(b3,c);

        jp1.setVisible(false);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        actionPerformed(new ActionEvent(jf,5,"now"));

        FirstPage();
    }
    //action that creats the first page of the quiz.
    public void FirstPage(){
        jf.add(firstPage);

        b= new JButton("start");
        JLabel ja= new JLabel("explenation");
        JLabel ja1= new JLabel(" explenation");

        firstPage.setLayout(new GridLayout(4,1));
        firstPage.add(b);
        firstPage.add(ja);
        firstPage.add(ja1);

        b.setFont(new Font("David", 7,100));
        b.setBackground(Color.pink);
        b.addActionListener(this);
        actionPerformed(new ActionEvent(jf,5,"now"));
    }
    //action that creats the transition page
    public void TransitionPage(){

        JLabel ju= new JLabel("You have completed the quiz, Now it is your partners turn");
        bl= new JButton("continue");

        bl.setFont(new Font("David", 7,100));
        bl.setBackground(Color.pink);
        bl.addActionListener(this);


        transPage.add(bl);
        transPage.add(ju);
        transPage.setLayout(new GridLayout(4,1));
        transPage.setVisible(true);
jf.add(transPage);
        actionPerformed(new ActionEvent(jf,5,"now"));
    }


    //action that changes the texts to the next page's text. also adds 1 to the the current page number
    public void change(){

        if(counter<5 ){
            jl.setText(context[counter][0]);
            b1.setText(context[counter][1]);
            b2.setText(context[counter][2]);
            b3.setText(context[counter][3]);
            counter++;  

        }
        //if the quiz ends and the playeranswering is the first one, activates the transition page
        else if(this.getCurrentPlayer().equals(p1)){
            jp1.setVisible(false);
            TransitionPage();
            this.setCounter(0);
            this.setCurrentPlayer(p2);
            jl.setText(context[counter][0]);
            b1.setText(context[counter][1]);
            b2.setText(context[counter][2]);
            b3.setText(context[counter][3]);
            counter++;
        }
//      if both of the players finished answering, activates the finish page
        else if(counter==5&&this.getCurrentPlayer().equals(p2)){
            jp1.setVisible(false);
            endPage(matchPercent());
//          System.out.println("player 2 score is"+this.p2.getScore());
        }
    }

    public void actionPerformed(ActionEvent e){
        if(e.getSource() == b1){
            this.getCurrentPlayer().addScore(1);
            System.out.println("p1 score+"+ this.getCurrentPlayer().getScore());
            change();

        }else if(e.getSource() == b2){
            change();
            this.getCurrentPlayer().addScore(2);

        }else if(e.getSource() == b3){
            change();
            this.getCurrentPlayer().addScore(3);

        }
        else if(e.getSource()== b){
            jp1.setVisible(true);
            firstPage.setVisible(false);

        }else if(e.getSource()== bl){
            jp1.setVisible(true);
            transPage.setVisible(false);
        }

    }
    //action that calculate the match percent of the two players according to an algorithem
    public int matchPercent(){
        int p1Score= this.p1.getScore();
        int p2Score= this.p2.getScore();
        int match=0;

        if(p1Score+p2Score>20){
            match= 100-(((p1Score+p2Score)-20)*10);
        }else{
            match= 100-((20-(p1Score+p2Score))*10);
        }
        System.out.println("match:" + match);
        return match;
    }

    //action that creats the finish page
    public void endPage(int match){
        endPage= new JPanel();
        JLabel j2= new JLabel("");
        JLabel j3= new JLabel("");
        JLabel jl= new JLabel("congrats for finishing the quiz!");
        JLabel j4= new JLabel("your match is:"+match);

        jl.setFont(new Font("david",7,70));
        j4.setFont(new Font("david",7,70));

        endPage.setLayout(new GridLayout(4,1));
        endPage.add(j3);
        endPage.add(j2);
        endPage.add(jl);
        endPage.add(j4);
        endPage.setVisible(true);
        jf.add(endPage);

    }
    public player getCurrentPlayer()
    {
        return this.currentPlayer;
    }
    public void setCurrentPlayer(player currentPlayer)
    {
        this.currentPlayer = currentPlayer;
    }

    public int getCounter()
    {
        return this.counter;
    }

    public void setCounter(int counter)
    {
        this.counter = counter;
    }
    public static void main(String[] args)
    {
        player p1= new player("mor",0,2002);
        player p2= new player("mor",0,2002);
        newTry3 new3= new newTry3(p1,p2);
    }
}



public class player
{
    private String name;
    private int score;
    private int yearOfBirth;
    public player(String name, int score, int yearOfBirth){
        this.name=name;
        this.score=score;
        this.yearOfBirth=yearOfBirth;
    }
    public String getName()
    {
        return this.name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getScore()
    {
        return this.score;
    }
    public void addScore(int score)
    {
        this.score = this.score+score;
    }
    public int getYearOfBirth()
    {
        return this.yearOfBirth;
    }
    public void setYearOfBirth(int yearOfBirth)
    {
        this.yearOfBirth = yearOfBirth;
    }

}


public class ArrayTexts{
    private static final int PAGES=5;
    private static final int TEXTS=4;
    private String [][] array;
    public ArrayTexts(){
        //the type arraeTexts includes all of the thext that the quiz has. 5 pages, each has 1 question and three answers (5*4)
        array= new String[PAGES][TEXTS];
        array[0][0]= "1.    q 1 ";
        array[0][1]="   1";
        array[0][2]= "  2";
        array[0][3]="  3.";
        array[1][0]="    2. ?";
        array[1][1]="1";
        array[1][2]=    "2";
        array[1][3]="3.";

        array[2][0]="  3. q3";
        array[2][1]=" 1";
        array[2][2]=    "2";
        array[2][3]=" 3";

        array[3][0]=". q4? ";
        array[3][1]="1";
        array[3][2]=    "2";
        array[3][3]="3";

        array[4][0]=". q5?";
        array[4][1]="1";
        array[4][2]=    "2";
        array[4][3]="3";
    }
    public String[][] GetArray(){
        return this.array;
    }

}

1 个答案:

答案 0 :(得分:0)

这可以通过在java中使用StdDraw库来完成,其中 标准绘图使我们可以在java应用程序中使用图形。

为了画一颗心,你应该首先画一颗钻石。然后在钻石的左上角和右上角画两个圆圈。 :) StdDraw在以下代码中绘制依赖关系。以下代码将提供一个漂亮的粉红色心脏。

public class Heart {
    public static void main(String[] args) {
        StdDraw.setXscale(-1.5, +1.5);
        StdDraw.setYscale(-1.5, +1.5);
        StdDraw.setPenColor(StdDraw.PINK);

        // draw the diamond first
        double[] xs = { -1,  0, 1, 0 };
        double[] ys = {  0, -1, 0, 1 };
        StdDraw.filledPolygon(xs, ys);

        // circles
        StdDraw.filledCircle(+0.5, 0.5, 1 / Math.sqrt(2));
        StdDraw.filledCircle(-0.5, 0.5, 1 / Math.sqrt(2));
    }
  }

参考

以下文章可能会有所帮助: https://introcs.cs.princeton.edu/java/15inout/ 心的例子:https://introcs.cs.princeton.edu/java/15inout/Heart.java.html StdDraw类: https://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html