JavaFX manually set NumberAxis ticks - javafx

I have a LineChart that displays a market indicator such as this one:
As you see, what is interesting when using this kind of indicator is when the indicator is above or below threshold lines.
I want to do exactly as shown on the picture above: only have ticks on the YAxis at given values (middle, upper limit, lower limit).
Is it possible?

Following #kleopatra advice in the comments, I implemented my own NumberAxis:
public class FixedTicksAxis extends ValueAxis<Number> {
// List of ticks
private final List<Number> ticks;
// Formatter
private NumberAxis.DefaultFormatter defaultFormatter;
public FixedTicksAxis(Number... ticks) {
super();
this.ticks = Arrays.asList(ticks);
this.defaultFormatter = new NumberAxis.DefaultFormatter(new NumberAxis());
}
#Override
protected List<Number> calculateMinorTickMarks() {
return new ArrayList<>();
}
#Override
protected void setRange(Object range, boolean animate) {
final double[] rangeProps = (double[]) range;
final double lowerBound = rangeProps[0];
final double upperBound = rangeProps[1];
final double scale = rangeProps[2];
setLowerBound(lowerBound);
setUpperBound(upperBound);
currentLowerBound.set(lowerBound);
setScale(scale);
}
#Override
protected Object getRange() {
return new double[]{
getLowerBound(),
getUpperBound(),
getScale(),
};
}
#Override
protected List<Number> calculateTickValues(double length, Object range) {
return ticks;
}
#Override
protected String getTickMarkLabel(Number value) {
StringConverter<Number> formatter = getTickLabelFormatter();
if (formatter == null) formatter = defaultFormatter;
return formatter.toString(value);
}
}
NumberAxis is final, so I can't subclass it.
I didn't want to copy/paste the content of NumberAxis, but I cannot use a delegate for the protected methods, so I did not find any other way than copy/pasting...
But it works, using:
FixedTicksAxis yAxis = new FixedTicksAxis(30, 50, 70);
We get:

Related

JavaFX - Get Actual Computed-Size of HBOX

I have a simple HBOX control which uses USE_COMPUTED_SIZE in Pref-Height, hence the size is all calculated and adjusted by the controls inside, which are a couple VBOX.
The issue comes when I try to add a new Pane as a children to the HBOX and draw a vertical line from top to bottom of the HBOX, so I write my line:
int startX = 5;
int startY = 0;
int endX = 5;
Line line = new Line(startX,startY,endX,hbox.getHeight());
Here, I need the hbox.getHeight(), but surprise: it is =-1, because it is using USE_COMPUTED_SIZE. So, how can I get the real (computed) value of hbox.getHeight()?
Not tested, but I think you can do something like:
public class HBoxWithLine extends HBox {
// Example of configurable property:
private final DoubleProperty lineOffset = new SimpleDoubleProperty(5);
public DoubleProperty lineOffsetProperty() {
return lineOffset ;
}
public final double getLineOffset() {
return lineOffsetProperty().get();
}
public final void setLineOffset(double lineOffset) {
lineOffsetProperty().set(lineOffset);
}
private final Line line = new Line();
public HBoxWithLine() {
getChildren().add(line);
// request layout when offset is invalidated:
lineOffset.addListener(obs -> requestLayout());
}
#Override
protected void layoutChildren() {
line.setStartX(getLineOffset());
line.setEndX(getLineOffset());
line.setStartY(0);
line.setEndY(getHeight());
super.layoutChildren();
}
}
Now you can just create a HBoxWithLine and add (additional) child nodes to it, set it's pref width and height to either fixed values, or Region.USE_COMPUTED_SIZE, etc., and it should just work.

Update the Jfreechart in the SwingNode with the Slider

