簡體   English   中英

如何在Java Swing中在屏幕上切換形狀?

[英]How can I toggle shapes on and off screen in Java Swing?

好的,所以我試圖使用秋千構建一個程序,其中必須能夠在屏幕的右下角繪制一個橢圓形和矩形。 我目前正在嘗試使用以前的程序,其中該程序使用JToggleSwitch啟動和停止正方形。 我的問題是,由於它需要參數“ Graphics g”,我如何才能獲得在屏幕上切換的形狀。 到目前為止,這是我的PaintPanel代碼。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PaintPanel extends JPanel
{
    private static Color[] colors =
        { Color.RED, Color.BLACK, Color.PINK, Color.ORANGE };
    private int colorNumber = 0;

    private int shape = 0;

    private int x = 0;
    private int y = 0;

    private int width = 100;
    private int height = 100;

    private int dx = 2;
    private int dy = 2;

    private String displayString = "hello";

    private boolean isStarted = true;

    public void update()
    {
        if (isStarted) 
        {
            x += dx;
            y += dy;
        }
        //dx ++;
        //dy ++;

        if (y + height > getHeight())
        {
            dy = -Math.abs(dy);
            shape++;
            colorNumber = (colorNumber+1)%colors.length;
        }
        else if (y < 0)
        {
            dy = Math.abs(dy);
            shape++;
            colorNumber = (colorNumber+1)%colors.length;
        }

        if (x + width > getWidth())
        {
            dx = -Math.abs(dx);
            shape++;
            colorNumber = (colorNumber+1)%colors.length;
        }
        else if (x < 0)
        {
            dx = Math.abs(dx);
            shape++;
            colorNumber = (colorNumber+1)%colors.length;
        }

    }

    public void changeColor()
    {
        colorNumber = (colorNumber+1) % colors.length;
    }

    public void startStop()
    {
        //if (isStarted == true) isStarted = false;
        //else isStarted = true;

        isStarted = !isStarted;
    }

    public void setDisplayText(String dt)
    {
        displayString = dt;
    }
    public void paintRectangle(Graphics g)
    {

        int w = getWidth();
        int h = getHeight();
        g.setColor(Color.PINK);
        g.fillRect(w/2, h/2, w/2, h/2);
        //g.setColor(Color.CYAN);
        //g.fillOval((5*w)/8, (5*h)/8, w/4, h/4);


    }


    public void paintComponent(Graphics g)
    {



        int w = getWidth();
        int h = getHeight();
        g.setColor(colors[colorNumber]);
        if (shape % 2 == 0) //g.fillOval(x, y, width, height);
        /*else*/ g.fillRect(x,y, width, height);


        int textx = x+width/2;
        int texty = y+height/2;

        FontMetrics fm = g.getFontMetrics();
        int texth = fm.getHeight();
        int textw = fm.stringWidth(displayString);

        textx -= textw/2;
        texty += texth/2;
        texty -= 5;     

        g.setColor(colors[(colorNumber+1)%colors.length]);
        g.drawString(displayString, textx, texty);
    }

}

這是創建窗口和面板的實際代碼。 這是我的主班。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GuiTest extends JFrame 
    implements ActionListener
{
    private Timer frameTimer;
    private JToggleButton name;
    private JToggleButton ovalButton;
    private JToggleButton rectangle;
    //private JTextField theText;
    private PaintPanel paintPanel;

    public GuiTest()
    {
        setTitle("Servando Hernandez");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new GridLayout(1,3));

        name = new JToggleButton("Name");
        name.addActionListener(this);

        ovalButton = new JToggleButton("Oval");
        ovalButton.addActionListener(this);

        rectangle = new JToggleButton("Rectangle");
        rectangle.addActionListener(this);

        //theText = new JTextField("HI");
        //theText.addActionListener(this);


        topPanel.add(name);
        topPanel.add(ovalButton);
        topPanel.add(rectangle);
        //topPanel.add(theText);

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(topPanel, BorderLayout.SOUTH);

        paintPanel = new PaintPanel();
        contentPane.add(paintPanel, BorderLayout.CENTER);

        frameTimer = new Timer(50, this);
        frameTimer.start();
    }

    public void actionPerformed(ActionEvent e)
    {
        //System.out.println("Action performed");
        if (e.getSource() == name)      
        {
            paintPanel.changeColor();
        }
        else if (e.getSource() == frameTimer)
        {
            paintPanel.update();
        }
        else if (e.getSource() == ovalButton)
        {
            //System.out.println("start/stop");
            paintPanel.startStop();
        }
        else if (e.getSource() == rectangle)
        {

        }
        /*else if (e.getSource() == theText)
        {
            String text = e.getActionCommand();
            paintPanel.setDisplayText(text);
            //System.out.println(text);
        }
        */
        repaint();
    }

    public static void main(String[] args)
    {
        GuiTest gui = new GuiTest();
        gui.setVisible(true);

    }
}

你可以...

維護要繪制的Shape List ,根據需要添加或刪除它們。 然后,您可以在paintComponent方法中遍歷列表,繪制List並在需要更新組件時簡單地調用repaint

你可以...

具有一系列boolean (或其他類型的)標志,這些標志指示要繪制的形狀。 這種類型的修復可以繪制哪些形狀,並且實際上不允許您控制形狀的z順序

你應該...

在進行任何自定義繪制之前,請先調用super.paintComponent ,否則每次繪制該組件時,您可能會生成不需要的繪制工件。 請記住,繪畫是破壞性的,當調用paintComponent時,您應該完全重新繪制組件的當前狀態。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM