波多野结衣 蜜桃视频,国产在线精品露脸ponn,a v麻豆成人,AV在线免费小电影

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了如何在文本區上繪制?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用的程序只是通過在StackPane上使用畫布層在文本或圖像上進行繪制。我想要完成的是,當我釋放鼠標MouseEvent.MOUSE_RELEASED處理程序時,它會自動獲取畫布的快照,將圖像添加到ImageView Cover并將其顯示在TextArea的頂部,但它不能將更改添加到StackPane類,即ImageView

我這里有一個程序,我將把它添加到我正在處理的另一個程序中,并且我計劃將主類TextCanvas中的所有內容都從主項目中引入菜單控制器類。

主類TextCanvas.java:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class TextCanvas extends Application
{
    private ScrollPane Scroll = new ScrollPane();

    Canvas Can = new Canvas(800, 400);
    GraphicsContext GG = Can.getGraphicsContext2D();

    TextArea TA = new TextArea();

    ImageView Cover = new ImageView();

    VBox ButtonBox = new VBox();

    WordCanvas WC = new WordCanvas(Can, TA, GG);

    @Override
    public void start(Stage PrimaryStage)
    {
        ToggleGroup DrawErase = new ToggleGroup();

        ToggleButton Draw = new ToggleButton("Draw");
        ToggleButton Erase = new ToggleButton("Erase");
        Button Clear = new Button("Clear");
        Draw.setToggleGroup(DrawErase);
        Erase.setToggleGroup(DrawErase);

        double DotsPerInch = Screen.getPrimary().getDpi();
        double W = DotsPerInch * 8.5;
        double H = DotsPerInch * 11.0;

        StackPane Stack = new WordCanvas(W, H);

        WC.GetArea().setMaxWidth(W);
        WC.GetArea().setMaxHeight(H);
        WC.GetCan().setWidth(W);
        WC.GetCan().setHeight(H);

        DrawErase.selectedToggleProperty().addListener(new ChangeListener<Toggle>()
        {
            public void changed(ObservableValue<? extends Toggle> OV, Toggle TOld, Toggle TNew)
            {
                if(TNew == null)
                {
                    GG.setStroke(Color.TRANSPARENT);

                    Stack.getChildren().remove(WC.GetCan());
                }
                else if(DrawErase.getSelectedToggle().equals(Draw))
                {
                    GG.setStroke(Color.BLACK);

                    if(!Stack.getChildren().contains(WC.GetCan()))
                    {
                        Stack.getChildren().add(WC.GetCan());
                    }
                }
                else if(DrawErase.getSelectedToggle().equals(Erase))
                {
                    GG.setStroke(Color.WHITE);

                    if(!Stack.getChildren().contains(WC.GetCan()))
                    {
                        Stack.getChildren().add(WC.GetCan());
                    }
                }
            }
        });

        Clear.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                WC.GetGC().clearRect(0, 0, W, H);

                Stack.getChildren().remove(WC.GetCover());
            }
        });

        Button Snap = new Button("Snap");

        Snap.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent e)
            {
                Cover.setMouseTransparent(true);

                WritableImage WImage = Can.snapshot(new SnapshotParameters(), null);

                ByteArrayOutputStream Bos = new ByteArrayOutputStream();

                try
                {
                    ImageIO.write(SwingFXUtils.fromFXImage(WImage, null), "png", Bos);
                }
                catch(IOException ex)
                {
                    Logger.getLogger(WordCanvas.class.getName()).log(Level.SEVERE, null, ex);
                }

                InputStream Fis = new ByteArrayInputStream(Bos.toByteArray());
                Image Imos = new Image(Fis);

                int IW = (int) Imos.getWidth();
                int IH = (int) Imos.getHeight();

                WritableImage OutputImage = new WritableImage(IW, IH);
                PixelReader PReader = Imos.getPixelReader();
                PixelWriter PWriter = OutputImage.getPixelWriter();

                for (int y = 0; y < IH; y++)
                {
                    for (int x = 0; x < IW; x++)
                    {
                        int argb = PReader.getArgb(x, y);

                        int r = (argb >> 16) & 0xFF;
                        int g = (argb >> 8) & 0xFF;
                        int b = argb & 0xFF;

                        if(r >= 0xCF && g >= 0xCF && b >= 0xCF)
                        {
                            argb &= 0x00FFFFFF;
                        }

                        PWriter.setArgb(x, y, argb);
                    }
                }

                if(!Stack.getChildren().contains(WC.GetCover()))
                {
                    WC.GetCover().setImage(OutputImage);

                    Stack.getChildren().add(WC.GetCover());
                }
                else
                {
                    WC.GetCover().setImage(OutputImage);
                }
            }
        });

        ButtonBox.getChildren().addAll(Draw, Erase, Clear, Snap);

        BorderPane Border = new BorderPane();
        Border.setCenter(Stack);
        Border.setBottom(ButtonBox);

        Scroll.setContent(Border);
        Scroll.setFitToWidth(true);
        Scroll.setFitToHeight(true);

        Scene MainScene = new Scene(Scroll);

        PrimaryStage.setMaximized(true);
        PrimaryStage.setTitle("Practice Canvas");
        PrimaryStage.setScene(MainScene);
        PrimaryStage.show();
    }

    public static void main(String[] args)
    {
        launch(args);
    }
}