I am using SwingNode to insert JFreeChart into a JavaFX application. I am able to plot the Line graph using arrays. I want to update the Chart with the slider values. I mean the chart should be movable in the graphical area.
Here are my Controller code and the code to plot graph
I don't have any exception everything executes properly but I am not able to connect the slider with graph. Please help me in finding the mistake I have done.
public class FXMLController implements Initializable
{
#FXML
private void open_btn_action(ActionEvent event)
{
xArray = new double[]{0.0};
yArray = new double[]{0.0};
fc=new FileChooser();
fc.getExtensionFilters().addAll();
fc.getExtensionFilters().addAll(new ExtensionFilter("Text Files","*.txt"));
fc.getExtensionFilters().addAll(new ExtensionFilter("CSV Files","*.csv"));
file=fc.showOpenDialog(null);
if(file!=null)
{ //System.out.println(file.getAbsolutePath());
try
{
count = (int) Files.lines(file.toPath(), Charset.defaultCharset()).count();
}
catch (IOException ex)
{
Logger.getLogger(Data.class.getName()).log(Level.SEVERE, null, ex);
}
data = Data.getData(file,count);
xArray = new double[data.length];
yArray = new double[data.length];
xArray = Data.array_x_value(data);
yArray = Data.array_y_value(data);
chart = Chart.chart_plot(xArray,yArray);
final SwingNode chartSwingNode = new SwingNode();
final SwingNode sliderNode = new SwingNode();
chartSwingNode.setContent(new ChartPanel(chart));
final JSlider slider = new JSlider(0,xArray.length);
slider.addChangeListener(new ChangeListener()
{
#Override
public void stateChanged(ChangeEvent e)
{
final XYPlot plot = (XYPlot) chart.getPlot();
plot.setDataset(Chart.list.get(slider.getValue()));
System.out.println(slider.getValue());
}});
sliderNode.setContent(slider);
//Adding Chart to the splitPane and slider on the display
bPane.setCenter(chartSwingNode);
bPane.setBottom(sliderNode);
upperCount = xArray.length;
mainSplit.getItems().set(1,bPane);
}
}
}
//Chart Class to plot chart
public class Chart
{
static JFreeChart chart;
static double[] xValues,yValues;
static XYPlot plot;
static List<XYDataset> list = new ArrayList<XYDataset>();
public static JFreeChart chart_plot(double[] x,double[] y)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // for look and feel like a desktop application
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException e) { }
xValues=new double[x.length];
xValues=x;
yValues=new double[y.length];
yValues=y;
XYDataset dataset=createDataset(xValues,yValues);
chart = ChartFactory.createXYLineChart("","Wave number cm"+'\u2212'+'\u00B9',"Intensity",
dataset, PlotOrientation.VERTICAL,true, true, false);
chart.getPlot().setBackgroundPaint(new Color(235, 244, 250));
plot = (XYPlot)chart.getPlot();
final NumberAxis axis2 = (NumberAxis) plot.getRangeAxis();
//axis2.setLabelPaint(Color.magenta);
//axis2.setTickLabelPaint(Color.darkGray);
axis2.setAutoRange(true);
axis2.setAutoRangeIncludesZero(false);
plot.getRenderer().setSeriesPaint(0, new Color(10,11,45));
plot.getRenderer().setSeriesStroke(0,new BasicStroke(1.25f));
plot.setDomainPannable(false);
plot.setDomainGridlinePaint(new Color(0xC0,0xC0,0xC0));
plot.setRangeGridlinePaint(new Color(0xC0,0xC0,0xC0));
plot.setRangeZeroBaselineVisible(false);
plot.setDomainZeroBaselineVisible(false);
plot.setOutlineVisible(false);
ValueAxis domain = plot.getDomainAxis();
domain.setAutoRange(true);
//range.setRange(-MINMAX, MINMAX);
plot.configureRangeAxes();
plot.setDomainCrosshairVisible(true);
plot.setDomainCrosshairLockedOnData(true);
plot.setRangeCrosshairVisible(true);
//plot.setOutlinePaint(Color.gray);
//System.out.println(plot.isRangeZeroBaselineVisible());
XYLineAndShapeRenderer renderer =
(XYLineAndShapeRenderer) plot.getRenderer();
ChartListener.chartListner(chart);
//System.out.println("X length="+ xValues.length);
list = new ArrayList<XYDataset>();
for (int i = 0; i <= xValues.length; i++) {
list.add(getDataset(i));
}
return chart;
}
private static XYDataset getDataset(int n) {
final XYSeries series = new XYSeries("series1");
double values;
for (int length = 0; length < xValues.length; length++) {
values = xValues[length];
series.add(values, yValues[length]);
}
return new XYSeriesCollection(series);
}
public static XYDataset createDataset(double[] array_x,double[] array_y)
{
DefaultXYDataset ds = new DefaultXYDataset();
double[][] data={array_x,array_y};
ds.addSeries("series1", data);
return ds;
}
}
Thanks in advance

How to set DateAxis TickUnit (Date Interval) in JavaFX?