包含文本區域和畫布的次要類:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Cursor;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javax.imageio.ImageIO;

public class WordCanvas extends StackPane
{
    private Canvas Can = new Canvas();

    private GraphicsContext GC = Can.getGraphicsContext2D();

    private TextArea Area = new TextArea();

    private ImageView Cover = new ImageView();

    double Width;
    double Height;

    public WordCanvas()
    {
        CreateUI();
    }

    public WordCanvas(double W, double H)
    {
        this.Width = W;
        this.Height = H;

        CreateUI();
    }

    public WordCanvas(ImageView IV)
    {
        this.Cover = IV;
    }

    public WordCanvas(Canvas C, TextArea TA, GraphicsContext GG)
    {
        this.Can = C;
        this.Area = TA;
        this.GC = GG;

        CreateUI();
    }

    public void CreateUI()
    {
        Caligraphy();

        Imagination();

        Color C = Color.STEELBLUE;

        BackgroundFill BFill = new BackgroundFill(C, CornerRadii.EMPTY, Insets.EMPTY);

        Background BGround = new Background(BFill);

        this.getChildren().addAll(Area);

        this.setBackground(BGround);
    }

    public void Caligraphy()
    {
        Area.setMaxWidth(Width);
        Area.setMaxHeight(Height);
    }

    public void Imagination()
    { 
        double CanvasWidth = GC.getCanvas().getWidth();
        double CanvasHeight = GC.getCanvas().getHeight();

        GC.setFill(Color.TRANSPARENT);
        GC.fillRect(0, 0, Can.getWidth(), Can.getHeight());
        GC.rect(0, 0, CanvasWidth, CanvasHeight);
        GC.setLineWidth(3);

        Can.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                Can.setCursor(Cursor.CROSSHAIR);
            }
        });

        Can.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                Can.setCursor(Cursor.DEFAULT);
            }
        });

        Can.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                GC.beginPath();
                GC.lineTo(event.getX(), event.getY());
                GC.moveTo(event.getX(), event.getY());
                GC.stroke();
            }
        });

        Can.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                GC.lineTo(event.getX(), event.getY());
                GC.stroke();
            }
        });

        Can.addEventHandler(MouseEvent.MOUSE_RELEASED, new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                ImageView IV = new ImageView();

                WordCanvas Stack = new WordCanvas(IV);

                Cover.setMouseTransparent(true);

                WritableImage WImage = Can.snapshot(new SnapshotParameters(), null);

                ByteArrayOutputStream Bos = new ByteArrayOutputStream();

                try
                {
                    ImageIO.write(SwingFXUtils.fromFXImage(WImage, null), "png", Bos);
                }
                catch(IOException ex)
                {
                    Logger.getLogger(WordCanvas.class.getName()).log(Level.SEVERE, null, ex);
                }

                InputStream Fis = new ByteArrayInputStream(Bos.toByteArray());
                Image Imos = new Image(Fis);

                int IW = (int) Imos.getWidth();
                int IH = (int) Imos.getHeight();

                WritableImage OutputImage = new WritableImage(IW, IH);
                PixelReader PReader = Imos.getPixelReader();
                PixelWriter PWriter = OutputImage.getPixelWriter();

                for (int y = 0; y < IH; y++)
                {
                    for (int x = 0; x < IW; x++)
                    {
                        int argb = PReader.getArgb(x, y);

                        int r = (argb >> 16) & 0xFF;
                        int g = (argb >> 8) & 0xFF;
                        int b = argb & 0xFF;

                        if(r >= 0xCF && g >= 0xCF && b >= 0xCF)
                        {
                            argb &= 0x00FFFFFF;
                        }

                        PWriter.setArgb(x, y, argb);
                    }
                }

                if(!Stack.getChildren().contains(Cover))
                {
                    Cover.setImage(OutputImage);

                    Stack.getChildren().add(Cover);
                }
                else
                {
                    Cover.setImage(OutputImage);
                }
            }
        });
    }

    public void SetCan(Canvas C)
    {
        this.Can = C;
    }

    public Canvas GetCan()
    {
        return Can;
    }

    public void SetGC(GraphicsContext GG)
    {
        this.GC = GG;
    }

    public GraphicsContext GetGC()
    {
        return GC;
    }

    public void SetArea(TextArea TA)
    {
        this.Area = TA;
    }

    public TextArea GetArea()
    {
        return Area;
    }

    public ImageView GetCover()
    {
        return Cover;
    }
}

在主類中,Snap按鈕處理程序確實按我希望的那樣工作,但我想要的是,在輔助類中,Even處理程序自動創建快照并執行主類中的Snap按鈕所做的事情。但是我都沒試過,它甚至不接受this.getChildren().add(Cover)

另一個小問題是,我希望WritableImage通過更優雅的解決方案自動變得透明。User@珠海給出了一個完美工作的解決方案here,但我更喜歡不需要讀完每個像素的更短的解決方案。現有的PNG文件確實可以正常工作,但當我制作自己的PNG文件時,它們不是透明的。

推薦答案

不清楚為什么每次繪制都要創建畫布,玩父級的子級,以及每次都需要捕獲屏幕并創建圖像。

您只是想在文本區域上進行繪制,所以在文本區域上有一塊畫布,您可以在上面繪制。畫布的mouseTransparentProperty可用于確定哪個層獲得輸入。

public class TextCanvas extends Application {

    private GraphicsContext gc;

    @Override
    public void start(Stage primaryStage) {
        TextArea textArea = new TextArea();
        Canvas canvas = createCanvas();

        ToggleButton draw = new ToggleButton("Draw");
        ToggleButton erase = new ToggleButton("Erase");
        ToggleGroup drawErase = new ToggleGroup();
        draw.setToggleGroup(drawErase);
        erase.setToggleGroup(drawErase);
        drawErase.selectedToggleProperty().addListener((ov, oldV, newV) -> {
            if (newV == null) {
                gc.setStroke(Color.TRANSPARENT);
                canvas.setMouseTransparent(true);
            } else if (drawErase.getSelectedToggle().equals(draw)) {
                System.out.println("Fd");
                gc.setStroke(Color.BLACK);
                canvas.setMouseTransparent(false);
            } else if (drawErase.getSelectedToggle().equals(erase)) {
                gc.setStroke(Color.WHITE);
                canvas.setMouseTransparent(false);
            }
        });

        Button clear = new Button("Clear");
        clear.setOnAction(e -> gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight()));

        CheckBox cb = new CheckBox("Show canvas");
        cb.setSelected(true);
        canvas.visibleProperty().bind(cb.selectedProperty());

        VBox buttonBox = new VBox(draw, erase, clear, cb);

        StackPane stack = new StackPane(textArea, canvas);
        ScrollPane scrollPane = new ScrollPane(stack);
        scrollPane.setFitToHeight(true);

        canvas.widthProperty().bind(scrollPane.widthProperty());
        canvas.heightProperty().bind(scrollPane.heightProperty());

        stack.setBackground(new Background(new BackgroundFill(Color.STEELBLUE, CornerRadii.EMPTY, Insets.EMPTY)));

        BorderPane border = new BorderPane();
        border.setCenter(scrollPane);
        border.setBottom(buttonBox);

        primaryStage.setMaximized(true);
        primaryStage.setTitle("Practice Canvas");
        primaryStage.setScene(new Scene(border));
        primaryStage.show();
    }

    private Canvas createCanvas() {
        Canvas canvas = new Canvas();
        gc = canvas.getGraphicsContext2D();
        gc.setLineWidth(3);

        canvas.setOnMouseEntered(event -> canvas.setCursor(Cursor.CROSSHAIR));
        canvas.setOnMouseExited(event -> canvas.setCursor(Cursor.DEFAULT));

        canvas.setOnMousePressed(event -> {
            gc.beginPath();
            gc.lineTo(event.getX(), event.getY());
            gc.moveTo(event.getX(), event.getY());
            gc.stroke();
        });
        canvas.setOnMouseDragged(event -> {
            gc.lineTo(event.getX(), event.getY());
            gc.stroke();
        });

        canvas.setMouseTransparent(true);
        return canvas;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

編輯:我添加了一個復選框來切換畫布的可見性。您必須明確您的使用要求,了解每個用戶操作對每個模式執行的操作。在任何情況下,這應該足夠玩了。

另外,使用適當的Java命名約定:局部變量、(非常量)字段名和方法參數應以小寫開頭。

這篇關于如何在文本區上繪制?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:如何在 文本 繪制
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定