I'm developing a chart using JavaFX. I needed DateAxis, so I use
DateAxis from this code. The result is this. But I want show 2 date
interval, I didn't find solution. The result always show one day interval. This is my code.
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
ObservableList<XYChart.Series<Date, Number>> series = FXCollections.observableArrayList();
ObservableList<XYChart.Data<Date, Number>> series1Data = FXCollections.observableArrayList();
series1Data.add(new XYChart.Data<Date, Number>(new GregorianCalendar(2018,1, 12).getTime(), 2));
series1Data.add(new XYChart.Data<Date, Number>(new GregorianCalendar(2018, 1, 15).getTime(), 3));
series1Data.add(new XYChart.Data<Date, Number>(new GregorianCalendar(2018, 1, 16).getTime(), 4));
series.add(new XYChart.Series<>("Series1", series1Data));
NumberAxis numberAxis = new NumberAxis();
DateAxis dateAxis = new DateAxis();
dateAxis.setAutoRanging(false);
dateAxis.setLowerBound(new GregorianCalendar(2018,2, 12).getTime());
dateAxis.setUpperBound(new GregorianCalendar(2018,2, 16).getTime());
dateAxis.setTickLabelFormatter(new StringConverter<Date>() {
#Override
public String toString(Date object) {
return simpleDateFormat.format(object);
}
#Override
public Date fromString(String string) {
return null;
}
});
LineChart lineChart = new LineChart(dateAxis, numberAxis, series);
Scene scene = new Scene(lineChart, 900, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
How can I do that?
I might be wrong, but it seems that DateAxis API does not allow you to specify a preferred displayed interval, so you need to change it yourself.
In DateAxis class there's an Interval enum. If you change this line:
DAY(Calendar.DATE, 1),
To this one:
DAY(Calendar.DATE, 2),
And comment other intervals as necessary...
// HOUR_12(Calendar.HOUR, 12),
2-day intervals will be shown. Keep in mind that doing this is contrary to the author's intent, who wrote it this way so that the intervals are adjusted automatically, and it's a very crude solution. For a better one, you'd have to rewrite the calculateTickValues method, as well as change the private enum to some kind of public API.

JavaFX WhatApp-Like ConversationView

I'm trying to make a WhatsApp-Like Conversation-View in JavaFX.
In order to make the sent messages appear on the right and the received messages appear on the left then I cannot use TextArea. How can I do it? I tried GridPane without TextArea but it didn't make things easier.
Moreover, is it a good practice to make controls static?
Extra: if you can also help me do the chat bubble behind the text, it would be great.
Here is my code:
public class ConversationView implements WhatAppView {
private static Label nameLabel, statusLabel;
private static TextField messageTextField;
static TextArea messagesTextArea;
private static GridPane conversationSection;
private static Label changeViewLink;
private static Button sendMsgButton;
// private static int rowIndex = 1;
public void showView() {
AppMain.stage.setResizable(false);
AppMain.stage.setWidth(350);
AppMain.stage.setHeight(550);
BorderPane rootPane = new BorderPane();
rootPane.setPadding(new Insets(5, 5, 5, 5));
final int sectionHeight = 55;
StackPane contactSection = new StackPane();
nameLabel = new Label("RW");
statusLabel = new Label("Online");
changeViewLink = new Label("Go Back");
changeViewLink.setStyle("-fx-text-fill: blue;");
changeViewLink.styleProperty().bind(
Bindings.when(changeViewLink.hoverProperty())
.then(new SimpleStringProperty("-fx-underline: true; -fx-text-fill: blue;"))
.otherwise(new SimpleStringProperty("-fx-underline: false; -fx-text-fill: blue;")));
changeViewLink.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
AppMain.changeView(new ChatsView());
}
});
contactSection.getChildren().addAll(nameLabel, statusLabel, changeViewLink);
StackPane.setAlignment(changeViewLink, Pos.TOP_RIGHT);
StackPane.setAlignment(statusLabel, Pos.BOTTOM_CENTER);
contactSection.setPrefHeight(sectionHeight);
conversationSection = new GridPane();
conversationSection.setStyle("-fx-background-image: url('whatsapp-wallpaper.jpg')");
messagesTextArea = new TextArea();
messagesTextArea.setEditable(false);
// conversationSection.getColumnConstraints().addAll(new
// ColumnConstraints(AppMain.stage.getWidth()/2 - 10), new
// ColumnConstraints(AppMain.stage.getWidth()/2 - 10));
conversationSection.add(messagesTextArea, 0, 0);
conversationSection.setPrefSize(AppMain.stage.getWidth(), AppMain.stage.getHeight());
// conversationSection.getStylesheets().add("conversation.css");
ScrollPane scroll = new ScrollPane();
scroll.setPrefSize(conversationSection.getWidth(), conversationSection.getHeight());
scroll.setContent(conversationSection);
FlowPane messageSection = new FlowPane();
sendMsgButton = new Button("_Send");
sendMsgButton.setDisable(true);
sendMsgButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
sendMsg();
}
});
sendMsgButton.setPrefHeight(sectionHeight);
Tooltip sendMsgToolTip = new Tooltip("Send Message");
Tooltip.install(sendMsgButton, sendMsgToolTip);
FlowPane.setMargin(sendMsgButton, new Insets(0, 0, 0, 5));
messageTextField = new TextField();
messageTextField.setPromptText("Type your message here...");
Platform.runLater(new Runnable() { // 100% focus
public void run() {
messageTextField.requestFocus();
}
});
messageTextField.setPrefWidth(AppMain.stage.getWidth() - AppMain.stage.getWidth() / 5);
messageTextField.setPrefHeight(sectionHeight);
messageTextField.setAlignment(Pos.TOP_LEFT);
messageTextField.setOnKeyTyped(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (messageTextField.getText() != null && !messageTextField.getText().isEmpty()) {
sendMsgButton.setDisable(false);
} else {
sendMsgButton.setDisable(true);
}
}
});
messageTextField.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode().equals(KeyCode.ENTER) && messageTextField.getText() != null
&& !messageTextField.getText().isEmpty()) {
sendMsg();
}
}
});
messageSection.getChildren().add(messageTextField);
messageSection.getChildren().add(sendMsgButton);
messageSection.setPrefHeight(sectionHeight);
rootPane.setTop(contactSection);
rootPane.setCenter(conversationSection);
rootPane.setBottom(messageSection);
Scene scene = new Scene(rootPane);
AppMain.stage.setScene(scene);
AppMain.stage.setTitle("WhatsApp");
}
}
public class AppMain extends Application {
static Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
AppMain.stage.show();
changeView(new ConversationView());
}
public static void changeView(WhatAppView view) {
view.showView();
}
}
public interface WhatAppView {
public void showView();
}
You can create a custom control to determine message alignment and aesthetics such as the bubble like appearance. As a fan of HBox and VBox, I would recommend their usage in combination with an SVGPath to decorate the message.
SVGPath's let you draw custom shapes by providing information on the lines, arcs etc. These aren't unique to java so there are a few resources available to see some basic/advanced examples. My recommendation would be to read here: SVGPath and use the TryitEditor to experiment
Here are two quick examples:
When it comes to laying out the messages a VBox would suffice. You can bind the viewable children to an ObservableList of messages you would be able to iterate later. The added benefit of this is that adding to the list will update the UI automatically, and you'll also be able to iterate these later in the event you implement additional features such as delete, forward etc
I'd recommend reading up on the Bindings api, particularly bindContentBidirectional for more information on this
Using my above recommendations i've written a small example below you can reference. It's not visually impressive, but hopefully you can get some ideas from it, particularly this:
Extra: if you can also help me do the chat bubble behind the text, it
would be great.
The messages/speech bubbles:
enum SpeechDirection{
LEFT, RIGHT
}
public class SpeechBox extends HBox{
private Color DEFAULT_SENDER_COLOR = Color.GOLD;
private Color DEFAULT_RECEIVER_COLOR = Color.LIMEGREEN;
private Background DEFAULT_SENDER_BACKGROUND, DEFAULT_RECEIVER_BACKGROUND;
private String message;
private SpeechDirection direction;
private Label displayedText;
private SVGPath directionIndicator;
public SpeechBox(String message, SpeechDirection direction){
this.message = message;
this.direction = direction;
initialiseDefaults();
setupElements();
}
private void initialiseDefaults(){
DEFAULT_SENDER_BACKGROUND = new Background(
new BackgroundFill(DEFAULT_SENDER_COLOR, new CornerRadii(5,0,5,5,false), Insets.EMPTY));
DEFAULT_RECEIVER_BACKGROUND = new Background(
new BackgroundFill(DEFAULT_RECEIVER_COLOR, new CornerRadii(0,5,5,5,false), Insets.EMPTY));
}
private void setupElements(){
displayedText = new Label(message);
displayedText.setPadding(new Insets(5));
displayedText.setWrapText(true);
directionIndicator = new SVGPath();
if(direction == SpeechDirection.LEFT){
configureForReceiver();
}
else{
configureForSender();
}
}
private void configureForSender(){
displayedText.setBackground(DEFAULT_SENDER_BACKGROUND);
displayedText.setAlignment(Pos.CENTER_RIGHT);
directionIndicator.setContent("M10 0 L0 10 L0 0 Z");
directionIndicator.setFill(DEFAULT_SENDER_COLOR);
HBox container = new HBox(displayedText, directionIndicator);
//Use at most 75% of the width provided to the SpeechBox for displaying the message
container.maxWidthProperty().bind(widthProperty().multiply(0.75));
getChildren().setAll(container);
setAlignment(Pos.CENTER_RIGHT);
}
private void configureForReceiver(){
displayedText.setBackground(DEFAULT_RECEIVER_BACKGROUND);
displayedText.setAlignment(Pos.CENTER_LEFT);
directionIndicator.setContent("M0 0 L10 0 L10 10 Z");
directionIndicator.setFill(DEFAULT_RECEIVER_COLOR);
HBox container = new HBox(directionIndicator, displayedText);
//Use at most 75% of the width provided to the SpeechBox for displaying the message
container.maxWidthProperty().bind(widthProperty().multiply(0.75));
getChildren().setAll(container);
setAlignment(Pos.CENTER_LEFT);
}
}
Conversation window:
public class ConversationView extends VBox{
private String conversationPartner;
private ObservableList<Node> speechBubbles = FXCollections.observableArrayList();
private Label contactHeader;
private ScrollPane messageScroller;
private VBox messageContainer;
private HBox inputContainer;
public ConversationView(String conversationPartner){
super(5);
this.conversationPartner = conversationPartner;
setupElements();
}
private void setupElements(){
setupContactHeader();
setupMessageDisplay();
setupInputDisplay();
getChildren().setAll(contactHeader, messageScroller, inputContainer);
setPadding(new Insets(5));
}
private void setupContactHeader(){
contactHeader = new Label(conversationPartner);
contactHeader.setAlignment(Pos.CENTER);
contactHeader.setFont(Font.font("Comic Sans MS", 14));
}
private void setupMessageDisplay(){
messageContainer = new VBox(5);
Bindings.bindContentBidirectional(speechBubbles, messageContainer.getChildren());
messageScroller = new ScrollPane(messageContainer);
messageScroller.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
messageScroller.setHbarPolicy(ScrollBarPolicy.NEVER);
messageScroller.setPrefHeight(300);
messageScroller.prefWidthProperty().bind(messageContainer.prefWidthProperty().subtract(5));
messageScroller.setFitToWidth(true);
//Make the scroller scroll to the bottom when a new message is added
speechBubbles.addListener((ListChangeListener<Node>) change -> {
while (change.next()) {
if(change.wasAdded()){
messageScroller.setVvalue(messageScroller.getVmax());
}
}
});
}
private void setupInputDisplay(){
inputContainer = new HBox(5);
TextField userInput = new TextField();
userInput.setPromptText("Enter message");
Button sendMessageButton = new Button("Send");
sendMessageButton.disableProperty().bind(userInput.lengthProperty().isEqualTo(0));
sendMessageButton.setOnAction(event-> {
sendMessage(userInput.getText());
userInput.setText("");
});
//For testing purposes
Button receiveMessageButton = new Button("Receive");
receiveMessageButton.disableProperty().bind(userInput.lengthProperty().isEqualTo(0));
receiveMessageButton.setOnAction(event-> {
receiveMessage(userInput.getText());
userInput.setText("");
});
inputContainer.getChildren().setAll(userInput, sendMessageButton, receiveMessageButton);
}
public void sendMessage(String message){
speechBubbles.add(new SpeechBox(message, SpeechDirection.RIGHT));
}
public void receiveMessage(String message){
speechBubbles.add(new SpeechBox(message, SpeechDirection.LEFT));
}
}
Output:

NullPointerException at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion LIBGDX

I am getting null pointer exception when I try spawning an item(Mushroom) when the player collides with a particular coin(As in MARIO game). I have been following this tutorial but I want the inputs to be buttons and not the keys. I have created 4 buttons and added to the stage . The inputs are working perfectly . But I am getting exception when,
I simply add the buttons to the stage and the input is still using keys
I am not getting exception when,
I remove all the buttons from the code and the input is using keys.
Here is my code for adding buttons to the stage ,
public class PlayScreen implements Screen, InputProcessor
{
.......
private Array<Item> items;
private LinkedBlockingQueue<ItemDef> itemsToSpawn;
private TextureAtlas atlas = new TextureAtlas("move_sprites.pack"); //contains the mushroom as well as the player packed together
public PlayScreen(MyGame game) {
.............
stage = new Stage();
atlas = new TextureAtlas("ui/button_pack.pack");
skin = new Skin(atlas);
white = new BitmapFont(Gdx.files.internal("fonts/white.fnt"), false);
black = new BitmapFont(Gdx.files.internal("fonts/black.fnt"), false);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.getDrawable("buttonin");
textButtonStyle.down = skin.getDrawable("buttonout");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.font = black;
buttonright = new TextButton("Right", textButtonStyle);
buttonleft = new TextButton("Left", textButtonStyle);
//table.setBounds(500, 5, 250, 150);
buttonleft.setPosition(350, 5);
buttonleft.setHeight(100);
buttonleft.setWidth(100);
buttonright.setPosition(100, 5);
buttonright.setHeight(100);
buttonright.setWidth(100);
buttonup = new TextButton("up", textButtonStyle);
buttonup.setPosition(600, 5);
buttonup.setHeight(100);
buttonup.setWidth(100);
stage.addActor(buttonup);
stage.addActor(buttonright);
stage.addActor(buttonleft);
Gdx.input.setInputProcessor(stage);
......
}
//create a new method
public void spawnItem(ItemDef idef)
{
itemsToSpawn.add(idef);
}
public void handleSpawingItems()
{
if (!itemsToSpawn.isEmpty()) {
ItemDef idef = itemsToSpawn.poll(); //poll is pop
if (idef.type == Mushroom.class) {
items.add(new Mushroom(this, idef.position.x, idef.position.y));
}
}
}
public void update(float dt) {
handleInput(dt);
handleSpawingItems();
........
}
//Giving inputs using buttons
public void handleInput(float dt)
{
if (gamehero.currentState != Hero.State.DEAD) {
buttonup.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
gamehero.heroBody.setLinearVelocity(0, 4f);
}
return true;
}
}
);
}
}
public void render(float delta)
{
......
update(delta);
stage.act();
stage.draw();
}
}
}
Mushroom Class..
public class Mushroom extends Item {
public Mushroom(PlayScreen screen, float x, float y) {
super(screen, x, y);
//mush is one of the sprite packed together using texture packer. Exception is thrown in the below line
setRegion(screen.getAtlas().findRegion("mush"), 0, 0, 16, 16);
velocity = new Vector2(0.7f, 0); //velocity of mushroom
}
I am triggering this mushroom when mario head collides with coin like below,
public class Coins extends InteractiveTileObject {
....
public void onHeadHit(Hero hero) {
if (object.getProperties().containsKey("mushroom")) {
Gdx.app.log("coins", "collision");
//Exception occurs in the below line too. When the collision occurs, spawnItem method is called in the main class
screen.spawnItem(new ItemDef(new Vector2(body.getPosition().x, body.getPosition().y + 16 / MyGame.PPM), Mushroom.class));
}
ItemDef Class,
public class ItemDef {
public Vector2 position;
public Class<?> type;
public ItemDef(Vector2 position, Class<?> type)
{
this.position = position;
this.type = type;
}
}
Exception occurs if I just add a button to my code and gave the input using keys. Problem lies in spawning the mushroom if I just add a button to my code. Its weird. Please help. I am a beginner . Thanks in advance
I found the cause for this Exception.
I have given the same name for 2 TextureAtlas while declaring,
private TextureAtlas atlas = new TextureAtlas("move_sprites.pack");
and the other,
atlas = new TextureAtlas("ui/button_pack.pack");
But I am referring the first TextureAtlas in other class. Since I have declared the TextureAtlas only once but used for two different packs, It threw null pointer exception as there was name conflict. Thanks for all your answers.

Resources